liveBook · Manning

Chapter 6: Positioning and stacking contexts

What you will learn in chapter 6 of CSS in Depth, Second Edition

Introduces the position property, defines when an element is considered positioned, and contrasts positioning with earlier flow-based layout methods.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the position property, defines when an element is considered positioned, and contrasts positioning with earlier flow-based layout methods.

Key terms: position property, static, positioned, document flow, positioning, overlapping

6.1 Fixed positioning

Explains how fixed positioning uses viewport-relative offsets and how inset shorthands express those offsets.

Key terms: position: fixed, viewport, top, right, bottom, left, inset, inset-block, inset-inline, inset-block-start

6.1.1 Creating a modal dialog with fixed positioning

Builds a modal dialog whose backdrop and content stay in place over a scrolling page by using fixed positioning.

Key terms: modal dialog, modal-backdrop, modal-body, is-open, display: none, display: block, fixed positioning, inset, alpha, viewport

6.1.2 Preventing the screen from scrolling while the modal dialog is open

This section explains how to prevent the page behind an open modal dialog from scrolling while keeping the modal’s visual behavior in CSS.

Key terms: overflow: hidden, body, no-scroll class, modal, body.no-scroll, :has(), body:has(.modal.is-open), is-open class

6.1.3 Controlling the size of positioned elements

This subsection explains how to size a fixed-positioned element with selected offsets and how to keep page content from flowing behind it.

Key terms: fixed element, width, height, naturally, document flow, right-margin

6.2 Absolute positioning

Introduces absolute positioning by contrasting its containing block with fixed positioning and explaining how inset properties place its edges.

Key terms: absolute positioning, containing block, closest-positioned ancestor element, inset properties

6.2.1 Absolutely positioning the Close button

This subsection uses an absolutely positioned Close button to show how CSS chooses its containing block and applies top and right offsets.

Key terms: absolute positioning, containing block, modal-body, positioned element, initial containing block

6.2.2 Positioning a pseudo-element

Shows how to display an × on an absolutely positioned Close button while keeping meaningful button text for screen readers.

Key terms: screen readers, HTML, Close button, text-indent, overflow, content, ::after pseudo-element, absolute positioning, containing block

6.3 Relative positioning

This section explains how relative positioning shifts an element without moving its neighbors and why it is often used as a containing block for absolute positioning.

Key terms: position: relative, inset properties, normal document flow, top, bottom, left, right, containing block, absolutely positioned element

6.3.1 Creating a dropdown menu

This section builds a click-operated dropdown menu and explains how positioning, CSS state, and keyboard access work together.

Key terms: relative positioning, absolute positioning, containing block, left: 0, top: 2.1em, min-width: 100%, right: 0, toggle button, is-open class, opened and closed styles

6.3.2 Creating a CSS triangle

Shows how to make a CSS border triangle and use it as a state-changing arrow on a dropdown label.

Key terms: borders, transparent, top border, dropdown-toggle::after, absolute positioning, padding, top

6.4 Stacking contexts and z-index

The section explains why positioned elements need care and illustrates a stacking problem in which a dropdown appears in front of a modal dialog.

Key terms: positioned elements, stacking, modal dialog, dropdown, document flow, browser viewport, overflow

6.4.1 Understanding the rendering process and stacking order

Explains how paint order and positioning determine which overlapping elements appear in front, and why moving a modal in the markup can sometimes resolve a stacking problem.

Key terms: render tree, paint, source order, positioned elements, nonpositioned elements, source-dependent stacking, modal, dropdown menu, fixed positioning, relative positioning

6.4.2 Manipulating stacking order with z-index

This section shows how z-index changes stacking order and identifies two limits to its use.

Key terms: z-index, modal-backdrop, modal-body, positioned elements, static elements, stacking context

6.4.3 Understanding stacking contexts

This section explains why stacking contexts constrain z-index, how elements are ordered within them, and how to manage and debug stacking problems.

Key terms: stacking context, root, descendant elements, z-index, opacity, transform, filter, fixed positioning, sticky positioning, document root

6.5 Sticky positioning

Explains how sticky positioning keeps a sidebar visible during scrolling while confining it to its parent.

Key terms: sticky positioning, fixed positioning, affix menu, top, flex container, parent element, col-sidebar, min-height

6.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Grid layout Chapter 7: Responsive design →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.