← Learning Hub
Intermediate

Display and Positioning

The display property decides how an element takes up space: block, inline, inline-block (inline, but you can set width and height), flex, grid or none (hidden).

Normally, the browser places elements one after another in the normal flow: blocks stack down the page, inline elements line up inside text. display changes how an element joins that flow; position lets you take it out of the flow and put it somewhere specific - like a "SALE" badge in the corner of a card, a sticky header, or a pop-up over the page.

position

  • static - the normal flow (default).
  • relative - moved a little from its normal place with top/left. It also becomes the anchor for absolute children.
  • absolute - taken out of the flow and placed inside the nearest positioned parent. Great for badges on cards.
  • fixed - stuck to the screen, even when you scroll (a chat button).
  • sticky - scrolls normally, then sticks when it reaches the edge (a table header or top menu).
Example
<!DOCTYPE html>
<html>
  <head>
    <style>
      .card  { position: relative; width: 200px; padding: 20px; border: 1px solid #ccc; margin: 20px; }
      .badge { position: absolute; top: -10px; right: -10px; background: #dc2626; color: #fff;
               border-radius: 999px; padding: 4px 10px; font-size: 12px; }
      .bar   { position: sticky; top: 0; background: #111827; color: #fff; padding: 10px; }
      .chat  { position: fixed; bottom: 16px; right: 16px; background: #16a34a; color: #fff;
               border-radius: 50%; width: 56px; height: 56px; display: grid; place-items: center; }
      .tall  { height: 800px; }
    </style>
  </head>
  <body>
    <div class="bar">I stick to the top when you scroll</div>
    <div class="card">Wireless mouse <span class="badge">-30%</span></div>
    <div class="tall"></div>
    <div class="chat">💬</div>
  </body>
</html>
▶ Try it yourself

z-index: who is on top

When elements overlap, the one with the bigger z-index is shown on top. It only works on positioned elements (not static).

Example
.menu    { position: relative; z-index: 10; }
.overlay { position: fixed; inset: 0; z-index: 100; background: rgba(0,0,0,.5); }
inset: 0 is a short way of writing top: 0; right: 0; bottom: 0; left: 0; - it fills the whole parent (or the whole screen for fixed).
Key points
  • display decides how an element takes space.
  • relative, absolute, fixed and sticky change positioning.
  • absolute is placed inside the nearest positioned parent.
  • z-index decides which overlapping element is on top.

📝 Test yourself

1. Which position keeps an element on screen while you scroll the whole page?
2. An absolute badge is placed relative to...
3. Fill in the value that hides an element completely:
display: ;