"How do I center a div?" is one of the most searched CSS questions of all time, and it has become a running joke among developers. The joke exists because there isn't one answer. Centering text is different from centering a box, and centering horizontally is different from centering vertically. Older CSS made vertical centering genuinely awkward.
The good news is that modern CSS makes every case simple once you know which tool fits. In this lesson you'll learn the five techniques you need: text-align for inline content, margin: 0 auto for blocks, flexbox and grid for centering in both directions, and absolute positioning for overlays such as badges and pop-ups.
At the end there's a quick reference table, so next time you need to center something you can pick the right method in seconds.
Centering text and inline content
.title { text-align: center; }text-align: center centers the inline content inside a block: text, links, <span> elements, images (which are inline by default) and inline-block buttons. It doesn't move the block itself. If a paragraph is full width, its text appears centered, but the paragraph box still spans the whole container.
Centering a block horizontally with margin auto
.card { width: 320px; margin: 0 auto; }- A block with a width (or
max-width) andmargin-left: auto; margin-right: autois centered in its container. - The browser splits the leftover space equally between the two auto margins.
- Without a width, the block fills its container, so there's no leftover space and nothing changes.
- To center an image this way, first make it a block:
img { display: block; margin: 0 auto; }.
Centering in both directions with flexbox and grid
Flexbox and grid center in both directions with very little code:
/* flexbox */
.parent { display: flex; justify-content: center; align-items: center; }
/* grid: the shortest way */
.parent { display: grid; place-items: center; }Both work on the parent, and both need the parent to be taller than the child for vertical centering to be visible. Give the parent a min-height, such as min-height: 100dvh for a full-screen section. A third shortcut also works: make the parent flex or grid and give the child margin: auto.
Centering with absolute position
For overlays, badges and pop-ups that sit on top of other content:
.parent { position: relative; }
.child {
position: absolute;
top: 50%; left: 50%;
transform: translate(-50%, -50%);
}top: 50% and left: 50% put the child's top-left corner in the middle, then translate(-50%, -50%) shifts it back by half its own size. A newer version is position: absolute; inset: 0; margin: auto;, which works when the child has a set width and height.
CSS Centering Syntax
Centering cheatsheet
.text { text-align: center; }
.block { max-width: 600px; margin-inline: auto; }
.grid-center { display: grid; place-items: center; min-height: 300px; }
.flex-center { display: flex; justify-content: center; align-items: center; }
.overlay { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }CSS Centering Summary
text-align: centercenters text and inline content, not the block itself.margin: 0 autocenters a block that has a width.display: grid; place-items: centercenters a child in both directions.- Flexbox uses
justify-contentandalign-itemsto center along both axes. - Vertical centering needs the parent to have height to spare.
CSS Centering Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Text vs block centering
<div class="a">Centered text in a full-width box</div>
<div class="b">A centered box</div>div {
padding: 12px;
margin-bottom: 10px;
font-family: sans-serif;
background: #e0e7ff;
}
.a { text-align: center; }
.b {
width: 200px;
margin-left: auto;
margin-right: auto;
}Line-by-line explanation
- Line 8The text moves to the middle, but the box still fills the width.
- Lines 11 to 13The 200-pixel box sits in the middle with equal auto margins.
Perfect centering with grid and flexbox
<div class="grid-box"><div class="child">Grid</div></div>
<div class="flex-box"><div class="child">Flex</div><div class="child">Flex</div></div>.grid-box, .flex-box {
min-height: 140px;
background: #ecfdf5;
border: 2px dashed #16a34a;
margin-bottom: 12px;
}
.grid-box { display: grid; place-items: center; }
.flex-box {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
}
.child { padding: 12px 18px; background: #16a34a; color: white; border-radius: 8px; font: 600 15px sans-serif; }Line-by-line explanation
- Line 2The parents need extra height to show vertical centering.
- Line 8Two declarations center the child both ways.
- Lines 11 to 14Flexbox centers a row of items and spaces them with
gap.
A centered badge and modal
<div class="avatar">
<img src="https://picsum.photos/seed/face/120/120" alt="Profile photo" width="120" height="120">
<span class="badge">3</span>
</div>
<div class="screen">
<div class="modal">Saved!</div>
</div>.avatar { position: relative; width: 120px; }
.avatar img { display: block; border-radius: 50%; }
.badge {
position: absolute; top: 0; right: 0;
width: 28px; height: 28px; display: grid; place-items: center;
background: #dc2626; color: white; border-radius: 50%; font: bold 14px sans-serif;
}
.screen { position: relative; height: 160px; background: #1f2937; margin-top: 12px; border-radius: 10px; }
.modal {
position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%);
background: white; padding: 16px 24px; border-radius: 8px; font: 600 16px sans-serif;
}Line-by-line explanation
- Line 1The avatar is the positioning context for the badge.
- Line 5Grid also centers the number inside the badge.
- Line 9The dark screen is the context for the modal.
- Lines 11 to 12The modal's corner goes to the middle, then the transform pulls it back by half its size.
Common CSS Centering Mistakes
Using text-align to center a box
.card { width: 300px; text-align: center; } /* the card stays on the left */.card { width: 300px; margin: 0 auto; text-align: center; }text-align centers what's inside the card, not the card itself.
margin: auto without a width
.box { margin: 0 auto; } /* fills the width, so nothing to center */.box { max-width: 400px; margin: 0 auto; }vertical-align on block elements
.parent { height: 300px; vertical-align: middle; }.parent { height: 300px; display: grid; place-items: center; }vertical-align only works on inline elements and table cells, not on block layouts.
CSS Centering in Real Projects
Centering everywhere
Landing pages center a hero heading and button with grid or flexbox. Checkout and contact boxes are centered with max-width and margin: auto. Notification badges on app icons use absolute positioning, and every modal dialog library centers its window with either the transform technique or flexbox on a full-screen overlay. The native <dialog> element is centered by the browser using margin: auto.
CSS Centering Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Center a child in both directions with grid.
.parent {
display: grid;
: center;
}
place-items sets both align-items and justify-items.Which rule centers the text inside a paragraph?
What does a block need for margin: 0 auto to center it?
In the transform centering trick, why is translate(-50%, -50%) needed?
The message should sit in the exact center of the stage, both horizontally and vertically. Fix the CSS.
<div class="stage"><p class="msg">Hello</p></div>.stage {
height: 200px;
text-align: center;
vertical-align: middle;
}
.msg { margin: 0; }- Message is vertically centered
- Message is horizontally centered
CSS Centering Quiz
5 questions to check what you learned.
Which is the shortest way to center a child both ways?
- position: center
- align: middle
- display: grid; place-items: center
- margin: center
Which flexbox property centers items along the main axis?
- align-items
- align-content
- flex-center
- justify-content
Why doesn't vertical centering show even with align-items: center?
- Flexbox can't center vertically
- It needs !important
- The child needs a border
- The parent is only as tall as the child
How do you center an image horizontally with margin auto?
- img { display: block; margin: 0 auto; }
- img { margin: auto; } alone
- img { align: center; }
- img { text-align: center; }
Which works for a child with a set width and height inside a positioned parent?
- position: absolute; center: 0
- position: fixed; margin: 0
- position: absolute; inset: 0; margin: auto
- position: static; inset: auto
CSS Centering Challenge
A centered hero section
- Make
.heroa flex container that centers its content horizontally and vertically, stacked in a column - Give
.heroamin-heightof240px - Center the text inside
.herowithtext-align - Make
.innerno wider than300px
<section class="hero">
<div class="inner">
<h2>Learn CSS today</h2>
<p>Short lessons with live examples.</p>
</div>
</section>.hero { background: #fef3c7; font-family: sans-serif; }
- Hero is a centered column
- Hero is at least 240px tall
- Text is centered
- Inner box is at most 300px wide