Question

How to Horizontally Center an Element in CSS

csshtmlalignmentcentering

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 auto tells 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?

Take Quiz

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

  • #inner has a width of 200px
  • #outer is 400px wide
  • The extra space is shared equally on both sides

2. Flexbox method

<div id="outer">
  <div id="inner">Foo foo</div>
</div>
#outer {
  display: flex;
  justify-content: center;
  background: #eee;
}

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

Why it works

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

3. Grid method

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

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

Grid can also center child elements cleanly.


4. Centering inline or text content

If you want to center text or inline elements inside a container, use:

#outer {
  text-align: center;
}

This does not center a block <div> in the usual way. It centers inline content such as:

  • text
  • <span>
  • <a>
  • inline-block elements

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

  1. The browser creates the outer <div>.
  2. #outer gets a width of 500px.
  3. The browser creates the inner <div> inside it.
  4. #inner gets a width of 200px.
  5. That leaves 300px of extra horizontal space in the parent.
  6. margin: 0 auto means:
    • top margin = 0
    • bottom margin = 0
    • left margin = auto
    • right margin = auto
  7. The browser splits the 300px equally:
    • left margin = 150px
    • right margin = 150px
  8. The inner element appears centered.

What if width is missing?

If #inner has no width set, a block <div> usually expands to fill the parent width. Then there is no extra horizontal space, so auto margins do not visibly move it.

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

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:

.card {
  width: 90%;
  max-width: 400px;
  margin: 0 auto;
}

This keeps the element centered while allowing it to shrink on small screens.

5. Utility classes

Many teams create reusable classes:

.center-block {
  margin-left: auto;
  margin-right: auto;
}

Then they apply them across components for consistency.

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:

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

Why it fails:

  • This centers content inside #inner, not #inner itself within #outer

Fix:

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

4. Confusing horizontal and vertical centering

#outer {
  display: flex;
  justify-content: center;
  align-items: center;
}
  • justify-content: center usually handles horizontal centering in a row layout
  • align-items: center handles vertical centering

Use only what you need.


5. Centering an element wider than its parent

If the child is wider than the parent, centering may not look correct because the child overflows.

Fix:

  • use max-width
  • use responsive widths
  • check box sizing and padding

Comparisons

MethodBest forWorks on block elements?Needs width on child?Notes
margin: 0 autoSimple single element centeringYesUsually yesClassic and very common
display: flex; justify-content: center;Modern layouts and multiple childrenYesNo, not alwaysGreat for alignment control
display: grid; justify-content: center;Grid-based layoutsYesNo, not alwaysUseful when Grid is already used
text-align: centerText and inline elementsNo for normal block divsNoCenters text, not block boxes

Quick rule of thumb

  • Use margin: 0 auto for a single block with a known or max width
  • Use Flexbox when the parent controls layout
  • Use Grid when the parent is already a grid container
  • Use text-align: center only for text or inline content
Take Quiz

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 auto works 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: center centers inline content, not normal block boxes
  • Flexbox and Grid center through the parent container

Responsive pattern

.card {
  width: 90%;
  max-width: 400px;
  margin: 0 auto;
}

Common choice

For many page layouts:

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

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:

.child {
  max-width: 400px;
  width: 90%;
  margin: 0 auto;
}
Take Quiz

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

  1. Create an outer container with a visible background.
  2. Add an inner card element with text content.
  3. Center the card horizontally inside the outer container.
  4. Make the card responsive by using max-width.
  5. Add basic padding and colors so the layout is easy to see.
Take Quiz

Keep learning

Related questions

View more