IntermediateLesson 11 of 306 min read

CSS display and position: relative, absolute, fixed and sticky

Control where elements sit with display, position, offsets and z-index, including badges, overlays and sticky headers.

In this lesson you will

  • Choose the right display value
  • Use relative, absolute, fixed and sticky
  • Layer elements with z-index
  • Know when not to use positioning

About 15 minutes

Where does everything go?

In the beginner lessons you styled the look of boxes. Now you will control where they sit. Open any website and notice the layers: a menu that stays glued to the top as you scroll, a badge sitting on the corner of a picture, a popup floating above everything. All of that comes from two ideas: the display property and the position property. Understand them and you can build almost anything that overlaps, sticks or floats.

Normal flow

Without any CSS, the browser lays boxes out in normal flow. Block elements stack top to bottom, each taking the full width. Inline elements sit side by side inside a line, like words in a sentence. Everything you do next either works with that flow or deliberately steps out of it. Most of the time you want to stay inside the flow, and use flexbox and grid, which you meet in the next lessons, to arrange it. Positioning is for the exceptions.

The display property

  • block: starts on a new line and fills the width. Headings, paragraphs and divs.
  • inline: flows in a line of text. Links, spans and strong text. Width and height do not apply.
  • inline-block: flows like text but behaves like a box with width, height and padding.
  • none: the element disappears and takes no space. (visibility: hidden hides it but keeps the space.)
  • flex and grid: the modern layout modes you learn in the following lessons.

Changing display changes how an element behaves without changing the HTML. Turning a list of links into a horizontal menu is often just display: inline-block or flex on the list items.

The position property

Position has five values. The first is the default, and the other four are the tools.

static: the default

The element sits in normal flow. The properties top, left, right and bottom do nothing to it.

relative: nudge from where it should be

.badge { position: relative; top: 4px; left: 10px; }

A relative element keeps its place in the flow, so the gap it leaves behind stays, but it is drawn shifted by the offsets. Its bigger job is to act as an anchor for absolutely positioned children.

absolute: placed inside a container

.card { position: relative; }
.card .tag {
  position: absolute;
  top: 12px;
  right: 12px;
}

An absolute element is removed from the flow, as if it were not there, and placed using offsets measured from its nearest positioned ancestor, meaning the closest parent whose position is anything other than static. That is why you give the card position: relative: it becomes the reference frame. If no ancestor is positioned, the element is placed relative to the page itself, which is rarely what you intended.

Pin the badge to the top right corner of the card, 10px from each edge. The card needs position: relative so the badge uses it as its reference.

<style>
 .card { /* anchor */ width: 240px; height: 140px; background: #EFEAFD; border-radius: 14px; padding: 16px; font-family: Arial; box-sizing: border-box; }
 .badge { /* pin me */ background: #5A28C8; color: #fff; padding: 4px 10px; border-radius: 999px; font-size: 13px; }
</style>
<div class="card">Course card <span class="badge">NEW</span></div>

Add position: relative to .card and position: absolute; top: 10px; right: 10px; to .badge.

<style>
 .card { position: relative; width: 240px; height: 140px; background: #EFEAFD; border-radius: 14px; padding: 16px; font-family: Arial; box-sizing: border-box; }
 .badge { position: absolute; top: 10px; right: 10px; background: #5A28C8; color: #fff; padding: 4px 10px; border-radius: 999px; font-size: 13px; }
</style>
<div class="card">Course card <span class="badge">NEW</span></div>

fixed: glued to the window

.help-button {
  position: fixed;
  bottom: 20px;
  right: 20px;
}

A fixed element is positioned relative to the browser window and stays put while the page scrolls. It suits chat buttons, cookie notices and always-visible bars. Use it with care on phones, where it can cover content and the on-screen keyboard.

sticky: normal, then stuck

.toc {
  position: sticky;
  top: 20px;
}

A sticky element scrolls normally until it reaches the offset you give, then it sticks. It is wonderful for table headers, sidebars and section titles, and it needs no JavaScript. It only works when you give it an offset such as top, and when none of its parents hide overflow.

Stacking with z-index

When positioned elements overlap, which sits on top? By default, the one later in the HTML. The z-index property lets you decide: a higher number is closer to the viewer.

.navbar  { position: sticky; top: 0; z-index: 100; }
.modal   { position: fixed; inset: 0; z-index: 1000; }
.overlay { z-index: 900; }

z-index only works on positioned elements (and on flex and grid items). Do not escalate to 99999. Pick a small scale, such as 1, 10, 100 and 1000, write it in a comment, and stick to it. If a z-index seems to be ignored, a parent probably created a separate stacking context, which traps its children inside it.

Make the header stay at the top while the page scrolls. Use position: sticky, top: 0 and a z-index, then scroll inside the preview.

<style>
 body { margin: 0; font-family: Arial; }
 header { background: #14112E; color: #fff; padding: 14px; }
 p { padding: 0 14px; line-height: 1.7; }
</style>
<header>My sticky header</header>
<p>Scroll down in this preview. The text below is long enough to scroll.</p>
<p style="margin-top:400px">More content far below.</p>
<p style="margin-top:400px">Even more content.</p>

header { position: sticky; top: 0; z-index: 10; }

<style>
 body { margin: 0; font-family: Arial; }
 header { position: sticky; top: 0; z-index: 10; background: #14112E; color: #fff; padding: 14px; }
 p { padding: 0 14px; line-height: 1.7; }
</style>
<header>My sticky header</header>
<p>Scroll down in this preview. The text below is long enough to scroll.</p>
<p style="margin-top:400px">More content far below.</p>
<p style="margin-top:400px">Even more content.</p>

The inset shortcut

inset: 0 is shorthand for top, right, bottom and left all at 0. On an absolute or fixed element it stretches the element to fill its container. It is how you build full-cover overlays in one line.

Common patterns

  • Badge on a corner: parent relative, badge absolute with top and right.
  • Full-cover overlay on an image: wrapper relative, overlay absolute with inset: 0.
  • Sticky header or table heading: position: sticky; top: 0.
  • Floating action button: position: fixed; bottom; right.
  • Centring something absolutely: top: 50%; left: 50%; transform: translate(-50%, -50%). Flexbox is simpler for most cases, as you will see.
Common mistake

Using absolute positioning to build whole page layouts. Absolute elements do not take up space, so their neighbours collapse into the gap and the page breaks on different screen sizes. Use flexbox and grid for layout, and keep positioning for small decorations and overlays.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which position value keeps an element in the flow but lets you nudge it?

  1. static
  2. relative
  3. absolute
  4. fixed

A relative element keeps its place and is drawn shifted.

An absolute element is positioned relative to…

  1. The window always
  2. Its nearest positioned ancestor
  3. The previous sibling
  4. The body only

That is why the parent often gets position: relative.

What does position: sticky; top: 0 do?

  1. Fixes the element forever
  2. Scrolls normally then sticks at the top
  3. Hides the element
  4. Moves it to the bottom

It sticks when it reaches the offset.

Which elements does z-index apply to?

  1. Static ones only
  2. Positioned elements and flex or grid items
  3. Only images
  4. Only text

z-index needs a positioned element or a flex/grid item.

Why avoid absolute positioning for page layout?

  1. It is deprecated
  2. Absolute elements take no space, so the layout breaks on other screens
  3. It is slow
  4. It disables links

Use flexbox and grid for layout.

Summary

  • Display changes how an element behaves; position moves it.
  • Absolute elements use their positioned parent as the reference.
  • Sticky and fixed keep things in view.
  • Keep z-index values on a small scale.

Next lesson

Next: Flexbox, the layout tool you will use every day.

Questions

Why is my absolute element in the wrong place?

Its parent probably has position static. Give the parent position: relative.

Fixed or sticky?

Fixed always stays in the window. Sticky scrolls with the page until it reaches its offset.

Why is z-index ignored?

The element may not be positioned, or a parent creates a separate stacking context.

Progress is saved in this browser.