Skip to main content

Last updated: March 13, 2026

How to Center a Div in Tailwind CSS (5 Methods)

The complete guide to centering elements in Tailwind CSS. Live demos, copy-paste code, and when to use each method.

Quick Reference

Flexbox (Best)

flex justify-center items-center

Grid (Shortest)

grid place-items-center

Horizontal Only

mx-auto (+ width)

All methods require the parent to have a defined height for vertical centering (e.g., h-screen, h-full, h-64).

1

Flexbox (Recommended)

Most Common

The go-to method for centering in Tailwind. Use flex with justify-center and items-center to center both horizontally and vertically.

Live Preview

Centered

Tailwind Classes

Click to copy

Key Classes:

flexjustify-centeritems-center

When to use this method:

  • Centering content in any container
  • When you need both horizontal and vertical centering
  • For centering multiple items in a row or column
  • When building navigation or card layouts
2

Grid with place-items-center

Shortest Code

The most concise way to center a div. Grid's place-items-center handles both axes in a single class.

Live Preview

Centered

Tailwind Classes

Click to copy

Key Classes:

gridplace-items-center

When to use this method:

  • When you want the shortest possible code
  • Centering a single element
  • When you're already using CSS Grid
  • Perfect for hero sections and modals
3

Grid with place-content-center

Group Centering

Centers all content as a group. Unlike place-items-center, this keeps multiple elements together rather than centering each individually.

Live Preview

Line 1
Line 2

Tailwind Classes

Click to copy

Key Classes:

gridplace-content-center

When to use this method:

  • When centering multiple elements as a group
  • For text blocks that should stay together
  • When you need centered content to flow naturally
  • Perfect for centered forms or card content
4

Absolute + Transform

Overlay Centering

Perfect for overlays, modals, and popups. Uses absolute positioning with negative translate to achieve pixel-perfect centering.

Live Preview

Centered

Tailwind Classes

Click to copy

Key Classes:

absolutetop-1/2left-1/2-translate-x-1/2-translate-y-1/2

When to use this method:

  • For modals and popups
  • When element needs to be removed from document flow
  • For overlays on images or videos
  • When Flexbox/Grid might affect other elements
5

Margin Auto (Horizontal Only)

Horizontal Only

The classic CSS centering technique. Uses mx-auto to center a block element horizontally. Requires a fixed width.

Live Preview

Centered horizontally

Tailwind Classes

Click to copy

Key Classes:

mx-auto

When to use this method:

  • For horizontal centering only
  • When element has a fixed or max width
  • For centering containers or cards
  • Classic approach that works everywhere

Method Comparison

MethodHorizontalVerticalCode LengthBest For
FlexboxYesYes3 classesGeneral purpose, most layouts
Grid place-itemsYesYes2 classesSingle element, minimal code
Grid place-contentYesYes2 classesMultiple elements as group
Absolute + TransformYesYes5 classesOverlays, modals, popups
Margin AutoYesNo2 classesHorizontal only, containers

Common Mistakes to Avoid

1.

Forgetting the height

Vertical centering requires the parent to have a defined height. Without h-screen, h-full, or a specific height, there's nothing to center within.

2.

Using text-center for div centering

text-center only centers inline content (text, images). To center a div, use Flexbox, Grid, or margin auto.

3.

Missing the relative parent for absolute

When using absolute centering, the parent must have relative. Otherwise, positioning is relative to the viewport.

4.

Forgetting width for mx-auto

mx-auto requires the element to have a width. Block elements are 100% wide by default, so add w-fit or a specific width.

Copy-Paste Snippets

Ready-to-use centering patterns. Click to copy.

Frequently Asked Questions

What's the shortest Tailwind code to center a div?

The shortest way is using Grid: <div class="grid place-items-center">. This single utility class (plus a height) centers content both horizontally and vertically. It's more concise than the Flexbox approach which requires two classes (justify-center items-center).

How do I center vertically in Tailwind?

To center vertically in Tailwind, use either Flexbox with items-center (flex items-center) or Grid with place-items-center. Make sure the parent container has a defined height (h-screen, h-full, or a specific height like h-64). Without a height, there's nothing to center within.

What's the difference between place-items and place-content?

place-items-center centers each child element individually within its grid cell. place-content-center centers all children as a group in the center of the container. Use place-items for a grid of centered items, and place-content when you want multiple elements to stay together as a centered block.

How do I center text vs center a div in Tailwind?

To center text inside a div, use text-center. To center a div itself within its parent, use Flexbox (flex justify-center items-center), Grid (grid place-items-center), or margin auto (mx-auto with a width). text-center only affects inline content like text and images, not block-level positioning.

Can I use inset-0 for centering?

Yes! inset-0 (which sets top, right, bottom, left to 0) combined with m-auto and a fixed size creates centering: <div class="absolute inset-0 m-auto w-48 h-24">. This technique works because margin: auto on an absolutely positioned element with inset-0 distributes the remaining space equally. It's an alternative to the translate method.