How to align a <div> to the middle (horizontally/width) of the page
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To center a block-level div horizontally, the classic CSS answer is to give it a width and use margin-left and margin-right as auto, usually written as margin: 0 auto. Modern layouts also use flexbox or grid, but the right method depends on whether you are centering the element itself or centering content inside a container.
Center the div Itself with Auto Margins
If you want the block element itself centered within its parent, the traditional solution is:
This works because the browser calculates equal left and right margins once the element has a width smaller than the available space.
Without a constrained width or max-width, the element may already fill the available width, in which case there is nothing visually left to center.
Use max-width for Responsive Layouts
In real pages, a fixed percentage is not always the best choice. A common responsive pattern is:
This lets the block stay fluid on small screens while remaining centered and readable on larger screens.
Use Flexbox When the Parent Controls Layout
If you are centering a child inside a layout container, flexbox is often clearer than relying on auto margins alone.
This is especially useful when the parent already uses flex layout for spacing, alignment, or responsive direction changes.
Grid Can Do It Too
Grid is another modern option and can be very concise.
If you want both horizontal and vertical centering, grid can be even more compact:
That centers child items on both axes, which is slightly different from the original "horizontal only" requirement but often useful in the same layouts.
text-align: center Does Something Else
A common source of confusion is text-align: center. That centers inline content inside a block, not the block element itself.
This centers the text or inline elements, but it does not center a block-level div sibling on the page.
Which Method Should You Choose?
A practical rule is:
- use
margin: 0 autowhen you are centering one block element with a known width ormax-width - use flexbox when the parent already manages layout
- use grid when you want concise two-axis alignment or a grid-based page structure
Most "my div will not center" bugs come from choosing the right idea but applying it to the wrong level of the layout.
Common Pitfalls
The most common mistake is using margin: 0 auto without giving the element a width or max-width, so the element already spans the container and appears not to move. Another is using text-align: center and expecting it to center a block element. Developers also apply flexbox centering to the child instead of the parent, even though the layout behavior belongs on the container that controls alignment.
Summary
- Use
margin: 0 autoto center a block-leveldivwith constrained width. - Prefer
max-widthpluswidth: 100%for responsive centered blocks. - Use flexbox or grid when centering is part of a parent layout system.
- Remember that
text-align: centercenters inline content, not block elements themselves. - Choose the centering technique based on whether you are centering the element or its contents.

