Question
I have a div element with a width of 800px. When the browser window becomes 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 for block elements in CSS, especially fixed-width div containers. You will learn the common margin: 0 auto; technique, why it works, when it does not work, and how it is used in real layouts.
Concept
In CSS, a div is a block-level element by default. That means it normally takes up the full available width of its parent unless you give it a specific width.
If you set a block element to a fixed width such as 800px, it no longer fills the entire row. That leaves extra horizontal space on the left and right when the browser is wider than 800px.
To center that block horizontally, the usual approach is:
margin-left: auto;
margin-right: auto;
This is commonly written as:
margin: 0 auto;
Here is why it works:
autoon the left and right margins tells the browser to distribute the remaining horizontal space.- If both sides are
auto, the browser splits the space evenly. - The result is a centered block element.
This matters because centered containers are used everywhere in web design:
- page wrappers
- blog content areas
- dashboards
- forms
- documentation layouts
A very common pattern is a centered container with either:
- a fixed width like
800px, or
Mental Model
Think of the page as a shelf and your div as a box that is 800px wide.
If the shelf is wider than the box, there is empty space on both sides. Setting margin-left: auto and margin-right: auto is like telling the browser:
"Take the leftover space and split it evenly on both sides of the box."
That makes the box sit exactly in the middle.
If the box has no defined width, then it already fills the whole shelf, so there is no leftover space to split. In that case, centering has no visible effect.
Syntax and Examples
The classic solution is:
.container {
width: 800px;
margin: 0 auto;
}
Example HTML:
<div class="container">
Content goes here
</div>
Full example:
<!DOCTYPE html>
<html>
<head>
<style>
.container {
width: 800px;
margin: 0 auto;
background: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<div class="container">
This div stays 800px wide and is centered horizontally.
</>
Step by Step Execution
Consider this code:
<div class="container">Hello</div>
.container {
width: 800px;
margin: 0 auto;
}
Now imagine the browser window is 1200px wide.
Step-by-step
- The browser sees a block element:
.container. - Its width is set to
800px. - The parent area is
1200pxwide. - Remaining horizontal space is:
1200px - 800px = 400px
- Left and right margins are both
auto. - The browser splits the remaining
400pxequally:
left margin = 200px
right margin = 200px
- The element appears centered.
What if the browser is exactly 800px wide?
Real World Use Cases
Horizontal centering of containers is used in many common layouts:
- Website wrappers: keeping the main content centered on large screens
- Blog articles: limiting line length for readability
- Forms: centering login, signup, or checkout sections
- Dashboards: placing a main content column in the middle
- Documentation pages: centering readable content blocks
- Landing pages: keeping hero text and cards aligned inside a centered container
Example:
.page {
max-width: 960px;
margin: 0 auto;
}
This creates a central layout area while leaving whitespace on both sides on larger screens.
Real Codebase Usage
In real projects, developers often center a main layout container rather than centering each element individually.
Common patterns include:
1. App wrapper
.app {
max-width: 1200px;
margin: 0 auto;
}
Used for the main site shell.
2. Content container with padding
.content {
max-width: 800px;
margin: 0 auto;
padding: 0 20px;
}
This avoids content touching the edges on small screens.
3. Utility class
Some codebases create reusable helpers:
.centered {
margin-left: auto;
margin-right: auto;
}
Then combine them with width classes.
4. Responsive layout pattern
.container {
width: 100%;
max-width: ;
: auto;
}
Common Mistakes
Here are common beginner mistakes when trying to center a div.
1. Forgetting to set a width or max-width
Broken example:
.box {
margin: 0 auto;
}
Why it fails:
- A block
divnormally takes the full width already. - If it is already full width, there is no extra horizontal space to distribute.
Fix:
.box {
width: 800px;
margin: 0 auto;
}
2. Using text-align: center on the div itself
Broken idea:
.box {
width: 800px;
text-align: center;
}
Why it fails:
text-align: centercenters inline content inside the element.- It does not center the block element itself.
3. Applying auto margins to inline elements
Comparisons
Here are the most useful ways to compare horizontal centering techniques.
| Technique | What it centers | Good for | Notes |
|---|---|---|---|
margin: 0 auto; | Block element itself | Fixed-width or max-width containers | Classic and simple |
text-align: center; | Inline or inline-block content inside a parent | Text, buttons, inline elements | Does not center a block container by itself |
display: flex; justify-content: center; | Child elements inside a flex container | Modern layouts | Useful when centering one or more children |
display: grid; justify-content: center; | Grid items | Grid-based layouts | Less common for simple page wrappers |
Cheat Sheet
/* Classic horizontal centering for a block element */
.box {
width: 800px;
margin: 0 auto;
}
/* Responsive centered container */
.box {
width: 100%;
max-width: 800px;
margin: 0 auto;
}
Rules
- Works for block-level elements
- The element usually needs a
widthormax-width autoleft and right margins split extra horizontal space evenlytext-align: centercenters text or inline content, not the block itself
Quick reminders
margin: 0 auto;= top/bottom0, left/rightauto- Use
max-widthfor better responsiveness - Add padding if you want space from screen edges
Common pattern
FAQ
How do I center a div horizontally in CSS?
Use a width or max-width and set left and right margins to auto:
div {
width: 800px;
margin: 0 auto;
}
Why is margin: 0 auto; not working?
Usually because the element has no width or max-width, so it already fills the available space.
Does text-align: center; center a div?
No. It centers inline content inside a parent element, such as text or inline-block items.
Should I use width or max-width?
For most modern layouts, max-width is better because it is more responsive on small screens.
Can I center any element with auto margins?
It works best for block elements. Inline elements usually need display: block or a different centering method.
What happens on screens smaller than 800px?
With width: 800px, the element may overflow. Using width: 100%; max-width: 800px; avoids that problem.
Mini Project
Description
Build a simple centered content page with a main container that stays readable on large screens and adapts on smaller screens. This demonstrates the most common real-world use of horizontal centering in CSS: a page wrapper or content container.
Goal
Create a page where the main content is horizontally centered and never grows wider than 800px.
Requirements
- Create an HTML page with a main content container.
- Center the container horizontally using CSS.
- Prevent the container from becoming wider than
800px. - Add padding and a background color so the container is easy to see.
- Make the layout work on both large and small screens.
Keep learning
Related questions
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
CSS :not() Selector for Excluding 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 mistakes.
Can HTML Checkboxes Be Readonly? Understanding readonly vs disabled in HTML Forms
Learn why HTML checkboxes do not support readonly, how disabled differs, and practical ways to prevent changes while still submitting values.