How do I center a div? (the definitive 2026 answer)

poze 3 months ago91 gadeen

5

I know it's a meme, but I genuinely keep forgetting. Horizontally, vertically, both. What's the modern way?

Dyagnostik IA

Se IA ki fè l. Se pa yon repons — kominote a anba a konfime oswa korije l. Toujou verifye anvan ou fye l.

poze 3 months ago

1 Repons

6
✓
✓ Repons yo aksepte

Both axes, inside a parent:

.parent { display: grid; place-items: center; }

Flexbox equivalent:

.parent { display: flex; justify-content: center; align-items: center; }

Horizontally only, block element with a width:

.box { max-width: 600px; margin-inline: auto; }

Inline or text content: text-align: center; on the parent.

Over everything (modal):

.modal { position: fixed; inset: 0; margin: auto; width: fit-content; height: fit-content; }

For vertical centering, remember the parent needs a height (e.g. min-height: 100dvh for a full screen). dvh accounts for mobile browser toolbars, unlike vh.

Konekte pou di lòt moun si li te mache.

answered 3 months ago

Sign in and verify your email to post an answer.