Every common way to centre something with Tailwind, each labelled with its classes: flexbox, grid with place-items-center, absolute positioning with translate, m-auto inside a flex parent, inset-0 with m-auto and a centred block of text.
The everyday answer: flex items-center justify-center on the parent centres a child on both axes. It also centres several children as a group, and the parent just needs a height.
The shortest answer: grid place-items-center. One class on the parent centres the child horizontally and vertically, and it works the same for text, images and whole cards.
For overlays such as a play button over an image: absolute left-1/2 top-1/2 plus -translate-x-1/2 -translate-y-1/2. The child leaves the layout, so it can sit on top of other content.
Inside a flex or grid parent, m-auto on the child takes up all the free space on every side, which centres it. Handy when only one child should be centred while its siblings keep their place.
An absolutely positioned box with a fixed size, inset-0 and m-auto centres itself without transforms. It works because the browser shares the leftover space equally between the auto margins.
Centring a paragraph is two jobs: text-center for the lines and mx-auto with a max width for the block. text-balance evens out the line lengths so the centred shape looks tidy.
View code
<div class="w-full max-w-xs rounded-xl bg-white p-6 text-center">
<h3 class="mx-auto max-w-[16ch] text-balance text-2xl font-bold text-slate-900">Everything you need to launch</h3>
<p class="mx-auto mt-2 max-w-[28ch] text-balance text-sm text-slate-600">One plan, every feature, and help from real people when you get stuck.</p>
</div>
About these Center a Div in Tailwind
Pick grid or flex by default
grid place-items-center is the shortest; flex items-center justify-center is best when several children should sit together in the middle.
Absolute for overlays
absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 centres an element over something else, such as a play button on a video.
Auto margins
In a flex or grid parent, m-auto pushes a single child into the middle while its siblings stay where they are.
Text is its own job
text-center centres lines; mx-auto with a max width centres the block itself.
Frequently asked questions
Why isn't my div centred vertically?
The parent needs a height. Without one, there is no space to centre within.
Which method is fastest?
All are equally fast; choose the one that fits the layout.