Reading history

How to Center a Div in CSS: Every Method Explained

6 min read 0 views

Center anything in CSS: text-align, margin auto, Flexbox, Grid place-items and absolute positioning with transform, plus a table of which method to use.

“How do I center a div?” is one of the most searched CSS questions of all time. The confusion comes from the fact that there is no single center property. The right method depends on what you are centering (text, a block, or a group of items) and in which direction. This lesson gives you a small set of reliable techniques, explains when each one applies, and ends with a decision table you can keep coming back to.

In this lesson you will learn

  • Centering text and inline content with text-align
  • Centering a block with margin: auto
  • Centering in both directions with Grid and Flexbox
  • Centering with absolute positioning and transform
  • Choosing the right method for each situation

You should know Flexbox, Grid and positioning.

Horizontal Centering

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Horizontal centering</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 14px; }
    .area { border: 2px dashed #90a4ae; margin-bottom: 10px; padding: 6px 0; }
    .item { background: #ffcc80; padding: 6px 10px; }

    .text   { text-align: center; }                       /* 1. inline content and text */
    .auto .item { width: 200px; margin: 0 auto; }         /* 2. a block with a known width */
    .fit .item  { width: fit-content; margin-inline: auto; } /* 3. a block that shrinks to its content */
    .flex   { display: flex; justify-content: center; gap: 8px; } /* 4. a row of items */
  </style>
</head>
<body>
  <div class="area text">text-align: center <span class="item">inline span</span></div>
  <div class="area auto"><div class="item">margin: 0 auto (width 200px)</div></div>
  <div class="area fit"><div class="item">width: fit-content + auto margins</div></div>
  <div class="area flex"><span class="item">flex</span><span class="item">justify-content</span><span class="item">center</span></div>
</body>
</html>

Result

  • text-align: center centers the inline content of a block: text, links, images and inline-block elements. It is set on the container, not on the item.
  • margin: 0 auto centers a block element inside its parent. The browser splits the spare space equally between the left and right margins, so the block needs a width smaller than its parent.
  • width: fit-content makes a block only as wide as its content, so auto margins can center it without a fixed width. margin-inline: auto is the modern way to set both side margins.
  • For several items in a row, display: flex with justify-content: center centers the whole group, and gap spaces the items.

Centering in Both Directions

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Centering in both directions</title>
  <style>
    body { font-family: sans-serif; margin: 12px; display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; }
    .area { width: 150px; height: 110px; border: 2px dashed #90a4ae; }
    .item { background: #80cbc4; padding: 6px 8px; }

    .grid { display: grid; place-items: center; }                          /* 1. shortest */
    .flex { display: flex; justify-content: center; align-items: center; } /* 2. flexbox */
    .abs  { position: relative; }                                          /* 3. absolute + transform */
    .abs .item { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
    .auto { display: flex; }                                               /* 4. auto margins in flex */
    .auto .item { margin: auto; }
  </style>
</head>
<body>
  <div class="area grid"><div class="item">grid</div></div>
  <div class="area flex"><div class="item">flex</div></div>
  <div class="area abs"><div class="item">absolute</div></div>
  <div class="area auto"><div class="item">margin: auto</div></div>
</body>
</html>

Result

  • Grid: display: grid; place-items: center is the shortest way to center a child both ways. place-items sets align-items and justify-items together.
  • Flexbox: justify-content: center works along the main axis (across, in a row) and align-items: center along the cross axis (down).
  • Vertical centering only shows when the container is taller than its content. Each example box has a fixed height of 110px; a container that is only as tall as its content has no spare space to share.
  • Absolute positioning: top: 50%; left: 50% places the item’s top-left corner at the middle, and transform: translate(-50%, -50%) moves it back by half its own size. Use this for overlays, such as a play button on a video. The parent needs position: relative.
  • Auto margins in a flex container: margin: auto on a flex item takes up all spare space on every side, centering it both ways.

Which Method Should You Use?

What you are centeringDirectionMethod
Text or inline elementsHorizontaltext-align: center on the parent
One block with a widthHorizontalmargin-inline: auto
A row of itemsHorizontalFlex + justify-content: center
One childBothGrid + place-items: center
Several children in a rowBothFlex + justify-content + align-items
Something on top of other contentBothAbsolute + translate(-50%, -50%)
A whole page sectionBothGrid + place-items: center + min-height: 100vh

Common Mistakes

  1. Putting text-align: center on a block and expecting the block itself to move. It centers the content inside.
  2. Using margin: auto without a width on a normal block, which already fills the whole width.
  3. Expecting vertical centering in a container with no extra height.
  4. Using vertical-align: middle on blocks. It only affects inline and table-cell elements.

Practice Exercises

Exercise 1. Center a login card both ways on the full screen.

Show answer
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
}
.login-card { width: min(100% - 32px, 360px); }

Exercise 2. Center an icon and a label vertically inside a button.

Show answer
button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

Frequently Asked Questions

Which centering method is the best?

There is no single best method. Grid with place-items: center is the shortest for one item. Flexbox is better for a row of items, and margin-inline: auto is ideal for page-width containers.

Why does my absolutely positioned element center on the whole page?

An absolutely positioned element is placed relative to its nearest positioned ancestor. Add position: relative to the parent you want to center within.

Summary

  • text-align centers inline content; margin-inline: auto centers a block.
  • Grid place-items: center centers one child both ways.
  • Flexbox centers groups with justify-content and align-items.
  • Absolute + translate(-50%, -50%) centers overlays.

Next: Styling Images: object-fit, Filters and clip-path.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *