Lexington has been awarded a grant from Astro, to celebrate. Get a 30% discount. Apply code LEXINGTON30 at checkout.

← Back to all tutorials

Learn how to center a div with Tailwind CSS

Published and written on Aug 11 2023 by Michael Andreuzza

Centering elements is a common challenge in web design, and with the rise of responsive layouts, achieving perfect alignment across devices has become more crucial than ever. Tailwind CSS, a utility-first CSS framework, offers an efficient way to handle this task without delving into complex CSS code. In this blog post, we’ll guide you through various techniques to center a

element using Tailwind CSS.

1. Centering Horizontally

To horizontally center a

element using Tailwind CSS, you can combine the flex and justify-center classes. The flex class creates a flex container, and the justify-center class horizontally aligns the content within it.

<div class="flex justify-center">
  <!-- Your content here -->
</div>

2. Centering Vertically

For vertical centering, you can use the flex class along with the items-center class. The items-center class vertically aligns the content within the flex container.

<div class="flex items-center">
  <!-- Your content here -->
</div>

3. Centering Both Horizontally and Vertically

Combining the techniques from above will allow you to center the content both horizontally and vertically within the

element.

<div class="flex justify-center items-center">
  <!-- Your content here -->
</div>

4. Absolute Centering

In some cases, you might want to center a

element absolutely within its parent container. You can achieve this by using the absolute class along with the inset-0 class for positioning and the flex and items-center classes for alignment.

<div class="relative">
  <div class="absolute inset-0 flex justify-center items-center">
    <!-- Your content here -->
  </div>
</div>

5. Centering with Margin

If you prefer using margins for centering, Tailwind CSS provides utility classes to help. To horizontally center, use mx-auto, and to vertically center, use my-auto.

<div class="mx-auto my-auto">
  <!-- Your content here -->
</div>

6. Centering Text

Centering text within a

element is also straightforward with Tailwind CSS. Apply the text-center class to the
containing the text.

<div class="text-center">
  <!-- Your text here -->
</div>

In conclusion, Tailwind CSS simplifies the process of centering

elements and their content with its intuitive utility classes. Whether you’re aiming for horizontal, vertical, or absolute centering, Tailwind CSS offers a range of techniques to achieve your desired layout. Experiment with these methods to find the best fit for your project’s design and responsive requirements. Happy coding!

Remember to refer to the official Tailwind CSS documentation for a comprehensive list of utility classes and customization options. With practice, you’ll master the art of centering elements using Tailwind CSS and enhance your web design skills.

/Michael Andreuzza

Did you like this tutorial? Please share it with your friends!

Get access to all themes

Unlock all themes for $199 for forever! Includes lifetime updates, new themes, unlimited projects, and support
— No subscription needed.