The ultimate centering decision tree
Interactive guide to choosing the right centering technique — flexbox, grid, absolute positioning, or container queries.
What are you centering?
Use Flexbox
.parent { display: flex; justify-content: center; align-items: center; }
Use Grid
.parent { display: grid; place-items: center; }
Use Absolute + Transform
.child { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
Use Flexbox with gap
.parent { display: flex; gap: 1rem; justify-content: center; flex-wrap: wrap; }
Quick implementation
/* Flexbox center — single element or collection */
.flex-center {
display: flex;
justify-content: center;
align-items: center;
gap: 1rem; /* optional spacing */
}
/* Grid center — simplest syntax */
.grid-center {
display: grid;
place-items: center;
}
/* Absolute center — overlays, modals */
.absolute-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Container-query aware center */
@container (min-width: 40rem) {
.responsive-center {
display: grid;
place-items: center;
}
}
@media (max-width: 40rem) {
.responsive-center {
display: flex;
justify-content: center;
}
}
Prompt this to your LLM
Includes role, constraints, decision tree logic, and edge cases.
You are a senior frontend engineer specializing in CSS layout.
Goal: Build a decision-tree guide that helps developers choose the right centering technique.
Technical constraints:
- Use semantic HTML with radio inputs or buttons for navigation.
- Structure the decision tree with 2-3 levels of questions.
- Each path leads to one technique: flexbox, grid, absolute positioning, or container queries.
- Use oklch() for all colors — no hex or rgba().
- Respect prefers-reduced-motion for any transitions.
- Make it keyboard accessible (focus-visible states, arrow key support if using radios).
Decision tree logic:
1. First question: "What are you centering?" → Single element OR Multiple items
2. If single element: ask "Container type?" → Regular parent, Grid container, or Full-viewport overlay
3. If multiple items: ask "Layout direction?" → Row or Grid/2D
4. Each path returns the appropriate CSS snippet.
Edge cases to handle:
- What if the parent has no height defined? (Remedy: set min-height or explicit height)
- What if container queries aren't supported? (Remedy: media query fallback)
- Screen reader experience: announce current path or selected option.
Return HTML + CSS with clear comments explaining each branch.
Why this matters in 2026
Centering used to be CSS hell — negative margins, table-cell hacks, transform: translate gymnastics. Now we have flexbox, grid, and container queries, but that abundance creates a new problem: which technique do you pick? This decision tree codifies the mental model: single element vs. collection, parent type, viewport context. It's documentation you can interact with.
The logic
The tree branches on three key decisions. Single vs. multiple determines if you need justify-content (flexbox) or place-items (grid). Container type determines if you already have a grid or need to create one. Viewport context determines if absolute positioning makes sense (modals, overlays) or if you should use relative centering. Each path maps to a minimal CSS pattern.
Accessibility & performance
The interactive demo uses buttons with proper focus states — screen readers can navigate the tree linearly. For keyboard users, focus-visible styles ensure the current selection is clear. Performance-wise, there's no animation, just show/hide with display: none. No layout thrashing, no repaints beyond the initial interaction.
aria-current to the active question and role="list" on option groups for better screen reader navigation.