Question
How can text be centered both horizontally and vertically inside a div that is 90px wide and 90px high?
The div uses display: block and contains text. text-align: center centers the text horizontally, but vertical-align: middle does not vertically center it. What is the correct CSS approach?
Short Answer
You will learn why horizontal and vertical text alignment use different CSS properties, why vertical-align does not normally work on a block div, and how to center content reliably with Flexbox or CSS Grid.
Concept
text-align: center centers inline content—such as text and inline elements—along the horizontal axis of a block container.
Vertical centering is different because a normal block element does not have a built-in property that means “move this text to the middle vertically.” The property vertical-align is often misunderstood: it works primarily with inline-level elements, table cells, and inline-table elements. It does not vertically align ordinary content inside a normal block-level div.
For modern CSS, use a layout system such as Flexbox or Grid. These systems can align an element's contents along both axes without relying on fixed line heights or manual padding.
For a single box containing text, Flexbox is a clear and widely used solution.
Mental Model
Think of the div as a square room.
text-align: centermoves the text to the middle of the room from left to right.- It does nothing to move the text from top to bottom.
- Flexbox gives the room two alignment controls:
justify-contentpositions items along the main axis.align-itemspositions items along the cross axis.
With Flexbox's default row direction, justify-content: center centers horizontally and align-items: center centers vertically.
Syntax and Examples
Use Flexbox on the container:
.box {
width: 90px;
height: 90px;
display: flex;
justify-content: center;
align-items: center;
}
<div class="box">Hello</div>
display: flex turns the div into a flex container. Its direct content becomes a flex item.
justify-content: centercenters the item horizontally.align-items: centercenters the item vertically.
A CSS Grid alternative is even shorter:
.box {
width: 90px;
height: 90px;
display: grid;
place-items: center;
}
place-items: center centers content in both directions.
Step by Step Execution
Consider this HTML and CSS:
<div class="status">Saved</div>
.status {
width: 90px;
height: 90px;
display: flex;
justify-content: center;
align-items: center;
border: 1px solid #333;
}
What happens:
- The browser creates a
divthat is90pxwide and90pxtall. display: flexmakes.statusa flex container.- The text
Savedis wrapped in an anonymous flex item by the browser. - The default flex direction is
row, so the main axis runs horizontally. justify-content: centerplaces the text item in the horizontal center.- The cross axis runs vertically.
align-items: centerplaces the text item in the vertical center.
The text is centered even if its font size or line height changes.
Real World Use Cases
Vertical and horizontal centering is useful for many interface elements:
- Buttons: Center a label and icon inside a fixed-height button.
- Avatar placeholders: Center a user's initials in a circular profile image.
- Loading states: Place “Loading…” in the middle of a panel.
- Empty states: Center a message in a card with no search results.
- Dashboard tiles: Center a count, label, or status indicator inside a square widget.
- Modal dialogs: Center dialog content within an overlay, usually with additional sizing rules.
Real Codebase Usage
In production code, developers usually place alignment rules on the component container rather than trying to position each text node.
A reusable utility class might look like this:
.center-content {
display: flex;
align-items: center;
justify-content: center;
}
<button class="icon-button center-content" aria-label="Close">
×
</button>
For components that can contain multiple lines, icons, or dynamic content, Flexbox and Grid remain reliable:
.empty-state {
min-height: 240px;
display: grid;
place-items: center;
text-align: center;
}
text-align: center is still useful here: Grid centers the content block itself, while text-align centers lines of text inside that block.
When centering a full-page area, use a meaningful available height such as min-height: 100vh rather than assuming a fixed pixel height:
.page-message {
min-height: 100vh;
display: grid;
place-items: center;
}
Common Mistakes
Expecting vertical-align to work on a block container
This does not vertically center the text in a normal div:
.box {
width: 90px;
height: 90px;
vertical-align: middle;
}
vertical-align aligns an inline-level or table-cell element relative to nearby content. It is not a general-purpose vertical content alignment property for block elements.
Using only text-align
.box {
text-align: center;
}
This centers text horizontally only. Add Flexbox or Grid for vertical positioning.
Using line-height for content that may wrap
For one line, this older technique can appear to work:
.box {
height: 90px;
line-height: 90px;
text-align: center;
}
But it is fragile. If the text wraps to two lines, uses a different line height, or includes other elements, the layout will not be centered correctly. Prefer Flexbox or Grid.
Centering the wrong element
Alignment properties affect an element's direct children. If an inner child has a width of 100%, it may appear unchanged even though it is technically centered. In that case, use text-align: center on the child as well, or avoid giving the child unnecessary full width.
Forgetting available height
Vertical centering needs extra vertical space. If a container has no explicit height, min-height, or parent-constrained height, it may shrink to its content and leave nothing to center within.
Comparisons
| Technique | Horizontal centering | Vertical centering | Best use |
|---|---|---|---|
text-align: center | Yes, for inline content | No | Centering text lines in a block |
vertical-align: middle | No | Limited | Inline elements and table cells |
| Flexbox | Yes | Yes | Most component layouts |
Grid with place-items | Yes | Yes | Simple two-axis centering |
Matching line-height to height | Yes, with text-align | Yes, for one line | Legacy or strictly single-line text |
| Absolute positioning with transforms | Yes | Yes | Overlays or special positioned elements |
Flexbox versus Grid
Use Flexbox when the component may later need a row or column of items, such as an icon next to text.
Use Grid when the only goal is simple two-dimensional centering:
display: grid;
place-items: center;
Both are appropriate for centering text in a box.
Cheat Sheet
/* Recommended: Flexbox */
.box {
display: flex;
justify-content: center; /* horizontal in a row */
align-items: center; /* vertical in a row */
}
/* Short alternative: Grid */
.box {
display: grid;
place-items: center; /* horizontal + vertical */
}
/* Horizontal text alignment only */
.box {
text-align: center;
}
Key rules:
text-alignaligns inline text horizontally.vertical-aligndoes not vertically center text inside a normal blockdiv.- Use Flexbox or Grid for content that can wrap or change size.
- Give the container a
height,min-height, or parent-provided height if vertical centering needs visible space. - Add
text-align: centerwhen a centered child contains multiple text lines.
FAQ
Why does vertical-align: middle not work in a div?
A normal div is block-level. vertical-align is designed mainly for inline elements, inline-block elements, and table cells—not for aligning a block container's contents.
What is the easiest way to center text in a div?
Use CSS Grid:
display: grid;
place-items: center;
Or use Flexbox with justify-content: center and align-items: center.
Does Flexbox center multiline text too?
Yes. Flexbox centers the text block as an item. Add text-align: center if you also want each wrapped line centered.
Should I use line-height equal to the div height?
Only for guaranteed single-line text in a fixed-height box. It is less flexible than Flexbox or Grid and breaks down when text wraps.
Which Flexbox property controls vertical alignment?
With the default flex-direction: row, align-items: center controls vertical alignment. If you change to flex-direction: column, the axes switch.
Can I center an icon and text together?
Yes. Put both in the flex container. They will be centered as a group. Use gap to create spacing between them.
.button {
display: flex;
justify-content: center;
align-items: center;
gap: 0.5rem;
}
Mini Project
Description
Create a reusable status badge that centers a short status label inside a fixed square. This mirrors UI elements such as connection indicators, dashboard tiles, and compact action buttons.
Goal
Build a 90px square status badge with text centered horizontally and vertically using Flexbox.
Requirements
Use a div containing a status label.
Make the badge 90px wide and 90px high.
Center the label on both axes with Flexbox.
Add a border and background color so the badge boundaries are visible.
Ensure a longer label can wrap while remaining centered.
Keep learning
Related questions
Add a Favicon to a Static HTML Page
Learn how to add a favicon to static HTML pages, use correct paths and icon formats, and troubleshoot browser caching and local-file issues.
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Apply CSS !important with jQuery .css()
Learn why jQuery .css() cannot set !important and how to use CSSStyleDeclaration.setProperty() to apply inline important styles safely.