SolutionsCase StudiesAI ToolsResourcesAboutContact
Get Your Free AI Automation Audit Book a Free 30-Minute Strategy Call
Playground~/frontend Open editor

~/css/center-a-div

7 CSS Ways to Center a Div

7 Pure CSSPublished 2026-10-05Free · no sign-up

Seven reliable ways to centre an element, each shown live: grid with place-items, flexbox with justify and align, margin: auto in a flex parent, absolute positioning with translate, inset: 0 with margin: auto, line-height for a single line of text and align-content on a plain block.

01. Grid with place-items: center

CenteringGrid

The shortest way to centre anything in both directions: display: grid and place-items: center on the parent. It works for one child of any size and needs nothing on the child itself.

View code
<div class="ctr1"><span>Centred</span></div>
.ctr1 { box-sizing: border-box; display: grid; place-items: center; width: 260px; max-width: 100%; height: 150px; border: 2px dashed #d0d5dd; border-radius: 12px; }
.ctr1 span { padding: 10px 16px; border-radius: 8px; background: #7f56d9; color: #fff; font: 700 14px Inter, system-ui, sans-serif; }

02. Flexbox justify and align

CenteringFlexbox

Flexbox centres with two properties: justify-content for the main axis and align-items for the cross axis. It is the natural choice when the parent already lays out a row of items and you want them centred as a group.

View code
<div class="ctr2"><span>One</span><span>Two</span></div>
.ctr2 { box-sizing: border-box; display: flex; justify-content: center; align-items: center; gap: 8px; width: 260px; max-width: 100%; height: 150px; border: 2px dashed #475569; border-radius: 12px; }
.ctr2 span { padding: 10px 14px; border-radius: 8px; background: #38bdf8; color: #082f49; font: 700 14px Inter, system-ui, sans-serif; }

03. margin: auto Inside Grid or Flex

CenteringMargin

In a flex or grid container, margin: auto on the child absorbs all free space on every side, which centres it in both directions. It is handy when only one child of several should be centred.

View code
<div class="ctr3"><span>margin: auto</span></div>
.ctr3 { box-sizing: border-box; display: flex; width: 260px; max-width: 100%; height: 150px; border: 2px dashed #d0d5dd; border-radius: 12px; }
.ctr3 span { margin: auto; padding: 10px 14px; border-radius: 8px; background: #079455; color: #fff; font: 700 13px "JetBrains Mono", ui-monospace, monospace; }

04. Absolute Position and translate

CenteringPosition

The classic overlay technique: top and left at 50 percent move the element's corner to the centre, and translate(-50%, -50%) moves it back by half its own size. Use it when the element must float over other content, such as a play button on an image.

View code
<div class="ctr4"><span>Over the image</span></div>
.ctr4 { position: relative; width: 260px; max-width: 100%; height: 150px; border-radius: 12px; background: linear-gradient(135deg, #312e81, #be185d); }
.ctr4 span { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 10px 14px; border-radius: 8px; background: rgba(255,255,255,.92); color: #1e1b4b; font: 700 14px Inter, system-ui, sans-serif; white-space: nowrap; }

05. inset: 0 with margin: auto

CenteringPosition

An absolutely positioned element with inset: 0, a fixed width and height and margin: auto is centred by the browser's own sizing rules, with no transform. It suits fixed-size dialogs and badges.

View code
<div class="ctr5"><span>120 × 60</span></div>
.ctr5 { box-sizing: border-box; position: relative; width: 260px; max-width: 100%; height: 150px; border: 2px dashed #d0d5dd; border-radius: 12px; }
.ctr5 span { position: absolute; inset: 0; width: 120px; height: 60px; margin: auto; display: grid; place-items: center; border-radius: 8px; background: #f79009; color: #fff; font: 700 14px "JetBrains Mono", ui-monospace, monospace; }

06. Centring a Single Line of Text

CenteringText

For a single line of text in a fixed-height box, text-align: center and a line-height equal to the height still work, with no layout model at all. It breaks as soon as the text wraps, so use it only for short labels such as badges and avatars.

View code
<div class="ctr6">AB</div>
.ctr6 { width: 72px; height: 72px; border-radius: 50%; background: #e11d48; color: #fff; font: 800 24px/72px Inter, system-ui, sans-serif; text-align: center; }

07. align-content on a Plain Block

CenteringNew

Newer browsers let align-content: center work on an ordinary block container, so vertical centring no longer needs flex or grid. Older browsers ignore it and show the content at the top, which is a harmless fallback.

View code
<div class="ctr7"><p>Vertically centred block content</p></div>
.ctr7 { align-content: center; width: 260px; max-width: 100%; height: 150px; padding: 0 16px; box-sizing: border-box; border: 2px dashed #d0d5dd; border-radius: 12px; text-align: center; }
.ctr7 p { margin: 0; color: #101828; font: 700 14px Inter, system-ui, sans-serif; }

About these CSS Ways to Center a Div

Default to grid

display: grid; place-items: center on the parent centres one child in both directions with two declarations and nothing on the child. It is the right default for most cases.

Flexbox for groups

When several items should be centred together in a row or column, justify-content and align-items on a flex parent do it while keeping their spacing.

Absolute centring for overlays

Use top: 50%; left: 50%; transform: translate(-50%, -50%) only when the element must sit over other content, such as a play button on a video poster.

Horizontal only

To centre a block horizontally in normal flow, give it a width (or max-width) and margin-inline: auto. For text, text-align: center on the parent.

Frequently asked questions

Which method is best?

Grid with place-items for one element; flexbox for a group; absolute for overlays.

Why is my element not vertically centred?

The parent probably has no height. Centring needs space; give the parent a height or min-height.

Does align-content work without flex or grid?

In current browsers, yes, on block containers. Older browsers ignore it.