Question

How to Horizontally Center a div in CSS

htmlcssalignmentcentering

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: auto tells 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.

Take Quiz

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

  • #inner is narrower than #outer
  • margin-left: auto and margin-right: auto share the extra horizontal space
  • The element appears centered

2. Using Flexbox

Flexbox is often the easiest modern solution.

#outer {
  display: flex;
  justify-content: center;
  background: #eee;
}

#inner {
  width: 200px;
  background: #cde;
}

Why it works

  • display: flex turns the parent into a flex container
  • justify-content: center centers children horizontally along the main axis

3. Using CSS Grid

#outer {
  display: grid;
  justify-content: center;
  background: #eee;
}

This works well when using Grid for layout.

4. Centering inline content is different

If you want to center text or inline elements, use text-align: center on the parent.

#outer {
  text-align: center;
}

This does not center a normal block-level <div> by itself.

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:

  1. The browser creates #outer with a width of 500px.
  2. The browser creates #inner with a width of 200px.
  3. That leaves 300px of extra horizontal space inside #outer.
  4. margin-left: auto and margin-right: auto tell the browser to divide that free space automatically.
  5. The browser gives 150px to the left margin and 150px to the right margin.
  6. The child appears centered.

If #inner had width: 100%, then there would be no remaining horizontal space, so auto margins would not visibly center anything.

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;
}
Take Quiz

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

.overlay {
  display: flex;
  justify-content: center;
  align-items: center;
}

This is common for loading screens, alerts, and modals.

5. Utility-class usage

In many codebases or CSS frameworks, horizontal centering is wrapped in reusable classes such as:

.mx-auto {
  margin-left: auto;
  margin-right: auto;
}

This avoids rewriting the same centering rules repeatedly.

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;
  justify-content: center;
}

Why it fails:

  • This centers content inside #inner
  • It does not center #inner inside #outer

Fix:

#outer {
  display: flex;
  justify-content: center;
}

4. Confusing horizontal and vertical centering

justify-content: center;

This centers along the main axis in Flexbox. If flex-direction changes, the axis changes too.

5. Using old hacks when modern CSS is simpler

Older methods like absolute positioning and manual offsets are usually harder to maintain than Flexbox or Grid.

Comparisons

MethodBest forRequires child width?Centers block elements?Notes
margin: 0 autoSimple block centeringUsually yesYesClassic and widely used
text-align: centerText or inline elementsNoNoCenters text, not block <div> elements
FlexboxModern layoutsNo, not alwaysYesVery flexible and easy to read
GridGrid-based layoutsNo, not alwaysYesGreat when already using Grid

margin: 0 auto vs Flexbox

Featuremargin: 0 autoFlexbox
SimplicityVery simpleSimple
Works for single centered blockYesYes
Good for multiple childrenLimitedExcellent
Responsive layout controlBasicStrong
Common in old and new codeYesYes

A good rule:

  • Use margin: 0 auto for a single block with a known or limited width.
  • Use Flexbox when the parent is already managing layout.
Take Quiz

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 auto usually needs the child to be narrower than the parent.
  • text-align: center centers inline content, not block-level <div> elements.
  • In Flexbox, horizontal centering is commonly done with justify-content: center.
  • In Grid, justify-content: center can center grid content horizontally.
  • max-width is often better than fixed width for responsive layouts.

Quick pattern

.container {
  max-width: 800px;
  margin: 0 auto;
}

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.

Take Quiz

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

  1. Create an outer container with visible background spacing.
  2. Add an inner card element with a name and short description.
  3. Horizontally center the card inside the container.
  4. Use responsive sizing so the card does not overflow on small screens.
  5. Add simple styling so the centered result is easy to see.
Take Quiz

Keep learning

Related questions

View more