CSS course menu
How to Center in CSS: Horizontally, Vertically and Both
Every way to center things in CSS, and when to use each: text-align for inline content, margin auto for blocks, Flexbox and Grid place-items for both directions, absolute positioning with transform, and centering the whole page.
On this page
"How do I center a div?" is one of the most searched CSS questions. The answer depends on what you are centering and in which direction. Here is the toolbox.
Inline content: text-align#
Text, links, images and inline-block elements are centered horizontally inside their block with text-align: center:
.box { text-align: center; padding: 10px; background: #eef3ff; }<div class="box">
<p>Centered text</p>
<img src="/lesson-assets/mango.svg" alt="Mango" width="40" height="40">
</div>A block with a width: margin auto#
.card { width: 220px; margin: 0 auto; padding: 10px; background: #fff3c4; }<div class="card">Centered block</div>Only horizontal, and the block needs a width (or max-width) smaller than its container.
Both directions: Grid#
The shortest way to center anything both horizontally and vertically:
.stage {
display: grid;
place-items: center;
height: 160px;
background: #e6f5f3;
}
.stage div { padding: 12px 18px; background: #0f8a7e; color: white; border-radius: 8px; }<div class="stage"><div>Perfectly centered</div></div>place-items: center is shorthand for align-items: center plus justify-items: center.
Both directions: Flexbox#
.stage {
display: flex;
justify-content: center; /* main axis: horizontal */
align-items: center; /* cross axis: vertical */
gap: 8px;
height: 160px;
background: #fde7c8;
}
.stage span { padding: 10px 14px; background: #e8590c; color: white; border-radius: 6px; }<div class="stage"><span>One</span><span>Two</span><span>Three</span></div>Use Flexbox when several items should sit centered in a row together, as above. With Grid's place-items, several items would stack in separate rows.
Overlays: absolute and transform#
To center an element on top of another, such as a play button over a video thumbnail:
.thumb {
position: relative;
width: 280px;
height: 160px;
background: url("/lesson-assets/landscape-600.svg") center / cover;
}
.play {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 56px;
height: 56px;
border: none;
border-radius: 50%;
background: rgb(0 0 0 / 0.65);
color: white;
font-size: 22px;
}<div class="thumb"><button class="play" type="button" aria-label="Play video">▶</button></div>top: 50%; left: 50% moves the button's top-left corner to the centre; translate(-50%, -50%) then shifts it back by half its own size. Alternatively, inset: 0; margin: auto; centers an absolute element that has a fixed width and height.
Centering a whole page section#
To center content in the visible screen, give the container the screen's height:
.hero {
min-height: 100dvh;
display: grid;
place-items: center;
}Cheat sheet#
Practice#
Practice exercise
Make a 300 × 200 "empty cart" panel with a light grey background whose message and button are centered both ways and stacked vertically with 8px between them.
Show solution
.empty {
width: 300px;
height: 200px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 8px;
background: #f1f3f5;
border-radius: 10px;
}
.empty p { margin: 0; }<div class="empty">
<p>Your cart is empty.</p>
<button type="button">Browse courses</button>
</div>Key takeaway#
Finished this lesson?