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 <div> to appear centered from left to right within the outer <div>. What CSS approaches can be used to do this correctly?
Short Answer
By the end of this page, you will understand how horizontal centering works in CSS, why margin: 0 auto only works in certain cases, and when to use alternatives like Flexbox or Grid. You will also see practical examples, common mistakes, and a small project to practice the concept.
Concept
Horizontal centering in CSS means placing an element so that the empty space on its left and right is equal inside its parent container.
For block-level elements like a <div>, the most common traditional approach is:
margin-left: auto;
margin-right: auto;
This is often shortened to:
margin: 0 auto;
However, this only works when the centered element has a width smaller than its parent. If the child takes up the full width, there is nothing to center because it already fills the container.
Modern CSS also gives you layout tools like Flexbox and Grid, which make centering much easier and more predictable.
Why this matters in real programming:
- Centering is common in cards, forms, modals, buttons, navigation sections, and page layouts.
- Different layout methods behave differently depending on element type and width.
- Choosing the right centering method helps avoid fragile CSS hacks.
A key idea is that CSS centering depends on how the element participates in layout:
- Is it a block element?
- Does it have a defined width?
- Is the parent using Flexbox or Grid?
- Are you centering text or an actual element?
Understanding those questions helps you choose the correct solution instead of guessing.
Mental Model
Think of the parent <div> as a shelf and the child <div> as a box placed on it.
- If the box has a known width and there is extra space on both sides,
margin: autotells the browser to split that leftover space evenly. - If the shelf uses Flexbox, it can arrange items like a smart organizer and place the box in the middle automatically.
- If the box already stretches across the entire shelf, there is no leftover space to split, so centering has no visible effect.
So horizontal centering is really about how the remaining space is distributed.
Syntax and Examples
1. Classic method: margin: 0 auto
Use this when the child is a block element with a set width.
<div id="outer">
<div id="inner">Foo foo</div>
</div>
#outer {
width: 400px;
background: #eee;
}
#inner {
width: 200px;
margin: 0 auto;
background: #cde;
}
Why it works
#inneris narrower than#outermargin-left: autoandmargin-right: autoshare the extra horizontal space- The element appears centered
2. Using Flexbox
Flexbox is often the easiest modern solution.
Step by Step Execution
Consider this example:
<div id="outer">
<div id="inner">Centered box</div>
</div>
#outer {
width: 500px;
background: lightgray;
}
#inner {
width: 200px;
margin: 0 auto;
background: steelblue;
color: white;
}
Here is what happens step by step:
- The browser creates
#outerwith a width of500px. - The browser creates
#innerwith a width of200px. - That leaves
300pxof extra horizontal space inside#outer. margin-left: autoandmargin-right: autotell the browser to divide that free space automatically.
Real World Use Cases
Horizontal centering is used in many common interfaces:
- Login forms: center a form card inside the page or a panel
- Call-to-action sections: center a button group or message box
- Modal dialogs: center a dialog container within an overlay
- Article layouts: center the main content column on large screens
- Dashboard widgets: center smaller panels inside fixed-width containers
- Image or preview cards: center content blocks inside responsive sections
Example: centering a form container
.form-wrapper {
width: 320px;
margin: 0 auto;
}
Example: centering items in a toolbar with Flexbox
.toolbar {
display: flex;
justify-content: center;
}
Real Codebase Usage
In real projects, developers choose centering techniques based on layout context.
Common patterns
1. Fixed-width content container
.container {
max-width: 960px;
margin: 0 auto;
}
This is one of the most common patterns in websites. It keeps content readable and centered on wide screens.
2. Flexbox for component layout
.card-row {
display: flex;
justify-content: center;
gap: 1rem;
}
This is common in component libraries and dashboards.
3. Guarding against full-width children
Developers often use max-width instead of a fixed width to keep layouts responsive:
.dialog {
max-width: 500px;
margin: 0 auto;
}
4. Combining horizontal and vertical centering
{
: flex;
: center;
: center;
}
Common Mistakes
1. Forgetting to set a width
Broken example:
#inner {
margin: 0 auto;
}
Why it fails:
- A block
<div>usually stretches to fill the available width - If it already takes full width, there is nothing to center
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, like text or inline elements
- It does not center a normal block-level
<div>itself
3. Applying Flexbox to the wrong element
Broken example:
#inner {
display: flex;
: center;
}
Comparisons
| Method | Best for | Requires child width? | Centers block elements? | Notes |
|---|---|---|---|---|
margin: 0 auto | Simple block centering | Usually yes | Yes | Classic and widely used |
text-align: center | Text or inline elements | No | No | Centers text, not block <div> elements |
| Flexbox | Modern layouts | No, not always | Yes | Very flexible and easy to read |
| Grid | Grid-based layouts | No, not always | Yes | Great when already using Grid |
Cheat Sheet
/* Classic block centering */
.child {
width: 200px;
margin: 0 auto;
}
/* Flexbox centering */
.parent {
display: flex;
justify-content: center;
}
/* Grid centering */
.parent {
display: grid;
justify-content: center;
}
/* Center text or inline elements */
.parent {
text-align: center;
}
Rules to remember
margin: 0 autousually needs the child to be narrower than the parent.text-align: centercenters inline content, not block-level<div>elements.- In Flexbox, horizontal centering is commonly done with
justify-content: center. - In Grid,
justify-content: centercan center grid content horizontally. - is often better than fixed for responsive layouts.
FAQ
Why is margin: 0 auto not centering my div?
Usually because the element has no width set, or it already takes up the full width of its parent.
Can I center a div without giving it a width?
Yes, with Flexbox or Grid in many cases. With margin: auto, a width or max-width is usually needed for visible centering.
Does text-align: center center a div?
No. It centers text and inline or inline-block content inside the parent.
What is the modern way to center an element horizontally in CSS?
Flexbox is one of the most common modern approaches:
.parent {
display: flex;
justify-content: center;
}
Should I use width or max-width?
max-width is often better for responsive layouts because it allows the element to shrink on smaller screens.
Can I use Flexbox and margin: auto together?
Yes. In some layouts, auto margins inside flex containers are also useful, but for simple horizontal centering, justify-content: center is usually clearer.
Mini Project
Description
Build a simple centered profile card inside a page container. This project demonstrates two common horizontal centering techniques: margin: 0 auto for a single fixed-width block and Flexbox for parent-controlled layout. It is useful because centered cards, forms, and panels appear in many real applications.
Goal
Create a page where a profile card is horizontally centered inside its parent container and remains readable on different screen sizes.
Requirements
- Create an outer container with visible background spacing.
- Add an inner card element with a name and short description.
- Horizontally center the card inside the container.
- Use responsive sizing so the card does not overflow on small screens.
- Add simple styling so the centered result is easy to see.
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers.
Get the Selected Radio Button Value with jQuery
Learn how to find which radio button is selected in jQuery and get its value with simple examples and common mistakes.