Day 4: Positioning & Display

Goal

Learn how to control the placement of elements on the page using display, position, float, and z-index.

Topics

Display Examples

div {
  display: block;        /* occupies full width */
}

span {
  display: inline;       /* flows with text */
}

button {
  display: inline-block; /* behaves like inline but can have width/height */
}

.hidden {
  display: none;         /* element is hidden */
}

Position Examples

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

div.relative {
  position: relative;
  top: 20px;             /* moves relative to original position */
}

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;
}

Practice

Example HTML & CSS

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>CSS Positioning Practice</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>Positioning & Display Practice</h1>

  <img src="example.jpg" alt="Example Image">

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

</body>
</html>