Home/Tools/Layout/CSS Centering Guide
Layout · Generator

CSS Centering Guide

Compare all CSS centering techniques — Flexbox, Grid, absolute, margin auto — with live preview.

Centering Method

Flexbox

The most versatile centering method. Works in all modern browsers.

Browser:All browsers
Works for:Both axes, single element, multiple children

Preview Colors

#f3f4f6
#6366f1

Live Preview — Flexbox

Centered

CSS

.parent

display: flex;
align-items: center;
justify-content: center;
Generated CSS
.parent {
  display: flex;
  align-items: center;
  justify-content: center;
}
100%
Free, no
sign-up needed
0
Code written
by hand
85+
CSS tools
in one place
<1s
Copy-ready
CSS instantly
What is this

What is the CSS Centering Guide?

Find the right CSS centering technique for your situation. Pick your element type and context - get the minimal code that works.

Everything runs locally in your browser. Adjust the controls above, preview instantly, and copy clean, production-ready CSS — no account, no upload.

How to use

Using the CSS Centering Guide

Everything lives in the panel at the top of this page. There is nothing to install — it all runs right here in your browser.

1

Describe your centering need

Select what you are centering: text, a block element, an image, a modal, or an unknown-size element. Then choose the context.

2

Pick the technique

The guide shows all applicable approaches, sorted by simplicity. Each technique is explained with the exact trade-offs.

3

Copy the minimal CSS

Click Copy next to the technique you choose. The output contains only the properties you need - no extra declarations.

The output

What the generated code looks like

Clean, production-ready CSS — no vendor bloat, no unnecessary declarations.

styles.css
/* Center anything - modern approach */
.parent {
  display: grid;
  place-items: center;
}

/* Flexbox centering */
.parent-flex {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Center block element horizontally */
.centered-block {
  margin: 0 auto;
  width: fit-content;
}

/* Absolute centering */
.parent-abs { position: relative; }
.child-abs {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

place-items: center on a grid container is the shortest working solution for centering both axes in modern browsers.

Pro tips

Pro tips for better results

display: grid; place-items: center is the shortest solution

Two lines center any single child horizontally and vertically. It works for any child size. Supported in Chrome 59+, Firefox 45+, Safari 11+.

margin: 0 auto only centres horizontally

margin: auto on a block element centres it horizontally, but only if the element has an explicit width less than the container width. It does nothing for vertical centering.

text-align: center only applies to inline content

text-align: center centres text and inline elements (like img) within their container. It has no effect on block elements like div.

The translate(-50%, -50%) technique works for any known-or-unknown size

top: 50%; left: 50%; transform: translate(-50%, -50%) centres an element of any size because translate percentages are relative to the element itself, not its parent.

FAQ

Frequently asked questions

margin: auto only distributes horizontal space in normal block flow. For vertical centering with margin: auto, the parent needs display: flex or display: grid. In those contexts, margin: auto works in all directions.
place-items is a shorthand for align-items and justify-items. On a grid container, place-items: center sets both to center, which centres all direct children on both axes. It is the most concise centering technique in modern CSS.
No. Everything runs in your browser. Nothing is sent to a server.

From the blog

Read more