Centering Elements with CSS
Centering Elements with CSS
Centering depends on what is being centered: inline text, a constrained block, or items in a two-axis layout.
What you’ll learn
- Center inline content with text-align.
- Center a block with auto margins.
- Center both axes using a modern layout method.
Understand the concept
text-align:center centers inline content inside a block. A block with a constrained width can be centered horizontally using margin-inline:auto. For both horizontal and vertical centering, CSS Grid with place-items:center or Flexbox with justify-content and align-items is usually clearer.
Choose the simplest rule for your goal. Centering a box does not necessarily center its text, and centering its text does not necessarily center the box.
What the browser evaluates
The centering strategy depends on whether the target is inline content, a block box, or an item inside a layout container.
Center a sized card
Use logical inline auto margins to center the card itself.
HTML
<div class="centered-card"><h3>Saved</h3><p>Your draft is ready.</p></div>CSS
.centered-card { max-width: 220px; margin-inline: auto; padding: 16px; background: #ccfbf1; border-radius: 10px; }
.centered-card h3 { margin: 0 0 5px; }
.centered-card p { margin: 0; }Rendered output
Saved
Your draft is ready.
Observe: The card box is centered within the preview; its inner text remains left-aligned.
Center content in both axes
Use a grid parent for a single concise status message.
HTML
<div class="center-stage"><div class="center-label">Loading complete</div></div>CSS
.center-stage { display: grid; place-items: center; min-height: 115px; max-width: 350px; background: #eff6ff; border: 1px dashed #60a5fa; border-radius: 10px; }
.center-label { padding: 10px 16px; background: #1d4ed8; color: #fff; border-radius: 999px; }Rendered output
Observe: The label sits near the middle of the dashed region on both axes.
Explain the result
The centering strategy depends on whether the target is inline content, a block box, or an item inside a layout container.
Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.
Common mistakes to avoid
- Auto inline margins center a block only when there is leftover inline space to distribute.
- text-align:center will not make an arbitrary positioned block move to the center of its parent.
Try it yourself
Change place-items:center to place-items:start and compare. Then center just the card heading with text-align:center.
Before changing the code, predict what will change. Then verify by checking the rendered result.
Check your understanding
What you should remember
Always identify the element to center and which axes matter before picking the CSS technique.
Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.
Reaching the recap and completing the quizzes are separate milestones.