How to Vertically Center an Element in CSS
To vertically center a child in CSS, set the alignment on its parent. In horizontal writing, use align-items: center for a Flexbox row and justify-content: center for a column. In Grid, place-items: center centers an item on both axes within its area.
Find the vertical axis in Flexbox
The main axis of a flex container runs in the direction its items are laid out. The cross axis runs perpendicular to it. With horizontal writing and the default flex-direction: row, the cross axis is vertical, so align-items: center centers each item vertically. With flex-direction: column, the main axis is vertical. justify-content: center centers the group of items on that axis, while align-items: center affects their horizontal placement.
Center on both axes with Grid
In Grid, place-items: center sets both align-items and justify-items. A child in one grid area is centered on both axes. To keep the horizontal placement and center only vertically in horizontal writing, set align-items: center alone. With several cells, each item is aligned within its own grid area.
Use table cells or equal spacing
In a table cell, vertical-align: middle centers the cell’s contents vertically. It also works with legacy layouts that use display: table-cell. On a plain block, that declaration does not center its children. Where the box can grow around its content and you only need equal space above and below, use padding-block. Equal margin-block on the child can provide similar spacing in some layouts, but adjoining margins may collapse and do not distribute extra parent height automatically.
Vertical alignment in table cells
Run the four layout examples
Save the snippets as index.html and center.css in one folder and open the HTML file. The colored item in each frame has equal space above and below. Grid also centers it horizontally, while the Flexbox row leaves it at the horizontal start. Change justify-content: center to flex-start in the column example to move the item to the top, then restore the original declaration.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Vertical centering examples</title>
<link rel="stylesheet" href="center.css">
</head>
<body>
<main>
<h1>Vertical centering examples</h1>
<h2>Flexbox row</h2>
<div class="frame row"><p class="item">Content area</p></div>
<h2>Flexbox column</h2>
<div class="frame column"><p class="item">Content area</p></div>
<h2>Grid on both axes</h2>
<div class="frame grid"><p class="item">Content area</p></div>
<h2>CSS table cell</h2>
<div class="frame table">
<div class="cell"><p class="item">Content area</p></div>
</div>
</main>
</body>
</html>
body {
margin: 1rem;
color: #14263c;
font: 1rem/1.55 system-ui, sans-serif;
}
main {
max-inline-size: 42rem;
margin-inline: auto;
}
.frame {
box-sizing: border-box;
min-block-size: 8rem;
padding: 0.75rem;
border: 2px solid #486784;
}
.item {
box-sizing: border-box;
min-inline-size: 0;
max-inline-size: 100%;
margin: 0;
padding: 0.35rem 0.75rem;
background: #ffe2a6;
overflow-wrap: anywhere;
}
.row {
display: flex;
align-items: center;
}
.column {
display: flex;
flex-direction: column;
justify-content: center;
}
.grid {
display: grid;
place-items: center;
}
.table {
display: table;
table-layout: fixed;
inline-size: 100%;
block-size: 8rem;
}
.cell {
display: table-cell;
vertical-align: middle;
}
Troubleshoot vertical centering
In developer tools, first check the parent’s display and flex-direction. Then compare the height of the parent’s alignment area with the child’s height. With no free space, centering has no visible effect. In horizontal writing, the example’s min-block-size provides a minimum height while letting the box grow as content wraps. Narrow the window or replace the sample with a longer sentence to check for clipping.




