Tag 4: Positionierung & Anzeige

Ziel

Lerne, wie du die Position von Elementen auf der Seite mit display, position, float und z-index steuerst.

Themen

Display Beispiele

div {
  display: block;        /* nimmt die volle Breite ein */
}

span {
  display: inline;       /* fließt mit dem Text */
}

button {
  display: inline-block; /* wie inline, aber mit Breite/Höhe */
}

.hidden {
  display: none;         /* Element ist versteckt */
}

Position Beispiele

img.absolute {
  position: absolute;
  top: 10px;
  right: 10px;
}

div.relative {
  position: relative;
  top: 20px;             /* verschiebt sich relativ zur Ausgangsposition */
}

header.fixed {
  position: fixed;
  top: 0;
  width: 100%;
  background: lightgray;
  z-index: 100;
}

Float & Clear

div.left {
  float: left;
  width: 45%;
}

div.right {
  float: right;
  width: 45%;
}

.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

Übung

Beispiel HTML & CSS

<!DOCTYPE html>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <title>CSS Positionierung Übung</title>
  <style>
    img {
      position: absolute;
      top: 10px;
      right: 10px;
      width: 100px;
    }

    .box {
      display: inline-block;
      width: 45%;
      height: 100px;
      background-color: #3498db;
      margin: 5px;
      color: white;
      text-align: center;
      line-height: 100px;
      font-weight: bold;
    }

    .flex-container {
      display: flex;
      justify-content: space-between;
    }
  </style>
</head>
<body>

  <h1>Positionierung & Anzeige Übung</h1>

  <img src="example.jpg" alt="Beispielbild">

  <div class="flex-container">
    <div class="box">Box 1</div>
    <div class="box">Box 2</div>
  </div>

</body>
</html>