Centering a Div with Tailwind CSS

devarpita

Arpita Roy

Posted on November 2, 2023

Centering a Div with Tailwind CSS

To center a div both horizontally and vertically, you can use both Flexbox or grid. First, we must assign a height to the div, as a div's default height is zero.

Here's an example with grid:

<div class="grid place-items-center bg-gray-400 h-screen ">
    <div>
        Hello world!
    </div>
</div>

Enter fullscreen mode Exit fullscreen mode

In this example, grid makes the container a grid container. Use place-items-center to place grid items on the center of their grid areas on both axes.h-screen makes the height of the container 100% of the viewport height (100vh).

Here's an example with flexbox:

<div class="flex justify-center items-center h-screen">
  <div>
    Your Content
  </div>
</div>

Enter fullscreen mode Exit fullscreen mode

In this example, flex makes the container a flex container. justify-center and items-center center the child div along both the main axis and the cross axis. h-screen makes the height of the container 100% of the viewport height (100vh).

💖 💪 🙅 🚩
devarpita
Arpita Roy

Posted on November 2, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related