Flexbox Centering (recommended)

Flexbox is the best general-purpose centering solution. It works on the parent container and centres the child element both horizontally and vertically regardless of size.

CSS
.parent {
  display: flex;
  justify-content: center; /* horizontal */
  align-items: center;     /* vertical */
  min-height: 100vh;       /* give the parent height */
}
Centered
display: flex; justify-content: center; align-items: center;

This is a two-axis solution. justify-content distributes space along the main axis (horizontal when flex-direction: row). align-items aligns items on the cross axis (vertical when flex-direction: row). Apply both to the parent and the child centres perfectly every time.

💡 The most common mistake

The parent must have a defined height for vertical centring to work. min-height: 100vh works for full-page centring. For a card, set an explicit height or let the content define it while centering within a flex row.

CSS Grid Centering

CSS Grid offers an even more concise centering syntax using the place-items shorthand, which combines align-items and justify-items.

CSS
.parent {
  display: grid;
  place-items: center; /* shorthand for both axes */
  min-height: 100vh;
}

/* Or explicitly: */
.parent {
  display: grid;
  align-items: center;
  justify-items: center;
  min-height: 100vh;
}

place-items: center is the shortest CSS centering solution that exists. It works in all modern browsers (Chrome 57+, Firefox 52+, Safari 10.1+) and is perfect for single-child centring.

Absolute Positioning + Transform

This method positions a child relative to a positioned parent using top: 50%; left: 50% and then corrects the offset with transform: translate(-50%, -50%).

CSS
.parent {
  position: relative;
  min-height: 300px;
}

.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

This works by first placing the child's top-left corner at the centre of the parent, then the translate(-50%, -50%) shifts the element back by half its own width and height, achieving true centering. Useful when you cannot use Flexbox or Grid on the parent (for example, inside a third-party component with locked styles).

Horizontal Only: margin auto

For block elements with a defined width, margin: 0 auto centres them horizontally. This is the classic pre-Flexbox approach and still perfectly valid for centring a content column.

CSS
.content {
  width: 780px;         /* or max-width */
  max-width: 100%;
  margin: 0 auto;       /* centres horizontally */
}

This does not centre vertically — it only works horizontally on block-level elements with an explicit width. It is the standard way to centre a page container or article content within a wide viewport.

Inline Elements: text-align

For inline content (text, inline-block elements, images), text-align: center on the parent centres children horizontally.

CSS
.parent {
  text-align: center; /* centres inline and inline-block children */
}

/* Force a block to be inline-block so text-align works: */
.child {
  display: inline-block;
  text-align: left; /* reset text alignment inside */
}

Method Comparison

MethodHorizontalVerticalBest for
Flexbox (recommended)Most use cases — components, cards, navbars
CSS Grid place-itemsSingle child centring — modal overlays, hero sections
Absolute + transformPositioned overlays, tooltips, when parent is locked
margin: 0 autoCentring a content column, article container
text-align: centerText and inline elements, button groups

How to Build Centred Layouts with CSSTools.io

The CSSTools.io Flexbox Generator lets you experiment with all of these alignment properties visually and see how they interact with different numbers of child items — before you write a single line of code.

1
Open the Flexbox Generator

Go to csstools.io/flexbox. The preview shows a flex container with child items that update in real time as you change settings.

2
Set justify-content to center

Click the center button in the justify-content row. The items in the preview immediately move to the horizontal centre of the container.

3
Set align-items to center

Click center in the align-items row. The items now sit at the vertical centre too. This is the two-line centering solution.

4
Test different item counts

Use the + / − buttons to add or remove items. See how the layout behaves with one item (perfect centring), three items (distributed), and wrapping behaviour when flex-wrap is enabled.

5
Download or copy

Click Download HTML for a complete standalone file or Copy CSS to get just the container properties. The generated CSS only includes properties you have changed from defaults, so it stays clean.

Build and test centred layouts visually

Adjust justify-content, align-items, flex-wrap and gap. Download the HTML or copy the CSS instantly.

Open Flexbox Generator →