Lesson 18 of 52Layout
Positioning
Move elements out of the normal flow with relative, absolute, fixed and sticky positioning, and control what sits on top with z-index.
Flexbox and Grid decide where things go by arranging boxes next to each other. Sometimes you need something to break out of that arrangement: a "New" badge pinned to the corner of a card, a header that stays at the top while you scroll, a dialog that floats above the whole page. That is the job of the position property. It is small, with only five values, but it is behind a huge number of everyday interface details. It is also behind some of the most confusing CSS bugs, usually involving z-index, so this lesson covers how the layering really works.
Normal flow first
By default, every element is laid out in normal flow. Block elements stack top to bottom, inline elements sit side by side in lines of text, and each box takes up space that pushes the next one along. Flexbox and Grid are variations on that idea: the boxes still take up space and still affect each other.
The position property decides whether an element stays in that flow, and whether you can nudge it with the offset properties top, right, bottom and left. There is also a shorthand, inset, that sets all four at once: inset: 0 means top, right, bottom and left are all 0.
| Value | Stays in flow? | Offsets measured from | Typical use |
|---|---|---|---|
static | Yes | Offsets ignored | The default for every element |
relative | Yes | Its own normal position | Small nudges, anchor for absolute children |
absolute | No | Nearest positioned ancestor | Badges, icons, dropdown menus |
fixed | No | The viewport (browser window) | Chat buttons, full-screen overlays |
sticky | Yes, until it sticks | Its scroll container | Headers, table headings |
static and relative
static is the default. A static element sits in the flow and ignores top, left and friends. You rarely write it, except to undo another rule.
relative keeps the element in the flow, so it still takes up its original space. Offsets then shift it visually from where it would have been. top: 10px moves it down 10px (away from the top), and left: 10px moves it right. The space it left behind stays empty; neighbours do not move to fill it.
Honestly, you will rarely nudge things with relative. Its real job is the next section: being an anchor for absolutely positioned children.
absolute: the badge on a card
An absolute element is taken out of the flow. It no longer takes up space, and the boxes around it act as if it were not there. Its offsets are measured from its containing block: the nearest ancestor that has a position other than static. If no ancestor qualifies, it uses the initial containing block, which is roughly the first screen of the page.
This is why you so often see this pair:
.card { position: relative; } /* becomes the anchor */
.badge { position: absolute; top: 8px; right: 8px; }The card stays exactly where it was, but now any absolute child measures from the card's corners instead of the page's.
Preview
Try deleting position: relative; from .card. The badge jumps to the top right corner of the whole preview, because it now measures from the page. Put it back, then change right: -10px to left: -10px to move the badge to the other corner.
span has position: absolute; top: 0; left: 0;. Its parent is static, but its grandparent has position: relative. Where does the span end up?You can also size an absolute element with its offsets. position: absolute; inset: 0; stretches it to cover its containing block exactly, which is perfect for a dark overlay on top of an image.
fixed: pinned to the screen
fixed is like absolute, but the containing block is the viewport: the visible area of the browser window. A fixed element stays in the same place on the screen while the page scrolls underneath. Chat buttons in the bottom right corner and cookie banners are usually fixed.
One trap: if any ancestor has a transform, filter or perspective, that ancestor becomes the containing block instead, and your "fixed" element scrolls with it. If a fixed element mysteriously moves, check its ancestors for a transform.
sticky: in flow until it sticks
sticky is a hybrid. The element behaves like a normal element in the flow until you scroll it to a threshold you set, then it sticks there like a fixed element, but only while its parent is still on screen. You must give it a threshold, usually top: 0. Without one, sticky does nothing.
Preview
Scroll the contact list. Each letter sticks to the top until the next letter pushes it away. Change top: 0 to top: 20px and see where they stick now. Then delete the top line completely and notice the headings scroll away like normal text.
Sticky sticks within its scroll container: the nearest ancestor that scrolls. A frequent bug is a parent with overflow: hidden, which quietly becomes the scroll container, so the sticky element never has anything to stick to. If sticky does not work, look for overflow on the ancestors first.
Match each interface detail to the position value you would usually use.
z-index: who sits on top
When positioned boxes overlap, the browser has to decide which one is drawn in front. By default, later elements in the HTML paint over earlier ones. z-index overrides that. Higher numbers come forward, lower numbers go back, and negative numbers are allowed.
Two rules catch almost everyone:
z-indexonly works on positioned elements (anything butstatic) and on flex or grid items. On a plain static block it does nothing.z-indexvalues are only compared within the same stacking context.
Stacking contexts
A stacking context is a group of elements that are layered together as one unit. Inside the group, children are ordered by their own z-index. But the whole group is then placed in its parent's stack as a single layer. A child can never escape its group, however large its z-index.
Things that create a new stacking context include:
position: relativeorabsolutewith a z-index other thanautoposition: fixedorsticky(always)opacityless than 1transform,filterand a few other visual effectsisolation: isolate, which exists purely to create one
Normal flow content is painted first, at the back.
Here is the classic bug. The dropdown has z-index: 999, yet it hides behind the banner, because its parent created a stacking context at level 1.
Preview
Fix it in one of two ways. Change the .nav z-index to 3 so the whole nav group sits above the banner. Or delete z-index: 1 from .nav; it then no longer forms a stacking context, and the menu's 999 is compared against the banner directly.
z-index: 50 but still appears behind a sibling with no z-index. The element is position: static. What is the fix?A modal overlay
A modal combines everything: a fixed overlay covering the screen with inset: 0, a high z-index, and the dialog centred inside it. For a real site, the HTML dialog element with showModal() handles focus and the Escape key for you and draws in the browser's top layer, above every z-index. Still, knowing how to build the overlay explains what it is doing.
Preview
Open the dialog, then change inset: 0 to inset: 40px to see the overlay shrink away from the edges. Change the last number in the background colour from 0.5 to 0.9 to make it darker.
Put the steps for adding a corner badge to a card in order.
- Tap the steps below in the right order.
- normal flow
- The default layout where boxes take up space and push each other along.
- offset properties
- top, right, bottom, left and the inset shorthand, used to move positioned elements.
- containing block
- The box an absolute or fixed element measures its offsets from.
- viewport
- The visible area of the browser window.
- z-index
- A number that orders overlapping positioned elements within one stacking context.
- stacking context
- A group of elements layered together as a single unit in its parent's stack.