Question
How can I horizontally center a <div> inside another <div> using CSS?
<div id="outer">
<div id="inner">Foo foo</div>
</div>
I want the inner element to appear centered from left to right within the outer element.
Short Answer
By the end of this page, you will understand the main ways to horizontally center an element in CSS, especially a <div> inside another <div>. You will learn the classic margin: 0 auto technique, when it works, and modern alternatives such as Flexbox and Grid.
Concept
In CSS, horizontal centering means placing an element so that the empty space on its left and right is equal within its parent container.
For block-level elements like <div>, the most common traditional method is:
margin-left: auto;
margin-right: auto;
This is often written as:
margin: 0 auto;
However, this only works when the element being centered has a defined width that is smaller than its parent.
Why? Because a normal block element usually takes up the full available width. If it is already 100% wide, there is no leftover horizontal space to distribute, so auto margins cannot visibly center it.
Modern CSS also provides layout systems like Flexbox and Grid, which make centering easier and more explicit.
This matters in real programming because centering is a very common layout task. You use it for:
- cards inside containers
- forms on a page
- modal content
- navigation sections
- buttons or callout boxes
Understanding why a centering method works is more useful than memorizing a single snippet.
Mental Model
Think of the parent container as a shelf and the child element as a box placed on that shelf.
- If the box has a known width, CSS can put equal empty space on both sides of it.
margin: 0 autotells the browser: "Take the extra horizontal space and split it evenly on the left and right."- If the box already stretches across the whole shelf, there is no extra space left to split, so centering does nothing.
With Flexbox, the shelf itself becomes smart: you can tell it to place its items in the center automatically.
With Grid, you can also tell the container to align its content in the center.
So the key question is: Are you centering the element by its own margins, or by the parent layout system?
Syntax and Examples
1. Classic method: margin: 0 auto
This is the traditional way to horizontally center a block element.
<div id="outer">
<div id="inner">Foo foo</div>
</div>
#outer {
width: 400px;
background: #eee;
}
#inner {
width: 200px;
margin: 0 auto;
background: #cce5ff;
}
Why it works
#innerhas a width of200px#outeris400pxwide- The extra space is shared equally on both sides
2. Flexbox method
Step by Step Execution
Consider this example:
<div id="outer">
<div id="inner">Foo foo</div>
</div>
#outer {
width: 500px;
background: lightgray;
}
#inner {
width: 200px;
margin: 0 auto;
background: steelblue;
color: white;
}
Step by step
- The browser creates the outer
<div>. #outergets a width of500px.- The browser creates the inner
<div>inside it. #innergets a width of200px.- That leaves
300pxof extra horizontal space in the parent. - means:
Real World Use Cases
Horizontal centering appears in many common layouts:
- Login forms: center a form card inside the page or a section
- Content boxes: center an article preview card in a layout
- Buttons or banners: place a fixed-width call-to-action box in the middle
- Dialogs and modals: center modal content within an overlay
- Dashboard panels: keep widgets visually balanced in a container
Example: centering a form card
.form-wrapper {
width: 320px;
margin: 0 auto;
}
Example: centering navigation with Flexbox
.navbar {
display: flex;
justify-content: center;
}
Real Codebase Usage
In real projects, developers choose a centering method based on the layout system already in use.
Common patterns
1. margin: 0 auto for fixed-width content areas
Often used for:
- page containers
- article bodies
- form cards
- documentation layouts
.container {
max-width: 960px;
margin: 0 auto;
}
This is extremely common in real codebases.
2. Flexbox for component layouts
Used when the parent already controls alignment of multiple items.
.header {
display: flex;
justify-content: center;
}
3. Grid for structured layout systems
Useful when a layout already uses CSS Grid.
.wrapper {
display: grid;
justify-content: center;
}
4. Guarding against oversized elements
Developers often combine centering with responsive sizing:
{
: ;
: ;
: auto;
}
Common Mistakes
1. Forgetting to set a width
Broken example:
#inner {
margin: 0 auto;
}
Why it fails:
- A block
<div>usually takes full width - No remaining horizontal space exists to center it
Fix:
#inner {
width: 200px;
margin: 0 auto;
}
2. Using text-align: center to center a block element
Broken example:
#outer {
text-align: center;
}
Why it fails:
- This centers inline content, not normal block-level
<div>elements
Fix:
- Use
margin: 0 auto, Flexbox, or Grid for the block element
3. Applying Flexbox to the wrong element
Broken example:
Comparisons
| Method | Best for | Works on block elements? | Needs width on child? | Notes |
|---|---|---|---|---|
margin: 0 auto | Simple single element centering | Yes | Usually yes | Classic and very common |
display: flex; justify-content: center; | Modern layouts and multiple children | Yes | No, not always | Great for alignment control |
display: grid; justify-content: center; | Grid-based layouts | Yes | No, not always | Useful when Grid is already used |
text-align: center | Text and inline elements | No for normal block divs |
Cheat Sheet
Horizontal centering quick reference
Center a block element with known width
.child {
width: 200px;
margin: 0 auto;
}
Center with Flexbox
.parent {
display: flex;
justify-content: center;
}
Center with Grid
.parent {
display: grid;
justify-content: center;
}
Center text or inline elements
.parent {
text-align: center;
}
Rules to remember
margin: 0 autoworks for block elements with available extra width- A block element without a set width often fills the container, so centering is not visible
text-align: centercenters inline content, not normal block boxes- Flexbox and Grid center through the parent container
Responsive pattern
FAQ
How do I center a div horizontally in CSS?
Use either margin: 0 auto on the child with a defined width, or use Flexbox on the parent with justify-content: center.
Why does margin: 0 auto not work on my div?
Usually because the element has no width set and expands to full width, leaving no extra horizontal space to split.
Can I center a div without giving it a fixed width?
Yes. Flexbox and Grid can center elements without requiring a fixed width in many cases.
Does text-align: center center a div?
Not a normal block <div>. It centers text and inline content inside a container.
What is the modern way to center elements in CSS?
Flexbox is one of the most common modern solutions because it is clear, flexible, and widely supported.
Should I use Flexbox or margin: auto?
Use margin: auto for simple single-element centering. Use Flexbox when the parent layout already manages alignment or contains multiple items.
Can I use max-width instead of width?
Yes. That is often better for responsive design:
Mini Project
Description
Build a simple centered profile card inside a page section. This project demonstrates the most common real-world use of horizontal centering: placing a fixed-width or max-width content box in the middle of its parent container.
Goal
Create a profile card that stays horizontally centered inside a larger container using CSS.
Requirements
- Create an outer container with a visible background.
- Add an inner card element with text content.
- Center the card horizontally inside the outer container.
- Make the card responsive by using
max-width. - Add basic padding and colors so the layout is easy to see.
Keep learning
Related questions
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.
CSS Parent Selector Explained: Selecting a Parent <li> from an <a>
Learn whether CSS has a parent selector, how :has() works, and practical alternatives for styling a parent li from a child anchor.
CSS Previous Sibling Selector: What Exists and How to Work Around It
Learn whether CSS has a previous sibling selector, why it does not, and practical ways to style earlier elements using CSS alternatives.