Vertical alignment has historically been one of the trickier layout tasks in CSS. But with Tailwind CSS, vertically aligning a <div>
across the full height of the screen is clean, fast, and reliable β thanks to its utility-first classes.
In this blog, you’ll learn how to align a <div>
vertically across the full screen using different methods in Tailwind CSS, including when and why to use each approach.
π― Goal: Vertically Center a <div>
Within the Full Viewport Height
Imagine you want this:
+-----------------------------+
| |
| [ Content ] |
| |
+-----------------------------+
This means:
- The
<div>
appears in the vertical center of the screen. - It adjusts responsively.
- It may or may not be horizontally centered β that part is optional.
β
Method 1: Flexbox (flex
+ items-center
+ h-screen
)
Best For: Full-screen layouts, hero sections, centered content.
π§© Example:
<div class="flex items-center h-screen bg-gray-100">
<div class="mx-auto bg-white p-6 rounded shadow">
<h1 class="text-xl font-bold">Vertically Centered Content</h1>
</div>
</div>
π Explanation:
h-screen
: Makes the parent div the full height of the viewport.flex
: Activates Flexbox layout.items-center
: Vertically centers the child content.mx-auto
: (Optional) Horizontally centers the inner<div>
.
β Result: The inner box is vertically centered across the full screen.
β
Method 2: CSS Grid (grid
+ place-items-center
)
Best For: Single child elements that need both horizontal and vertical centering.
π§© Example:
<div class="grid place-items-center h-screen bg-blue-50">
<div class="bg-blue-600 text-white p-8 rounded">
Centered with Grid
</div>
</div>
π Explanation:
grid
: Uses CSS Grid layout.place-items-center
: Centers the content both vertically and horizontally.h-screen
: Makes the grid full height of the screen.
Grid is especially helpful when you want concise centering syntax for a single element.
β Method 3: Fixed or Absolute Position + Translate
Best For: Floating elements (modals, alerts) that should stay centered regardless of content height.
π§© Example:
<div class="fixed top-1/2 left-1/2 -translate-y-1/2 bg-white p-6 rounded shadow">
Vertically Centered Fixed Box
</div>
π Explanation:
top-1/2
: Positions the top of the element at 50% of the viewport.-translate-y-1/2
: Pulls the element up by half its own height.fixed
: Keeps the element anchored even when scrolling.
You can also use absolute
for positioning relative to a parent container with relative
.
β Method 4: Vertically Align Only (Without Horizontal Centering)
If you only want to vertically align a div (not center it horizontally), skip the justify-center
or mx-auto
.
<div class="flex items-center h-screen">
<div class="bg-red-100 p-4">
Only Vertically Centered
</div>
</div>
This keeps your layout aligned to the top, left, or anywhere else, while still centering vertically.
β οΈ Common Mistakes to Avoid
- β Forgetting
h-screen
: Without a full-height container, vertical alignment wonβt work. - β Applying
items-center
withoutflex
orgrid
: The alignment wonβt take effect. - β Not using
translate-y-1/2
withtop-1/2
: Your element will appear off-center.
β Summary Table
Method | Use Case | Key Classes |
---|---|---|
Flexbox | Full-screen layout | flex items-center h-screen |
Grid | Clean centering for a single element | grid place-items-center h-screen |
Translate | For modals, overlays | fixed top-1/2 -translate-y-1/2 |
Vertical only | When you want left-aligned but vertically centered | flex items-center h-screen without horizontal centering |
π¬ Final Thoughts
Tailwind CSS makes vertical alignment easy β whether you’re using Flexbox, Grid, or transforms. Just remember to set the correct height context (h-screen
) and choose the method that best fits your layout structure.