DEV Community

Arpita Roy
Arpita Roy

Posted on

1

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).

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more