What you’ll learn
By the end, you’ll be able to explain how display affects an element’s layout, choose between the main position values, anchor an overlay to the right element, and troubleshoot basic z-index and sticky-position issues.
1. Normal flow: the browser’s default layout
Before positioning elements, it helps to know what happens when you don’t set a special position. The browser lays content out in normal flow:
- Block elements—such as headings, paragraphs and sections—typically stack vertically.
- Inline content—such as text and links—flows along a line and wraps when needed.
Most page content should stay in normal flow. It keeps the page responsive and lets later content move naturally when earlier content changes.
<p>This paragraph is in normal flow.</p>
<p>This one follows it.</p>p {
margin-block: 1rem;
}The second paragraph appears below the first because that is the normal layout for block elements.
2. display: how an element participates in layout
The display property changes an element’s layout behaviour. This lesson focuses on four values:
| Value | What it does | Typical use |
|---|---|---|
block | Starts on a new line and usually fills the available width | Sections and paragraphs |
inline | Flows within a line of text | Links and emphasized words |
inline-block | Flows inline, but accepts width and height | Small badges or compact controls |
none | Removes the element from the rendered layout | Content intentionally hidden |
For example, a <span> is normally inline. Changing it to inline-block lets you give it a more predictable box:
<span class="badge">New</span>.badge {
display: inline-block;
padding: 0.25em 0.6em;
border-radius: 0.4em;
background: #dbeafe;
}With display: none, the element takes no space in the layout:
.notice {
display: none;
}Use that intentionally: hidden content may no longer be available to people using assistive technology. If something should be hidden only visually while remaining accessible, that requires a different technique.
Quick check 1: Which value lets an element sit alongside text while still accepting a width?
3. The position values
CSS provides several ways to change where an element appears. The important distinction is whether it stays in normal flow.
static: the default
.card {
position: static;
}A static element stays in normal flow. Offset properties such as top and left do not reposition it.
relative: move visually, keep its space
A relatively positioned element remains in normal flow. You can nudge it using an inset such as top or left, but its original space is still reserved.
.label {
position: relative;
top: 4px;
left: 6px;
}The label appears shifted, but neighbouring content still behaves as though the label were in its original place.
A common use is to make an element the anchor for an absolutely positioned child. You’ll see that in the next section.
absolute: position relative to an anchor
An absolutely positioned element is removed from normal flow, so it no longer reserves its usual space. It is placed relative to its containing block—often the nearest ancestor with a positioning value other than static.
<div class="card">
<h2>Lesson notes</h2>
<span class="card__badge">New</span>
</div>.card {
position: relative;
}
.card__badge {
position: absolute;
top: 0.75rem;
right: 0.75rem;
}Here, .card is the anchor. The badge is placed inside its top-right corner rather than somewhere on the page.
Important: If no suitable ancestor establishes the containing block, the absolute element may be positioned relative to a more distant ancestor or the page. When it appears in an unexpected place, check which ancestor is acting as its anchor.
fixed: keep it in a viewport position
A fixed element is removed from normal flow and usually stays in the same position in the browser viewport as the page scrolls.
.help-button {
position: fixed;
right: 1rem;
bottom: 1rem;
}This can suit a persistent help button. But fixed controls can cover content, particularly on small screens, so check the page at different window sizes.
sticky: stick after reaching a threshold
A sticky element behaves like normal flow content until it reaches an inset threshold. It then sticks within its scrolling container.
.site-header {
position: sticky;
top: 0;
z-index: 10;
}The top: 0 tells the header where to stick. Without an inset such as top, bottom, left or right, sticky positioning has no threshold to use.
Sticky positioning can be affected by the element’s ancestors and scroll container. If it does not stick as expected, check:
- whether an inset value is set
- whether the element has room to move within its container
-
whether an ancestor’s
overflowsetting changes the scrolling behaviour
Quick check 2: Which position value keeps the element’s original space while visually nudging it?
4. Insets: placing positioned elements
Use top, right, bottom and left to position elements. These are often called inset properties.
.corner-label {
position: absolute;
top: 0;
right: 0;
}For a centered overlay, place its top-left corner at the centre, then shift it back by half its own size:
.overlay {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}The percentages in top and left are measured from the containing block. The percentages in translate() are measured from the overlay’s own box.
5. z-index: controlling which element appears on top
When positioned elements overlap, z-index can control their paint order:
.card {
position: relative;
}
.card__badge {
position: absolute;
z-index: 1;
}A larger z-index does not guarantee that an element will appear above everything else on the page. Elements are also affected by stacking contexts—groups that are painted together. A child cannot simply out-rank elements outside its parent’s stacking context.
Some properties that can create stacking contexts include a positioned element with a non-auto z-index, a transform, and opacity below 1.
If z-index seems to have no effect:
-
Check whether the element can use
z-indexin its current layout. - Inspect its ancestors for stacking contexts.
-
Check whether an ancestor is clipping it with
overflow. - Use small, purposeful values rather than extremely large numbers.
6. Three practical patterns
A badge inside a card
<article class="card">
<span class="card__badge">New</span>
<h2>Getting started</h2>
<p>Read the introduction before beginning the exercises.</p>
</article>.card {
position: relative;
padding: 1.5rem;
border: 1px solid #cbd5e1;
border-radius: 0.75rem;
}
.card__badge {
position: absolute;
top: 0.75rem;
right: 0.75rem;
}A sticky section heading
.section-heading {
position: sticky;
top: 0;
padding: 0.75rem;
background: white;
z-index: 5;
}Test it by scrolling through enough content to reach the sticky threshold.
A fixed corner button
.back-to-top {
position: fixed;
right: 1rem;
bottom: 1rem;
z-index: 20;
}Check that the button does not cover important text or controls.
Common mistakes
| Symptom | What to check |
|---|---|
| An absolute element appears in the wrong place | Find its containing block; set position: relative on the intended anchor |
| An absolute element overlaps later content | It is removed from normal flow; reserve space or choose a flow-based layout |
| A sticky element does not stick | Add an inset such as top: 0; check scrolling ancestors and available space |
z-index appears ineffective | Inspect stacking contexts and ancestors |
| An overlay is cut off | Check for ancestor overflow clipping it |
| A fixed button covers content | Reposition it or adjust the design for smaller screens |
Exercises
Try these before opening the answer key.
1. Display choices. Which display value would you choose for a small badge that should sit beside text and have padding?
2. Keep the space. A label should move down slightly, but the layout should keep its original space. Which position value fits?
3. Anchor a badge. Write the two key positioning rules needed to place a badge at the top-right corner of a card.
4. Diagnose sticky positioning. A header has position: sticky but scrolls away normally. What property should you check first?
5. Explain the overlap. An absolutely positioned badge covers part of the text underneath it. Why can this happen?
6. Build it. Create a card with a heading, two paragraphs and a “Featured” badge. Position the badge inside the card’s top-right corner. Resize the browser and check that it stays anchored to the card.
7. Experiment. Give two overlapping elements different z-index values. Then add a transform to an ancestor and observe whether the stacking behaviour changes. Use DevTools to inspect the elements and their ancestors.
Free resources
These free resources are useful for continuing the lesson:
- MDN Web Docs: search for the guides titled CSS display and CSS position for detailed explanations and examples.
- MDN Web Docs: look up Understanding CSS z-index for stacking contexts and paint order.
-
Browser DevTools: use Inspect to check computed
position, inset values,z-index,overflowand ancestor styles. - Practice idea: recreate the badge, sticky header and fixed button patterns in a small page, then test each at different browser sizes.
Key takeaways
- Normal flow is the browser’s default layout; keep ordinary content in it where possible.
-
relativekeeps an element’s space;absoluteandfixedremove it from normal flow. - An absolute element is positioned relative to its containing block, so choose its anchor deliberately.
- Sticky positioning needs an inset and is influenced by its scrolling container.
-
z-indexis affected by stacking contexts, not just the size of its number.
Quick check 1: inline-block.
Quick check 2: relative.
Exercise 1: inline-block.
Exercise 2: position: relative—it moves visually but keeps its original space in normal flow.
Exercise 3: Make the card position: relative, then make the badge position: absolute with top and right insets.
.card {
position: relative;
}
.card__badge {
position: absolute;
top: 0;
right: 0;
}Exercise 4: Check for an inset, such as top: 0. Also inspect the relevant scrolling container and ancestors.
Exercise 5: An absolutely positioned element is removed from normal flow, so the surrounding content does not reserve space for it. The badge can therefore overlap other content.
Exercise 6: Your badge should remain inside the card as the viewport changes. The card needs to establish the containing block; the badge needs absolute positioning and suitable insets.
Exercise 7: Results depend on the exact elements and ancestor styles. Look for a stacking-context boundary created by the ancestor; a child’s z-index cannot escape its parent’s stacking context.
Next: Lesson 9 is Flexbox—a flexible way to arrange items in rows or columns and control alignment and spacing.