Zum Inhalt springen

CSS top-Eigenschaft

Die top-Eigenschaft definiert die obere Position eines Elements in Kombination mit der position-Eigenschaft.

Die Wirkung der top-Eigenschaft hängt davon ab, wie das Element positioniert ist (siehe position-Eigenschaft).

  • Wenn position auf "absolute" oder "fixed" gesetzt ist, gibt die top-Eigenschaft die obere Kante eines Elements relativ zu einer Einheit oberhalb/unterhalb der oberen Kante seines nächsten positionierten Elternelements an.
  • Wenn position auf "relative" gesetzt ist, gibt die top-Eigenschaft an, um wie viel die obere Kante oberhalb/unterhalb ihrer Normalposition verschoben werden soll.
  • Wenn position auf "sticky" gesetzt ist, wechselt die top-Eigenschaft ihre Position zu relativ, wenn sich das Element im Viewport befindet, und zu fixed, wenn es sich außerhalb befindet.
  • Wenn die position-Eigenschaft auf "static" gesetzt ist, wird die Eigenschaft nicht angewendet.

INFO

Negative Werte sind erlaubt.

Initialwertauto
Anwendbar aufPositionierte Elemente.
VererbtNein.
AnimierbarJa.
VersionCSS2
DOM-SyntaxObject.style.top = "50px";

Syntax

Syntax der CSS top-Eigenschaft

css
top: auto | length | initial | inherit;

Beispiel zur top-Eigenschaft:

Beispiel zur CSS top-Eigenschaft mit Längenwert

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: #8ebf42;
        height: 200px;
        width: 600px;
        position: relative;
      }
      p {
        margin: 0;
        color: #eee;
        position: absolute;
        border: 2px solid #666;
      }
      .ex1 {
        top: 0;
      }
      .ex2 {
        top: 50px;
      }
    </style>
  </head>
  <body>
    <h2>Top property example</h2>
    <div>
      <p class="ex1">Some text (top: 0;)</p>
      <p class="ex2">
        Lorem ipsum is simply dummy text...(this text is positioned 50 pixels down from the top edge of the containing positioned element.)
      </p>
    </div>
  </body>
</html>

Ergebnis

CSS top-Eigenschaft

Beispiel zur top-Eigenschaft mit negativem Wert:

Beispiel zur CSS top-Eigenschaft mit negativem Wert

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: #666;
        height: 200px;
        position: relative;
      }
      p {
        margin: 0;
        color: #fff;
      }
      .top {
        position: absolute;
        top: -35px;
        color: #000000;
      }
    </style>
  </head>
  <body>
    <h2>Top property example</h2>
    <div>
      <p>Some text.</p>
      <p class="top">Text with the top property.</p>
    </div>
  </body>
</html>

Beispiel zur top-Eigenschaft mit Werten in "pt", "%" und "em":

Beispiel zur top-Eigenschaft mit Werten in "pt", "%" und "em":

html
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      div {
        background-color: #8ebf42;
        height: 200px;
        width: 600px;
        position: relative;
      }
      p {
        margin: 0;
        color: #eee;
        position: absolute;
        border: 2px solid #666;
      }
      .ex1 {
        top: 5em;
      }
      .ex2 {
        top: 10pt;
      }
      .ex3 {
        top: 75%;
      }
    </style>
  </head>
  <body>
    <h2>Top property example</h2>
    <div>
      <p class="ex1">Some text (top: 0;)</p>
      <p class="ex2">
        Lorem ipsum is simply dummy text...(this text is positioned 50 pixels down from the top edge of the containing positioned element.)
      </p>
      <p class="ex3">
        Lorem ipsum is simply dummy text...(this text is positioned 50 pixels down from the top edge of the containing positioned element.)
      </p>
    </div>
  </body>
</html>

Werte

WertBeschreibungAusführen
autoLegt die Position der oberen Kante fest. Dies ist der Standardwert dieser Eigenschaft.Ausführen »
lengthLegt die Position der oberen Kante mit px, cm usw. fest. Negative Werte sind gültig.Ausführen »
%Legt die Position der oberen Kante in % des Elternelements fest. Negative Werte sind gültig.Ausführen »
initialLegt die Eigenschaft auf ihren Standardwert zurück.Ausführen »
inheritErbt die Eigenschaft von ihrem Elternelement.

Praxis

Was bewirkt die CSS-Eigenschaft 'top'?

Finden Sie das nützlich?

Dual-run-Vorschau — vergleichen Sie mit den Symfony-Routen live.