How to center a div in TailwindCSS
Written by Ted
The most difficult question in development... how to center a div.
Well, don't worry, because it is very simple in TailwindCSS - and you have a choice of which ever works best for you.
Method 1
We can use grid to center a div.
<div class="grid h-screen place-items-center">
<p>I'm centered!</p>
</div>
Let's break down what is happening here:
gridgives the div thedisplay: gridCSS property.place-items-centergives thecentervalue inplace itemsCSS property.h-screenensures that the height is the full screen,100vh.
Method 2
We can use flexbox to center a div.
<div class="flex items-center justify-center h-screen">
<p>I'm centered!</p>
</div>
Let's break down what is happening here:
flexgives the div thedisplay: flexCSS property.justify-centercenters the div horizontally.items-centercenters the div vertically.h-screenensures that the height is the full screen,100vh.
That's all!
Hope this blog post was useful.
