CSS Reference ยท Practical Pages
Centering cheat sheet
Every practical way to center in CSS: text, a block, and an element both horizontally and vertically with flexbox, grid, margins and positioning.
"How do I center this?" has a different answer depending on what you center and in what. This cheat sheet groups the techniques by situation. In modern CSS, the shortest answer for centering one thing in both directions is display: grid; place-items: center; on the parent.
.parent { display: grid; place-items: center; }
Try it
Six parents of the same size, each using a different technique. Every pink square must end up in the middle of its dashed box.
Horizontal only
| What you center | Technique |
|---|---|
| Inline content (text, links, inline images) | text-align: center on the parent |
| A block with a set width | margin-inline: auto (needs a width or max-width) |
| Flex items | justify-content: center on the flex container |
| A block that shrinks to its content | width: fit-content; margin-inline: auto |
.page { max-width: 60rem; margin-inline: auto; } .label { width: fit-content; margin-inline: auto; }
Vertical only
- Single line of text in a box of known height: set
line-heightequal to the height. It breaks as soon as the text wraps. - Anything, in a flex container:
align-items: center(the container needs a height to center within). - Grid:
align-content: centerfor the whole grid, oralign-items: centerfor items in their cells. - Table-cell trick (legacy):
display: table-cell; vertical-align: middle. Use flexbox or grid instead.
Both directions
- Grid +
place-items: centeris the shortest. For several children useplace-content: centerso the whole group is centered. - Flexbox with
justify-content: center; align-items: centeris equally reliable and familiar. - Absolute positioning is for overlays and badges: either
top: 50%; left: 50%; translate: -50% -50%, orinset: 0; margin: autowith a fixed size. - A single flex or grid child can center itself with
margin: auto, no properties on the parent other than the display type.
Usage notes
- The parent needs space. Vertical centering does nothing if the parent is only as tall as its content. Give it a
height,min-height(for example100dvhfor a full-screen hero) or let a layout stretch it. margin: autodoes not center vertically in normal flow. Auto vertical margins compute to 0 in block layout; they work in flex, grid and absolutely positioned boxes.- Overflow in flex:
justify-content: centercan push content off the start edge when it is bigger than the container, where it becomes unreachable by scrolling. Usesafe centeror a margin approach if that matters. - Subpixel blur:
translate: -50% -50%can land on half pixels and blur text on some displays; grid or flex avoid that. - Centering text inside a centered box is a separate step: add
text-align: centeras well.
Browser support
Flexbox, grid, margin: auto and text-align are supported everywhere; the individual translate property and the safe alignment keyword are newer, so check a compatibility table if you must support old browsers.
Related
place-itemsandplace-contentjustify-contentandalign-itemsmargin-inlineandtranslateinset- Lesson: Centering in CSS