Center a card horizontally and vertically with CSS Flexbox
To center one card both horizontally and vertically with Flexbox, set display: flex, justify-content: center, and align-items: center on the parent. The parent needs free space for the alignment to be visible. Use a min-height if the card may grow with its content.
Set alignment on the parent
.stage {
display: flex;
justify-content: center;
align-items: center;
min-height: 320px;
}
justify-content aligns along the main axis and align-items along the cross axis. In the default row with horizontal writing, those are the horizontal and vertical axes. Here .stage is the container and its direct child .card is the one flex item. The heading and the paragraph inside the card keep their text alignment. See CSS Flexbox · justify-content and CSS Flexbox · align-items.
Try a centered card that grows with its content
Save and open the example as flex-center.html. It needs no external files or JavaScript. The card’s width: min(100%, 280px) takes the smaller of the parent’s content width and 280px. With box-sizing: border-box, the card’s padding is included in that width.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Center a card with Flexbox</title>
<style>
body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
* { box-sizing: border-box; }
h1 { font-size: 24px; }
.stage { display: flex; justify-content: center; align-items: center;
min-height: 320px; padding: 24px; border: 1px solid #7795a3;
background: #edf5f7; }
.card { width: min(100%, 280px); min-width: 0; padding: 20px;
border-radius: 12px; background: white; box-shadow: 0 4px 16px #20334818;
overflow-wrap: anywhere; }
.card h2 { margin: 0 0 12px; font-size: 20px; }
.card p { margin: 0; }
</style>
</head>
<body>
<h1>Centering with Flexbox</h1>
<div class="stage">
<section class="card" aria-labelledby="card-title">
<h2 id="card-title">A centered card</h2>
<p>The parent aligns the card on two axes. The card and its container can grow when the content gets longer.</p>
</section>
</div>
</body>
</html>
Suppose the viewport is 390px wide, with no space reserved for a vertical scrollbar. Body margins use 40px, leaving a 350px outer width for .stage. Padding and borders take another 50px, leaving 300px inside. A 280px card therefore leaves 10px on each side. At a 320px viewport the card fills the available 230px. Centering still applies even though there is no visible gap.
The parent starts at a minimum height of 320px. Its border and padding take 50px, leaving 270px inside. When the card needs more height, the parent grows. min-width: 0 lets the card get past the automatic minimum width and overflow-wrap: anywhere lets long text such as a URL break when needed.
Compare the starting positions in a row and a column
First change only justify-content in .stage to flex-start. In a left-to-right row, the card moves to the horizontal start. Add flex-direction: column as shown below. The main axis becomes vertical, so the card moves to the top and stays horizontally centered. Remove the added direction and restore justify-content: center afterward.
/* Values to compare in the example’s .stage rule */
.stage {
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
min-height: 320px;
padding: 24px;
}
With both axes centered, row and column can position a single card in the same place. That is why the exercise uses flex-start on one axis. The physical start also depends on the document’s direction and writing-mode.
Center in the viewport and diagnose alignment issues
For a page devoted to the card, remove the outer h1 and promote the card heading to h1. Keep its id and update the selector to .card h1. Add the following declarations at the end of style to make the card area at least one viewport tall. For a page with a header and footer, give the card area the remaining height.
body { margin: 0; }
.stage {
min-height: 100vh;
min-height: 100dvh;
}
100vh provides the fallback. The later 100dvh declaration follows the dynamic viewport as browser controls expand or collapse. Add enough text to make the card taller than the viewport and verify that the page still scrolls. Centering a tall card inside a container fixed with height can push its top outside the visible area. See CSS Values and Units · Viewport lengths.
If centering appears to have no effect, compare the parent’s computed display and content width with the child’s actual width in developer tools. Try flex-grow: 1 on the card to use all the spare horizontal space. Then remove it and try margin-inline-start: auto instead. In the initial left-to-right row, the left margin absorbs the spare space. Remove the trial declaration when done. See CSS Flexbox · Auto margins.




