Tailwind CSS Q&A Logo
Tailwind CSS Q&A Part of the Q&A Network
Q&A Logo

How do I center a div both vertically and horizontally using Tailwind?

Asked on Sep 28, 2025

Answer

To center a div both vertically and horizontally using Tailwind CSS, you can utilize the flexbox utilities. By setting the parent container to "flex" and using "justify-center" and "items-center", you can achieve the desired centering effect.
<!-- 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 parent a flex container.
  • "items-center" vertically centers the content.
  • "justify-center" horizontally centers the content.
  • "h-screen" sets the height of the parent to the full viewport height, ensuring vertical centering.
✅ Answered with Tailwind CSS best practices.

← Back to All Questions
The Q&A Network