Ask any question about Tailwind CSS here... and get an instant response.
How do I center a div both vertically and horizontally using Tailwind classes?
Asked on Nov 26, 2025
Answer
To center a div both vertically and horizontally in Tailwind CSS, you can use flexbox utilities. By setting the parent container to use `flex`, and then applying `justify-center` and `items-center`, you can achieve perfect centering.
<!-- BEGIN COPY / PASTE -->
<div class="flex items-center justify-center h-screen">
<div class="bg-blue-500 text-white p-4">
Centered Content
</div>
</div>
<!-- END COPY / PASTE -->Additional Comment:
- The `flex` class makes the container a flexbox.
- `justify-center` centers content horizontally.
- `items-center` centers content vertically.
- `h-screen` makes the container take the full height of the viewport.
Recommended Links:
