Command Palette

Search for a command to run...

Hectal
Phase 6Intermediate7 of 12 in CSS

Positioning and Layout Basics

How boxes lay out by default, writing modes, the five position values, z-index and stacking contexts, floats, and every way to center something.

In Phase 5 you learned that every element is a box. This phase is about where those boxes go. First you'll see what the browser does when you write no layout CSS at all: normal flow, where blocks stack from top to bottom and inline content runs along lines. You'll also meet writing modes and logical properties, which let one stylesheet work for English, Arabic and vertical Japanese text.

Then you'll take boxes out of that flow on purpose. The position property moves boxes with relative, pins them with absolute and fixed, and makes them stick while scrolling with sticky. Once boxes can overlap, you need to control which one is on top, so you'll learn z-index and the idea behind every "z-index doesn't work" bug: the stacking context. You'll also learn floats, the oldest layout tool, which is still the right one for wrapping text around an image, and how to stop floats escaping their parent.

The phase ends with centering, the most searched CSS question there is, solved every way that matters and with a clear rule for which one to pick. Every example is live and most print real measured positions, so you can change a value and check the browser's maths. Flexbox (Phase 7) and Grid (Phase 8) build directly on this phase.

0/5 · 0%
5 topics ~2 h 17 code blocks & diagrams
Start with the first topic
1
6.1

Normal Flow and Writing Modes

With no layout CSS, the browser places boxes in normal flow: block boxes stack one under another and fill the width, while inline content runs along lines and wraps; writing modes and logical properties decide which way "down" and "along" really point.

29 min 1 diagram 2 code practice

2
6.2

Positioning

The position property decides how a box is placed: static follows normal flow, relative shifts a box but keeps its space, absolute and fixed take it out of flow and place it against a containing block, and sticky lets it scroll until it hits an edge and then stay there.

30 min 4 code practice

3
6.3

z-index and Stacking Contexts

When boxes overlap, the browser paints them in a fixed order; z-index changes that order for positioned boxes, but only among siblings in the same stacking context, which is why a child with z-index: 9999 can still be stuck under another element.

28 min 1 diagram 3 code practice

4
6.4

Floats and Clearing

float pushes a box to the left or right edge of its container and lets text and inline content wrap around it; floats leave normal flow, so parents collapse unless you contain them with display: flow-root, and clear moves a box below earlier floats.

27 min 3 code practice

5
6.5

Centering Anything

Pick the centering method by what you're centering: text-align: center for text and inline content, margin-inline: auto for a block with a width, place-items: center (Grid) or justify-content plus align-items (Flexbox) for both directions, and inset: 0; margin: auto or translate(-50%, -50%) for positioned boxes.

27 min 3 code practice

Practise this phase

Hands-on exercises: write the code, see your page live, and press Check to have it tested automatically.