index.html1.b {2 position: relative;3 top: 20px; left: 30px;4}5.tag {6 position: absolute;7 top: 0; right: 0;8}9.card {10 position: relative;11}1<div class="card">2 <div class="tag">3 NEW</div>4 <div class="a">A</div>5 <div class="b">B</div>6 <div class="c">C</div>7</div>8<div class="more"></div>.more: the rest of the page.cardACNEWBrelativetop/leftabsolutetop/rightrelativefixed
Left: style.css, the three rules. Front right: index.htmlBack right: the browser window (320 × 240 CSS px) and the boxes it drawsDashed: B's reserved space and the tag's containing blockLeft: the CSS rules this lesson adds. Back right: the browser window, drawn to scale, showing the card with its NEW tag and boxes A, B and C. Front right: the HTML. relative shifts B but keeps its space; absolute takes the tag out of the flow and places it against its containing block (the page, then the positioned card); fixed places it against the window, so it stays put when the page scrolls. A real browser lays out the page once with all the rules in force. The figure adds one rule at a time so you can see what each line does.

Normal flow

  1. Every box starts as position: static, the default. Static boxes sit in normal flow: inside the card, each block goes below the one before it, in the order of the HTML. First the NEW tag, then A, B and C.

Relative

Absolute

Fixed

What happens

;; step 1/8 · flow
index.html, in <body>:
  <div class="card">
    <div class="tag">
      NEW</div>
    <div class="a">A</div>
    <div class="b">B</div>
    <div class="c">C</div>
  </div>
all static: one below the other (CSS px)
  .tag  x 28, y 28, 48×24
  .a    x 28, y 60, 120×40
  .b    x 28, y 108, 120×40
  .c    x 28, y 156, 120×40

A real browser lays out the page once with all the rules in force. The figure adds one rule at a time so you can see what each line does. The window is 320 by 240 CSS px. The .more box stands in for the rest of a long page, so there is something to scroll. The figure ignores the scrollbar (overlay scrollbars, as on phones, take no space).