What CSS centering really means
“Centering” sounds like one task, but it’s actually a few different ones:
- Center text horizontally inside a box.
- Center an element horizontally within its parent.
- Center an element vertically within its parent.
- Center both horizontally and vertically (the classic “perfect center”).
- Center text vertically (single-line vs multi-line matters a lot).
The best method depends on what you’re centering and what the layout is doing (static content, unknown sizes, responsive containers, etc.). Let’s learn the main tools, then you’ll pick the one that fits the situation.
CSS center text
The most common kind of centering is centering text horizontally inside a container. For that, you usually
want text-align.
.box { text-align: center; }
.box { text-align: left; }
.box { text-align: right; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .box { border: 3px solid #111; padding: 18px; border-radius: 12px; background: #f4f4f4; } .box h3 { margin: 0 0 8px; font-size: 18px; } .box p { margin: 0; line-height: 1.4; }
Centered text (horizontal)
text-align affects inline content like text, links, and inline elements.
When text-align does (and doesn’t) help
- Works well for: text, inline elements, inline-block elements inside the container.
- Does not center the container itself. It centers the content inside it.
CSS center div / CSS center element / CSS center align
Centering an element horizontally usually means: “I want this element to sit in the middle of its parent.” The
classic approach for a block element is margin-left: auto; and margin-right: auto;. A more modern way that does exactly the same is margin-inline: auto;
.card { margin-left: 0; margin-right: 0; width: 320px; }
.card { margin-left: auto; margin-right: auto; width: 320px;}
.card { margin-inline: auto; width: 320px; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .frame { border: 3px dashed #111; padding: 18px; border-radius: 12px; background: #fbfbfb; } .card { border: 3px solid #111; border-radius: 12px; background: #f4f4f4; padding: 16px; } .card h4 { margin: 0 0 8px; font-size: 16px; } .card p { margin: 0; line-height: 1.4; }
Centered block element
Auto margins center me as long as I have a width.
Important: auto margins need a width
A block element (like a div) is normally width: auto, which effectively becomes “fill the
available space”. If it already fills the parent, there’s nothing to “center” horizontally.
- To use
margin-inline: auto;you typically set a width or max-width. - Often the best pattern is:
max-width+margin-inline: auto;.
.container { max-width: 340px; margin-inline: auto; }
*, ::before, ::after { box-sizing: border-box; } .page { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .container { border: 3px solid #111; border-radius: 12px; background: #f4f4f4; padding: 16px; } .container p { margin: 0; line-height: 1.4; }
This is a centered layout container: it stays centered, and it stops growing after max-width.
CSS center image
Images are a little special because <img> is an inline element by default. Your centering method
depends on whether you want to center the image itself or its container.
Center an image with text-align
If your image is inline, you can center it like text by applying text-align: center; on the parent.
.figure { text-align: center; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .figure { border: 3px solid #111; border-radius: 12px; padding: 18px; background: #f4f4f4; } .figure img { width: 180px; height: 120px; border-radius: 10px; border: 2px solid #111; }
Center an image with margin: auto
A super common modern approach is: make the image a block element, then use auto margins.
.figure img { display: block; margin-inline: auto; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .figure { border: 3px solid #111; border-radius: 12px; padding: 18px; background: #f4f4f4; } .figure img { width: 200px; height: 130px; border-radius: 10px; border: 2px solid #111; }
CSS center vertically
Vertical centering is where people start bargaining with CSS like it’s a mystical forest spirit. The good news: modern CSS has excellent options, and you only need to pick the right one for the job.
Vertical centering with Flexbox
Flexbox is perfect when you want to align items along one axis (or both). To center vertically, set
align-items: center; on the flex container. To center horizontally too, add
justify-content: center;.
.stage { display: flex; align-items: start; justify-content: start; }
.stage { display: flex; align-items: center; justify-content: start; }
.stage { display: flex; align-items: center; justify-content: center; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .stage { height: 220px; border: 3px solid #111; border-radius: 12px; background: #f4f4f4; padding: 14px; } .box { width: 160px; border: 3px solid #111; border-radius: 12px; background: #fff; padding: 12px; } .box p { margin: 0; line-height: 1.3; }
I want to be centered.
Vertical centering with Grid
Grid can center things in one line: place-items: center;. It’s one of the cleanest “perfect center”
solutions.
.stage { display: grid; place-items: start; }
.stage { display: grid; place-items: center; }
.stage { display: grid; place-items: end; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .stage { height: 320px; border: 3px solid #111; border-radius: 12px; background: #f4f4f4; padding: 14px; } .box { width: 170px; border: 3px solid #111; border-radius: 12px; background: #fff; padding: 12px; }
Grid centering is suspiciously easy.
Vertical centering with absolute + transform
This is the classic “unknown size” trick. You position the element at 50% / 50% of the parent, then pull it back by
half its own size using transform: translate(-50%, -50%).
.stage { position: relative; }
.modal {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .stage { height: 290px; border: 3px solid #111; border-radius: 12px; background: #f4f4f4; } .modal { width: 220px; border: 1px solid #111; background: #fff; padding: 14px; overflow: hidden;
resize: both;} .modal p { margin: 0; line-height: 1.4; }
Absolute + translate centers me even if my size changes. Try resizing me.
When to use absolute + transform
- Great for: modals, popovers, floating UI, “overlay” elements.
- Not ideal for: normal page flow layouts (it removes the element from document flow).
CSS center text vertically
Vertical centering of text depends on whether the text is a single line or multiple lines. The method changes because lines wrap, and wrapped text has height you can’t pretend isn’t there.
Single-line vertical centering with line-height
If you have one line of text in a fixed-height element, you can set line-height equal
to the element’s height. This centers the text vertically (because the line box becomes as tall as the container).
.pill { height: 64px; line-height: 64px; text-align: center; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .pill { border: 3px solid #111; border-radius: 999px; background: #f4f4f4; width: 320px; margin-inline: auto; font-size: 18px; }
One line only
A tiny warning
If the text wraps to multiple lines, line-height won’t “center the paragraph.” It just changes spacing
between lines.
Multi-line vertical centering: use Flex or Grid
For multi-line text, treat it like content inside a container and use Flexbox or Grid. This works whether it’s one line or fifteen.
.panel { display: flex; align-items: center; justify-content: center; text-align: center; }
.panel { display: grid; place-items: center; text-align: center; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .panel { height: 220px; border: 3px solid #111; border-radius: 12px; background: #f4f4f4; padding: 18px; } .panel p { margin: 0; max-width: 36ch; line-height: 1.4; }
This text can wrap to multiple lines, and it still stays centered both vertically and horizontally.
vertical-align: middle (the old-school tool)
There is no vertical-align: center; value. But vertical-align is useful in
specific cases: inline/inline-block elements and table-cell layouts.
Vertical-align with inline-block elements
If you have two inline-block elements next to each other, you can align them vertically with
vertical-align: middle;. This is handy for an icon next to text.
.icon, .label { display: inline-block; vertical-align: middle; }
.icon, .label { display: inline-block; vertical-align: top; }
.icon, .label { display: inline-block; vertical-align: bottom; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .row { border: 3px solid #111; border-radius: 12px; background: #f4f4f4; padding: 16px; } .icon { width: 52px; height: 52px; border: 3px solid #111; border-radius: 12px; background: #fff; } .label { margin-left: 10px; font-size: 18px; line-height: 1.2; max-width: 28ch; }
Icon next to text (watch how vertical-align affects them)
Table-cell vertical centering
Before Flexbox and Grid, one reliable trick was using display: table-cell; and
vertical-align: middle;. It still works, but you’ll usually reach for Flex or Grid first today.
.cell { display: table-cell; vertical-align: middle; text-align: center; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .table { display: table; width: 100%; height: 220px; border: 3px solid #111; border-radius: 12px; background: #f4f4f4; } .cell { border-radius: 12px; font-size: 18px; padding: 18px; }
Table-cell centering still works.
Flexbox centering cheat sheet
display: flex;turns the parent into a flex container.justify-contentaligns items along the main axis (row by default).align-itemsaligns items along the cross axis.
For the common “center both ways”: display: flex; + justify-content: center; +
align-items: center;.
Grid centering cheat sheet
display: grid;turns the parent into a grid container.place-items: center;is basicallyalign-items+justify-itemsat once.
For the common “center both ways”: display: grid; + place-items: center;.
Interactive playground
Let’s build a centering playground where you can tweak padding on each side (four sliders), while the element stays centered using Grid. This is a very real-world combo: a centered card that breathes more or less depending on spacing.
.stage { display: grid; place-items: center; } .card { padding: 18px 18px 18px 18px; }
*, ::before, ::after { box-sizing: border-box; } .wrap { padding: 18px; font-family: ui-monospace, SFMono-Regular, monospace; } .stage { height: 280px; border: 3px solid #111; border-radius: 12px; background: #f4f4f4; } .card { width: min(410px, 90%); border: 3px solid #111; border-radius: 14px; background: #fff; } .card h3 { margin: 0 0 10px; font-size: 18px; } .card p { margin: 0; line-height: 1.4; }
Centered card
Drag the padding sliders. The card stays centered while its spacing changes.
Common centering gotchas
- Auto margins don’t work without a width. If your element is full-width, it’s already “centered” because it fills the parent.
- Vertical centering needs a height. If the parent has no height (or no reason to be tall), there’s nothing to center within.
- Absolute centering removes the element from flow. Great for overlays, not great for normal document layouts.
Quick: pick the right centering method
- Center text horizontally:
text-align: center; - Center a block horizontally:
margin-inline: auto;+ width or max-width - Center an image: parent
text-align: center;or imagedisplay: block; margin-inline: auto; - Center vertically (and/or both): Flexbox or Grid
- Overlay perfect center:
position: absolute;+top/left: 50%;+transform: translate(-50%, -50%); - Inline icon + text alignment:
display: inline-block;+vertical-align: middle;
Conclusion
Centering in CSS isn’t hard once you stop looking for one magic spell and start picking the right tool: text-align for inline content, auto margins for blocks, and Flexbox/Grid for modern layout centering. Keep the “absolute + translate” trick in your toolbox for overlays, and you’re ready to center.
To learn even more about CSS Alignments, check out our CSS Align Interactive Tutorial.
