Tailwind CSS・Learning how to write it

Building Flexbox and Grid with classes

display: flex is flex; justify-content: center is justify-center. A lookup table you can convert your CSS layouts with directly, plus the go-to recipes for headers, card lists and sidebars.

Layout is where Tailwind’s benefit is easiest to see. You can convert mechanically from the CSS property names you already know, so there’s almost nothing new to learn.

TipsFlexbox cheat sheetTo refresh Flexbox itself, the cheatsheet with live demos

The Flexbox lookup table

Write flex on the parent. That’s display: flex. Then add from here.

TailwindPlain CSS
flexdisplay: flex
flex-colflex-direction: column
flex-row-reverseflex-direction: row-reverse
flex-wrapflex-wrap: wrap
justify-startjustify-content: flex-start
justify-centerjustify-content: center
justify-endjustify-content: flex-end
justify-betweenjustify-content: space-between
justify-aroundjustify-content: space-around
justify-evenlyjustify-content: space-evenly
items-startalign-items: flex-start
items-centeralign-items: center
items-endalign-items: flex-end
items-stretchalign-items: stretch
gap-4gap: 1rem
flex-1flex: 1 1 0%
shrink-0flex-shrink: 0
self-endalign-self: flex-end
order-firstorder: -9999

justify-content became justify-, align-items became items-. The flex in flex-start drops off too, giving the short justify-start.

The Grid lookup table

TailwindPlain CSS
griddisplay: grid
grid-cols-3grid-template-columns: repeat(3, minmax(0, 1fr))
grid-cols-nonegrid-template-columns: none
grid-rows-2grid-template-rows: repeat(2, minmax(0, 1fr))
col-span-2grid-column: span 2 / span 2
col-span-fullgrid-column: 1 / -1
row-span-2grid-row: span 2 / span 2
gap-4gap: 1rem
gap-x-4 / gap-y-2column-gap / row-gap
place-items-centerplace-items: center

grid-cols-3 means “three equal columns” by default. It’s plain CSS’s repeat(3, 1fr), so the usual card list is covered.

TipsCSS Grid cheatsheetTo refresh Grid itself, the cheatsheet with live demos

The classic recipes, side by side

Header: logo left, menu right

/* plain CSS */
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}
<!-- Tailwind -->
<header class="flex justify-between items-center p-4">
  <p class="font-bold">Shima's page</p>
  <nav class="flex gap-4">
    <a href="#about">About</a>
    <a href="#works">Work</a>
  </nav>
</header>
flex justify-between items-center—those three settle a header’s skeleton.

A card list (three columns)

/* plain CSS */
.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}
<!-- Tailwind -->
<div class="grid grid-cols-3 gap-4">
  <div class="p-4 bg-white rounded-xl">1</div>
  <div class="p-4 bg-white rounded-xl">2</div>
  <div class="p-4 bg-white rounded-xl">3</div>
</div>

Making that one column on phones is the next lesson.

/* plain CSS */
.layout { display: flex; gap: 1.5rem; }
.sidebar { flex: 0 0 240px; }
.main { flex: 1; }
<!-- Tailwind -->
<div class="flex gap-6">
  <aside class="w-60 shrink-0">Sidebar</aside>
  <main class="flex-1">Content</main>
</div>

w-60 is 240px (60 × 4) and shrink-0 means “don’t shrink”—plain CSS’s flex: 0 0 240px split across two classes.

Dead centre

/* plain CSS */
.center {
  display: grid;
  place-items: center;
  min-height: 100vh;
}
<!-- Tailwind -->
<div class="grid place-items-center min-h-screen">
  <p>Centre</p>
</div>

min-h-screen is min-height: 100vh. Full screen height is h-screen or min-h-screen—both come up often enough to memorise.

Push just one item to the right

<div class="flex gap-4">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#" class="ml-auto">Log in</a>
</div>

ml-auto is margin-left: auto. The plain-CSS trick of “auto on one side pushes it away” works exactly the same here.

Width and height classes

Just the ones layout work needs.

TailwindPlain CSS
w-fullwidth: 100%
w-1/2width: 50%
w-1/3 w-2/3width: 33.333% 66.667%
w-60width: 15rem (240px)
max-w-4xlmax-width: 56rem (896px)
h-screenheight: 100vh
min-h-screenmin-height: 100vh
mx-automargin-left/right: auto

Capping the content width and centring it—the thing every site does—is these two together.

<div class="max-w-4xl mx-auto px-4">
  content here
</div>

The same shape as plain CSS’s “max-width for the cap, margin-inline: auto for centring, padding for breathing room.”

Summary of this lesson

  1. It starts by writing flex or gridon the parent, same as plain CSS
  2. justify-content became justify-, align-items became items-just shorter
  3. grid-cols-3 is three equal columns, with overflow-resistant minmax(0, 1fr) by default
  4. max-w-4xl mx-auto is the go-to combination for capping and centring content
  5. Small tricks like ml-auto and shrink-0 work on the same logic as plain CSS

Next we add the md: prefix to all of this and switch by screen width.

FAQ

How do I use Flexbox in Tailwind?
Write flex on the parent. That one class is display: flex. From there you add classes taken from the front of the CSS property names, like justify-center and items-center.
Which is justify-center and which is items-center?
The same relationship as plain CSS. justify- is the direction you're laying out in; items- is at right angles to it. Laid out horizontally (the default), justify- handles horizontal and items- handles vertical. Only the names got shorter.
How do I set the number of Grid columns?
Write grid grid-cols-3. grid is display: grid and grid-cols-3 is grid-template-columns: repeat(3, minmax(0, 1fr)). Equal 1fr columns are the default, so that covers the common case.
Where does gap go?
On the parent, same as plain CSS. gap-4 is 16px. You can split the axes with gap-x-4 and gap-y-2.