How do I center a div both vertically and horizontally with TailwindCSS?

clock icon

asked 404 days ago

message icon

1

eye icon

12

I want a <div> to sit exactly in the center of the page, regardless of screen size. How can I achieve this using TailwindCSS utility classes?

1 Answer

Wrap it in a container that’s h-screen and use flexbox centering:

1htmlCopyEdit<div class="h-screen flex items-center justify-center">
2 <div class="bg-gray-100 p-6 rounded shadow">
3 <!-- Your content -->
4 </div>
5</div>
6
1htmlCopyEdit<div class="h-screen flex items-center justify-center">
2 <div class="bg-gray-100 p-6 rounded shadow">
3 <!-- Your content -->
4 </div>
5</div>
6
  • h-screen makes the outer div full viewport height.
  • flex items-center justify-center centers children vertically & horizontally.

1

Write your answer here

Top Questions