visit
In another article, I've already covered how to center an HTML element both horizontally and vertically using CSS. If you want to learn about that,
Horizontally and vertically centering something with Tailwind is easy with flexbox
. All we have to do is three things:
h-screen
to make the element the height of the screenflex
to make the element a flexbox
items-center
to vertically center itjustify-center
to center it horizontally
Our code then looks like this:
<div class="h-screen flex items-center justify-center">
Horizontally and Vertically Centered Element
</div>
See the Pen
The easiest way to vertically center something is to do what we did before, and use flexbox
. This time, though, we'll remove justify-center
. The item will then be centered only along the vertical axis.
<div class="h-screen flex items-center">
Vertically Centered Element
</div>
You can see an example of this in the codepen below:
See the Pen .
In the same way that we vertically centered the div above, we can also horizontally center using our previous approach. The code for that looks like this:
<div class="flex justify-center items-center">
Horizontally Centered Element
</div>
This is the same code we used for horizontal and vertical centering, just without h-screen
. Here's the result:
To learn more about CSS in general, you can