CSS3 Beginner Modern Layout · 6 min read

Center an Element with Flexbox

Lesson 8 of 41 in the CSS3 learning path.

How do I center a box horizontally and vertically?

Centering on both axes is one job of Flexbox, not the whole model. The full guide to direction, gap, grow, shrink, and basis is the Flexbox row lesson. For this job, make the parent a flex container, set justify-content to center, and set align-items to center. With the default row direction, justify-content centers horizontally and align-items centers vertically. The parent must have a height to center against, so the sample gives the stage min-height: 100% after the document is given a height.

The card itself should not be stretched. Leave align-items at center, and give the card a max-width so a long title wraps inside the card instead of growing to the stage width. margin-inline: auto on a block can center horizontally only, and it does nothing vertically, which is why this flex pair replaced the old absolute positioning trick for ordinary page UI. Use the pattern for a sign-in card, an empty state, or a confirm step.

Syntax & Parameters

Syntax
.stage { display: flex; justify-content: center; align-items: center; min-height: 100%; }
.dialog { width: 100%; max-width: 22rem; }

Example

Interactive preview available
styles.css
/* One Flexbox job: center a card. Direction, gap, and grow live in the row lesson. */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, sans-serif;
  line-height: 1.45;
  color: #1c2430;
  background: #e7e5e4;
}
.stage {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  min-height: 100%;
  padding: 1.25rem;
}
.dialog {
  width: 100%;
  max-width: 22rem;
  padding: 1.25rem 1.3rem 1.15rem;
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 12px 30px rgba(28, 36, 48, 0.12);
}
.kicker {
  margin: 0 0 0.3rem;
  color: #0f766e;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
h1 { margin: 0 0 0.4rem; font-size: 1.35rem; line-height: 1.2; }
p { margin: 0 0 1rem; color: #5c6b7a; }
.actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.5rem;
}
button {
  padding: 0.45rem 0.75rem;
  border: 0;
  border-radius: 8px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.ghost { background: #f4f1ea; color: #1c2430; }
.primary { background: #0f766e; color: #f8fafc; }

Expected Output

The stage fills the viewport with a gray background. A white card about 22rem wide sits in the horizontal and vertical center, with its title and paragraph wrapping inside the card rather than stretching the card to the stage width. A soft shadow separates the card from the gray. The two buttons sit in a row at the right edge of the card, with a half-rem gap.

If the viewport is shorter than the card, the stage padding keeps the card from touching the edges and the page can scroll. The card does not stick to the top, and it is not offset with a negative margin or with position: absolute.

Line-by-Line Walkthrough

  1. html and body both get height: 100% so a percentage height on the stage has a parent height to resolve against. Without that, min-height: 100% collapses.
  2. .stage is the flex container. The dialog is the only flex item, which is the usual shape of this trick.
  3. flex-direction: row is the default. On a row, justify-content moves along the horizontal axis and align-items moves along the vertical axis.
  4. justify-content: center and align-items: center together put the item in the middle of both axes.
  5. align-items: center also stops the default stretch, so the dialog stays as tall as its content instead of filling the stage.
  6. width: 100% and max-width: 22rem let the card shrink on a narrow stage and stop growing once it reaches 22rem. The title wraps inside the card.
  7. padding on the stage keeps the card off the viewport edge when the stage is shorter than the card plus that padding.
  8. The button row is a second flex container. justify-content: flex-end parks the actions on the right. That is alignment, not the page-centering trick.
  9. margin-inline: auto on a block would center the card horizontally and would not center it vertically. This sample does not need that margin.
  10. The absolute-and-transform centering trick still works, but it takes the card out of flow. Flex centering leaves the card in a normal formatting context.

This is one job of Flexbox. The row lesson, css-flex-row, is the full guide to direction, gap, grow, shrink, and basis.

Common Pitfalls & Security

The center fails when the parent has no height. Percentage heights need a chain of heights, and a flex item still stretches if you forget align-items.

  • min-height: 100% on the stage does nothing useful if html and body are height: auto. Set those heights, or use min-height: 100vh when you mean the viewport.
  • align-items: stretch, the default, makes a single child as tall as the stage. The card then looks like a full-page panel, not a centered dialog.
  • justify-content: center alone only centers on the main axis. In a row that is horizontal. You still need align-items for the vertical axis.
  • margin: auto on a flex item can also center it, but only if the item is allowed to take the free space. Mixing auto margins with space-between is a confusing second system.
  • An absolutely positioned dialog with a high z-index can cover the page and trap clicks. A centered empty state should not block the rest of the document unless it is a real modal.

Best Practices & Modern Standards

Use this flex pair when the design really is one box in the middle of a region: a sign-in card, an empty library, or a confirm step. Give the parent a definite height, center on both axes, and cap the child with max-width so the text wraps. Keep the button row as its own flex container so its alignment stays independent of the stage.

Grid can do the same job with place-items: center, which is worth knowing when the page is already a grid. It is still one job, not a reason to skip the Flexbox row lesson. For a sticky header or a badge in the corner, use positioning. For a sidebar beside the lesson, use grid tracks. Custom properties can store the dialog max-width so a compact theme can tighten it without touching the centering rules.

Real-World Use

A course player shows a "Resume lesson" card in the middle of the stage when the student returns and no video is selected yet. The card is centered both ways inside the player frame, the title wraps at 22rem, and the primary action sits at the right of the card.

The same pattern is the sign-in panel on a marketing page and the empty state in a dashboard main column. The nav above that column is a flex row from the full Flexbox lesson, not another centering trick.

Frequently Asked Questions

Which two properties center on both axes?

justify-content: center and align-items: center, on a parent with display: flex. In the default row direction, the first is horizontal and the second is vertical. The parent also needs a height, which this sample gets from height: 100% on the document and min-height: 100% on the stage.

Why did my card stretch to the full height of the stage?

The initial align-items value is stretch, so a flex item with height: auto grows to the container cross size. Set align-items: center, or give the item align-self: center. The card then stays as tall as its padding and text.

Can margin: auto center a box vertically?

On a block, margin-inline: auto centers horizontally and the vertical margins do not center the box in the viewport. On a flex item, auto margins absorb free space and can center it, which is a second technique. The explicit justify-content and align-items pair is easier to read.

Is this the whole of Flexbox?

Centering is one job, not the whole model. Direction, gap, grow, shrink, and basis are covered in the Flexbox row lesson. Use that lesson for a navbar or a card row, and come back here only when a single box must sit in the middle of a region.

Need more coins?

You do not have enough coins for this action.

Your balance: 0 coins

Share

Reactions

Loading reactions…