“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: centercenters 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 autocenters 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-contentmakes a block only as wide as its content, so auto margins can center it without a fixed width.margin-inline: autois the modern way to set both side margins.- For several items in a row,
display: flexwithjustify-content: centercenters the whole group, andgapspaces 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: centeris the shortest way to center a child both ways.place-itemssetsalign-itemsandjustify-itemstogether. - Flexbox:
justify-content: centerworks along the main axis (across, in a row) andalign-items: centeralong 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, andtransform: 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 needsposition: relative. - Auto margins in a flex container:
margin: autoon a flex item takes up all spare space on every side, centering it both ways.
Which Method Should You Use?
| What you are centering | Direction | Method |
|---|---|---|
| Text or inline elements | Horizontal | text-align: center on the parent |
| One block with a width | Horizontal | margin-inline: auto |
| A row of items | Horizontal | Flex + justify-content: center |
| One child | Both | Grid + place-items: center |
| Several children in a row | Both | Flex + justify-content + align-items |
| Something on top of other content | Both | Absolute + translate(-50%, -50%) |
| A whole page section | Both | Grid + place-items: center + min-height: 100vh |
Common Mistakes
- Putting
text-align: centeron a block and expecting the block itself to move. It centers the content inside. - Using
margin: autowithout a width on a normal block, which already fills the whole width. - Expecting vertical centering in a container with no extra height.
- Using
vertical-align: middleon 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-aligncenters inline content;margin-inline: autocenters a block.- Grid
place-items: centercenters one child both ways. - Flexbox centers groups with
justify-contentandalign-items. - Absolute +
translate(-50%, -50%)centers overlays.
