Published at

safe in flex and grid alignment

Box alignment lets you write values like safe center instead of plain center. Here is the short version of what that buys you.

CSS snippet highlighting the safe keyword in align-items: safe center

What does safe mean?

Alignment keywords such as center or end can center or push content in a way that overflows the scrollable area on both sides. In the worst case you cannot scroll far enough to see where the content starts.

The safe keyword tells the browser: use the requested alignment only if it does not cause that kind of “lost” overflow. If it would, fall back to start alignment (the safe edge for reading order) so the content remains fully reachable by scrolling.

Syntax: put safe before the alignment keyword, for example align-items: safe center; or justify-content: safe center;. The opposite is unsafe, which keeps the chosen alignment even when overflow is awkward.

Flexbox vs grid

The same idea applies to both flex and grid wherever the Box Alignment properties apply: for example justify-content, align-items, justify-items, place-content, and the *-self variants on individual items.

You will notice it most in scrollable regions (overflow) or when the viewport is narrower than the aligned group. It is a small addition to your CSS that prevents subtle scroll traps for centered or end-aligned layouts.

Each inner strip is horizontally scrollable so you can inspect how the aligned items overflow. Each row shows flex and grid side by side with the same values.

Flex and grid

same box-alignment rules

The alignment container itself is narrower than its items, and each strip is horizontally scrollable so you can inspect overflow behavior. Row 1center (unsafe center behavior) ; row 2safe center; row 3 — start (flex-start / start).

Needs ~296px to fit all items (scroll if smaller)

1 · justify-content: center

Flex

A
B
C

Grid

A
B
C

Leading gap before “A” at the green line when the window is narrower than the inner track.

2 · justify-content: safe center

Flex

A
B
C

Grid

A
B
C

Should match row 3 when the safe fallback applies.

3 · start (reference)

Flex

A
B
C

Grid

A
B
C

Explicit start—flush to the green marker (typical safe fallback).

Green line = left edge of each clip. Flex uses blue blocks, grid violet—layout rules are the same.