Question
I have a div element with a width of 800px. When the browser window is wider than 800px, I do not want the div to stretch. Instead, I want it to stay 800px wide and appear centered horizontally on the page.
What is the correct way to do this in CSS?
Short Answer
By the end of this page, you will understand how horizontal centering works in CSS for block elements, why margin: 0 auto; is the usual solution, and when newer layout tools like Flexbox are also useful.
Concept
In CSS, a block-level element such as a div normally takes up the full available width of its parent unless you give it a specific width.
If you set a fixed width like 800px, the element becomes narrower than the page on larger screens. At that point, CSS can distribute the remaining horizontal space around the element.
The most common way to center that element horizontally is:
.container {
width: 800px;
margin: 0 auto;
}
Here is what this means:
width: 800px;gives thediva fixed width.margin: 0 auto;means:- top and bottom margin =
0 - left and right margin =
auto
- top and bottom margin =
When left and right margins are both auto, the browser splits the extra horizontal space evenly on both sides. That places the element in the center.
This matters because centering content is one of the most common layout tasks in web development. You use it for:
- page wrappers
- article content areas
- login forms
- cards and panels
- fixed-width sections inside responsive pages
A key detail: this technique works for block elements that have a width smaller than their parent. If the element is still , there is no extra space to distribute, so centering will not have any visible effect.
Mental Model
Imagine a shelf that is much wider than a box.
- The box is your
div. - The shelf is the browser window.
- The box has a fixed width of
800px.
If the shelf is wider than the box, there is empty space on both sides. Setting the left and right margins to auto tells the browser:
"Take the extra space and divide it equally on the left and right sides."
That is what makes the box sit in the middle.
If the box were already as wide as the shelf, there would be no leftover space to divide, so centering would not change anything.
Syntax and Examples
The classic syntax is:
selector {
width: 800px;
margin-left: auto;
margin-right: auto;
}
This is often shortened to:
selector {
width: 800px;
margin: 0 auto;
}
Basic example
<div class="wrapper">
This content stays 800px wide and is centered.
</div>
.wrapper {
width: 800px;
margin: 0 auto;
background-color: #f2f2f2;
padding: 20px;
}
Explanation:
- The
divis limited to800pxwide. - If the page is wider than
800px, the extra space is split between the left and right sides.
Step by Step Execution
Consider this code:
<div class="box">Hello</div>
.box {
width: 800px;
margin: 0 auto;
}
Suppose the browser window is 1200px wide.
Step by step
- The browser creates the
divas a block element. - CSS applies
width: 800px, so the box becomes exactly800pxwide. - The parent container is
1200pxwide. - That leaves
400pxof extra horizontal space. - CSS sees
margin-left: autoandmargin-right: auto. - The browser divides the extra
400pxequally:200pxon the left200pxon the right
Real World Use Cases
Horizontal centering is used constantly in real websites and applications.
Common examples
- Page containers: keeping article or app content centered instead of stretched across huge screens
- Forms: centering a login or signup panel
- Cards and modals: keeping components visually balanced
- Documentation pages: limiting line length for readability
- Admin dashboards: wrapping main content in a centered layout container
Example: centered page layout
.page {
max-width: 1200px;
margin: 0 auto;
}
Example: centered login panel
.login-box {
width: 360px;
margin: 40px auto;
}
In both cases, the idea is the same: give the element a controlled width and let automatic horizontal margins place it in the middle.
Real Codebase Usage
In real projects, developers usually center layout wrappers rather than random individual elements.
Common pattern: page container
.container {
max-width: 800px;
margin: 0 auto;
padding: 0 16px;
}
Why this pattern is common:
max-widthprevents the content from becoming too widemargin: 0 autocenters it- horizontal
paddingstops content from touching screen edges on small devices
Common usage in design systems
Many codebases define reusable layout classes such as:
.container-sm { max-width: 640px; margin: 0 auto; }
.container-md { max-width: 800px; margin: 0 auto; }
.container-lg { max-width: 1200px; margin: 0 auto; }
Guarding against small-screen issues
Common Mistakes
1. Forgetting to set a width or max-width
Broken example:
.box {
margin: 0 auto;
}
Why it fails:
- A block
divusually stretches to fill the available width. - If it is already full width, there is nothing to center.
Fix:
.box {
width: 800px;
margin: 0 auto;
}
2. Using text-align: center to center a block element
Broken example:
.box {
width: 800px;
text-align: center;
}
Why it fails:
text-align: centercenters inline content inside the element.- It does not center the block itself.
3. Using fixed width without considering small screens
Problematic example:
{
: ;
: auto;
}
Comparisons
| Technique | Best for | How it works | Notes |
|---|---|---|---|
margin: 0 auto | Centering a block with known width | Splits leftover horizontal space evenly | Simple and classic |
text-align: center | Centering text or inline elements inside a container | Aligns inline content | Does not center the block itself |
display: flex; justify-content: center; | Centering child elements inside a parent | Parent controls child alignment | Great for modern layouts |
position + transform | Special layout cases | Moves element manually | More complex than needed here |
vs
Cheat Sheet
/* Classic horizontal centering */
.box {
width: 800px;
margin: 0 auto;
}
/* Responsive centered container */
.box {
width: 100%;
max-width: 800px;
margin: 0 auto;
}
Rules to remember
margin: 0 auto;centers a block element horizontally.- The element needs a width smaller than its parent.
text-align: center;centers text/inline content, not the block itself.max-widthis usually better than fixedwidthfor responsive pages.- Add horizontal padding if content gets too close to screen edges.
Quick patterns
.container {
max-width: 800px;
margin: 0 auto;
}
{
: ;
: ;
: auto;
: ;
}
FAQ
How do I center a div horizontally in CSS?
Give it a width or max-width, then use:
margin: 0 auto;
Why is margin: auto not working on my div?
Usually because the div is still full width. Set a width or max-width first.
Should I use width or max-width to center a container?
Use max-width in most modern layouts because it works better on small screens.
Does text-align: center center a div?
No. It centers inline content inside the div, not the div itself.
Can Flexbox center a div horizontally?
Yes. If the is inside a flex parent, you can use:
Mini Project
Description
Build a simple centered content container for a webpage. This project demonstrates how to keep content readable on large screens while still working well on smaller screens.
Goal
Create a centered content panel that stays visually balanced and does not stretch too wide.
Requirements
- Create an HTML page with one main content
div - Center the
divhorizontally on the page - Limit the content area to
800pxwide - Make the layout safer for smaller screens
- Add simple visual styling so the centering is easy to see
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.