Key points
- For a single element,
display: gridplusplace-items: centeron the parent handles most cases. - Flexbox for a group of items,
margin-inline: autofor a width-limited block,inset: 0withmargin: autofor a modal. - When vertical centring fails, the parent almost always has no height: that’s the first thing to check.
Centring an element is probably the most asked CSS question there is, and it still turns up in our code reviews all the time. Not for lack of solutions: there are too many, and each one only works in a specific context. A margin: 0 auto that does nothing, an align-items: center with no effect, centred content that gets cut off at the top on mobile… Most of the time, the problem isn’t the property you picked but the context: the parent’s display type, its height, or which element the rule is applied to. Here are the techniques we use day to day, when to reach for each, and how to diagnose centring that refuses to cooperate.
The default: Grid and place-items
To centre a single element in both directions, this is now the shortest and most readable option:
/* A single child, centred on both axes */
.area {
display: grid;
place-items: center; /* align-items + justify-items */
min-height: 100dvh; /* or whatever height you need */
}
/* Handy variant: the child centres itself */
.area { display: grid; }
.area > .card { margin: auto; }
place-items is shorthand for align-items (vertical) and justify-items (horizontal). It works whatever the child’s size, without knowing it and without any maths. The variant with margin: auto on the child is handy when several children need different placement: each one handles its own alignment.
Note the use of min-height rather than height: if the content grows (a longer translation, browser zoom, a small screen), the container grows with it instead of letting the content spill out. And 100dvh rather than 100vh accounts for the mobile address bar appearing and disappearing.
Flexbox to centre a group of items
A heading, a paragraph and a button in the middle of a hero section: the textbook Flexbox case. The whole group is centred and gap handles spacing between items:
/* Several stacked items, the group is centred */
.hero {
display: flex;
flex-direction: column;
justify-content: center; /* main axis (vertical here) */
align-items: center; /* cross axis (horizontal here) */
gap: 1rem;
min-height: 70vh;
text-align: center;
}
The classic Flexbox trap is mixing up the axes. justify-content always acts on the main axis and align-items on the cross axis. With flex-direction: row (the default) the main axis is horizontal; with column it becomes vertical. If horizontal centring works but vertical doesn’t after switching to a column, that’s almost certainly why: swap the two properties, or simply set both to center.
Horizontal only: margin auto and text-align
For a reading column, a form or a card, horizontal centring is often all you need, and the classic techniques are still the right ones:
/* A block with a limited width */
.content {
max-width: 70ch;
margin-inline: auto; /* same as margin: 0 auto */
}
/* Text or inline elements (links, inline-block buttons) */
.footer { text-align: center; }
/* An image is inline by default, so margin auto does nothing */
img.centred { display: block; margin-inline: auto; }
margin-inline: auto works under two conditions only: the element is a block, and it’s narrower than its parent (via width or max-width). Without a width, a block already takes up all the available space: there’s nothing to share out, so nothing to centre.
text-align: center, on the other hand, centres a block’s inline content: text, links, images, inline-block elements. It doesn’t move a child block. That’s the most common mix-up we see with juniors: text-align: center on a div containing a fixed-width div, and the child stubbornly staying left. Images are the same problem in reverse: they’re inline by default, so margin: auto does nothing until you set display: block.
Overlays: modals, badges and positioned elements
For an element taken out of the flow (a modal, a badge, an icon over an image), positioning is still the right tool. Two techniques cover every case:
/* A modal centred on screen */
.modal {
position: fixed;
inset: 0; /* top, right, bottom, left all 0 */
margin: auto; /* centres because all four sides are set */
width: min(90vw, 32rem);
height: fit-content;
max-height: 90dvh;
overflow: auto;
}
/* A badge centred on a positioned parent, unknown size */
.parent { position: relative; }
.badge {
position: absolute;
top: 50%;
left: 50%;
translate: -50% -50%; /* or transform: translate(-50%, -50%) */
}
The first pins all four sides to zero and lets margin: auto distribute the remaining space. It needs a defined size (a width and a height, here fit-content with a cap), but it has a valuable advantage: with max-height and overflow: auto, a modal with long content stays fully reachable and scrolls internally rather than running off the screen.
The second, with top: 50%, left: 50% and a -50% translation, works without knowing the element’s size. Its downside: text can look slightly blurry in some browsers when the translation lands on a half pixel. For modals, we prefer the HTML <dialog> element, which is centred by default and also handles focus and the Escape key.
It won’t centre: what to check, in order
When centring refuses to work, we always go through these checks in this order:
- The parent has no height. A block is as tall as its content: vertically centring a child in a parent that’s exactly its size changes nothing.
height: 100%only works if every ancestor has a defined height too; an explicitmin-heightis more reliable. - The rule is on the wrong element.
place-items,justify-contentandalign-itemsgo on the parent;margin: autoandjustify-selfon the child. Swapping them is the most ordinary mistake of all. - An extra wrapper has crept in. A WordPress plugin, a page builder or a component adds a wrapping
div: flex then applies to that wrapper, not the element you meant. The browser inspector shows it immediately. - The content is bigger than the container. An element taller than its centred parent overflows on both sides, including the top, where you can’t scroll to it. It happens a lot on phones in landscape.
/* 1. The parent has no height: nothing to centre against vertically */
.zone { display: grid; place-items: center; min-height: 100dvh; }
/* 2. Content taller than the container overflows at the top and becomes
unreachable. “safe” falls back to start alignment when needed */
.zone { display: flex; align-items: safe center; justify-content: safe center; }
/* To see the boxes while debugging */
* { outline: 1px solid rgb(255 0 0 / .3); }
The safe keyword fixes the overflow case: the element stays centred while it fits, then aligns to the top when it’s too big. Recent browsers support it; if your project has to handle older ones, the margin: auto-on-the-child variant gives the same behaviour.
FAQ
Should I use Grid or Flexbox to centre?
For a single element, Grid with place-items is the shortest. For a group of items aligned together, or a row of buttons, Flexbox is the better fit. Both are supported by every current browser.
Why doesn’t margin: 0 auto centre my element?
Either the element isn’t narrower than its parent (it already fills the space), or it’s inline (like an image or a span) rather than a block. Add a max-width or switch it to display: block.
How do I vertically centre a single line of text?
The old trick of setting line-height equal to the height works, but it breaks as soon as the text wraps onto two lines. A grid or flex container with align-items: center is more robust.
Does your site break on certain screens, or is your front-end piling up CSS patches? We take over and stabilise front-end builds for sites and applications, with CSS that’s readable and easy to maintain.



