
"How do I center a div?" is one of the most searched questions in web development, and for years it deserved its reputation. Centering horizontally was easy, but centering vertically required tricks that broke as soon as the content changed. In 2026 the problem is solved: every current browser supports several clean, reliable methods. The real skill is knowing which one to use and why the others sometimes seem not to work.
In the early web, developers centered content with HTML tables and the old <center> tag. CSS brought margin: 0 auto for horizontal centering, but vertical centering stayed awkward. Popular hacks included display: table-cell with vertical-align: middle, negative margins that only worked with a known height, and later position: absolute combined with transform: translate(-50%, -50%). Flexbox, widely supported from around 2015, and CSS Grid, supported in all major browsers since 2017, finally made centering a single, predictable instruction.
Most frustration comes from an unclear goal. Before writing CSS, answer three questions:
For text and inline content such as links, images and inline-block buttons, use text-align: center on the parent. It centers the lines of content, not the block itself.
For a block element such as a div, give it a width and set its horizontal margins to auto:
.card { max-width: 40rem; margin-inline: auto; }
The browser splits the leftover space equally on both sides. margin-inline is the modern logical property; margin: 0 auto does the same in left-to-right layouts. Without a width smaller than the parent, a block already fills the full width, so there is nothing to center.
To center a child both horizontally and vertically, make the parent a flex container:
.parent { display: flex; justify-content: center; align-items: center; min-height: 100vh; }
If you change flex-direction to column, the axes swap, so justify-content now controls vertical position.
CSS Grid does the same job in two declarations:
.parent { display: grid; place-items: center; }
place-items is shorthand for align-items and justify-items. To center only one child while its siblings behave differently, put place-self: center on that child. Grid is ideal for a single centered element, such as a login form.
Since 2024, all major browsers let you center content vertically inside a normal block element without Flexbox or Grid:
.box { align-content: center; min-height: 12rem; }
This keeps the element in regular block layout, so margins and text flow behave as usual. Combine it with text-align: center to center the text horizontally too.
When an element must float above the page, such as a dialog or a loading spinner, use positioning:
.modal { position: fixed; inset: 0; margin: auto; width: min(90vw, 30rem); height: fit-content; }
inset: 0 stretches the element to all four edges, and margin: auto then centers it in both directions, as long as it has a defined width and height. The older top: 50%; left: 50%; transform: translate(-50%, -50%) pattern still works, but it can blur text at fractional pixel positions. For real dialogs, the native <dialog> element opened with showModal() is centered by the browser and handles keyboard focus for you.
Open your browser's developer tools to check your work: Chrome, Firefox and Safari show flex and grid overlays that reveal the container's real size and the free space around each item. Once you see that space, centering stops being a mystery and becomes a simple, deliberate choice.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions