Skip to content

Lesson 36 of 52Tailwind CSS

Tailwind Layout and States

Flex and grid utilities, responsive prefixes like md:, state variants like hover: and focus-visible:, dark mode and arbitrary values, by building real components.

19 min read0/4 questions

In the last lesson you styled single elements: padding, colour, type. Real pages are about arrangement (what sits next to what, and how that changes on a phone) and interaction (what happens on hover, on focus, when a button is disabled). Tailwind handles both with the same trick: a short prefix in front of a class, like md: or hover:, that says "only apply this when...". By the end of this lesson you will build a responsive card grid and a navigation bar without writing a single line of CSS.

Flexbox utilities

Everything you know about flexbox maps directly onto classes.

TailwindCSS
flexdisplay: flex
flex-colflex-direction: column
flex-wrapflex-wrap: wrap
items-centeralign-items: center
justify-betweenjustify-content: space-between
gap-4gap: 1rem
flex-1grow and shrink to fill the space
shrink-0never shrink (good for icons and avatars)
HTML + Tailwind
<div class="flex items-center gap-4 rounded-xl border border-slate-200 p-4"> <div class="size-12 shrink-0 rounded-full bg-amber-300"></div> <div class="flex-1"> <p class="font-semibold text-slate-900">Order #4021 is on its way</p> <p class="text-sm text-slate-500">Arriving Thursday between 9 and 11</p> </div> <button class="rounded-lg bg-slate-900 px-3 py-1.5 text-sm text-white">Track</button></div>

Preview

Remove shrink-0 and make the text much longer: the circle gets squashed. Change items-center to items-start and the button jumps to the top.

Grid utilities

grid turns on CSS Grid, and grid-cols-3 makes three equal columns (it is repeat(3, minmax(0, 1fr))). col-span-2 makes an item span two columns, and gap-6 sets the gutters. In version 4 any number works, so grid-cols-7 is fine for a calendar.

HTML + Tailwind
<div class="grid grid-cols-3 gap-3"> <div class="col-span-2 rounded-lg bg-sky-200 p-4">Main story (spans 2)</div> <div class="rounded-lg bg-sky-100 p-4">Weather</div> <div class="rounded-lg bg-sky-100 p-4">Sport</div> <div class="rounded-lg bg-sky-100 p-4">Food</div> <div class="rounded-lg bg-sky-100 p-4">Travel</div></div>

Preview

Try grid-cols-2, or add row-span-2 to the Weather box.

Responsive prefixes

Here is the most important idea in Tailwind layout. A class with no prefix applies at every screen size. A class with a breakpoint prefix applies from that width upward.

PrefixApplies fromRoughly
(none)0pxevery screen
sm:40rem (640px)large phones
md:48rem (768px)tablets
lg:64rem (1024px)laptops
xl:80rem (1280px)desktops
2xl:96rem (1536px)large screens

So grid-cols-1 md:grid-cols-2 lg:grid-cols-4 reads: one column on phones, two from tablet width, four from laptop width. This is mobile-first: you write the phone layout plainly, then add prefixes as the screen grows. Under the hood each prefix is a @media (width >= ...) query.

How grid-cols-1 md:grid-cols-2 lg:grid-cols-4 resolves
  1. lg:grid-cols-41024px and wider
  2. md:grid-cols-2768px and wider
  3. grid-cols-1every width

On a 390px phone only the base class applies: one column.

1/3
Predict the outputHTML

The screen is 600px wide. How many columns does this grid have? Type a number.

<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4">
Need a nudge? Hints walk you through it.

The previews on this page are narrow, so they will usually show the phone layout. To make a component that responds to its own width instead of the screen's, Tailwind has container queries built in: mark a parent with @container, then use @sm:, @md: and so on in its children. The editor below puts the cards inside a box you can resize. Drag the bottom right corner of the dashed box.

HTML + Tailwind
<div class="@container resize-x overflow-auto border-2 border-dashed border-slate-300 p-3" style="width: 280px"> <div class="grid grid-cols-1 gap-3 @md:grid-cols-2 @2xl:grid-cols-3"> <article class="rounded-xl border border-slate-200 p-4 shadow-sm"> <h3 class="font-semibold">Lemon tart</h3> <p class="text-sm text-slate-500">40 min</p> </article> <article class="rounded-xl border border-slate-200 p-4 shadow-sm"> <h3 class="font-semibold">Green curry</h3> <p class="text-sm text-slate-500">30 min</p> </article> <article class="rounded-xl border border-slate-200 p-4 shadow-sm"> <h3 class="font-semibold">Focaccia</h3> <p class="text-sm text-slate-500">3 hours</p> </article> </div></div>

Preview

At about 450px (28rem) the cards go to two columns, and at 672px (42rem) to three, if your preview is wide enough. Container queries work in all modern browsers since 2023. On a real site you would use md: and lg: for page-level layout and container queries for reusable components.

Quick check
What does flex-col md:flex-row do?
Need a nudge? A hint rules out a wrong answer.

State variants

The same prefix idea handles interaction. hover:bg-slate-700 changes the background only on hover. You can stack variants too: md:hover:underline.

VariantApplies when
hover:the pointer is over the element
focus-visible:the element has keyboard focus
active:the element is being pressed
disabled:a form control has the disabled attribute
group-hover:a parent marked group is hovered
dark:dark mode is on
HTML + Tailwind
<div class="flex flex-wrap gap-3"> <button class="rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white hover:bg-indigo-500 active:scale-95 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"> Save </button> <button disabled class="rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white disabled:cursor-not-allowed disabled:opacity-40"> Disabled </button></div> <a href="#" class="group mt-6 block max-w-xs rounded-xl border border-slate-200 p-4 hover:border-indigo-400"> <p class="font-semibold group-hover:text-indigo-600">Read the guide</p> <p class="text-sm text-slate-500">Hover anywhere on this card.</p> <span class="mt-2 inline-block transition group-hover:translate-x-1">Go</span></a>

Preview

Hover and press the Save button, then click inside the preview and press Tab to see the focus ring appear. Keep focus-visible: styles on everything clickable: keyboard users rely on them. Notice how group on the card lets the title and the Go label react when any part of the card is hovered.

A few more things about variants are worth knowing early:

  • Variants stack. md:hover:bg-slate-100 means "from 768px up, and only on hover". dark:md:text-white works the same way. Read them left to right as a chain of conditions.
  • peer is the sibling version of group. Mark an element peer, and a later sibling can react to its state with peer-checked:, peer-invalid: or peer-focus:. A classic use is an error message that appears only when the input before it is invalid: peer-invalid:block on a hidden paragraph.
  • Form states have their own variants. checked:, invalid:, required: and placeholder-shown: map to the CSS pseudo-classes of the same names.
  • hover: only applies on devices that can hover. In version 4 it is wrapped in @media (hover: hover), so a tap on a phone does not leave a button stuck in its hover colour. Never hide important information behind hover alone, since touch users may never see it.
  • motion-safe: and motion-reduce: follow the prefers-reduced-motion setting, so you can write motion-safe:transition and respect people who turn animation off.
Match the pairs0/5 matched

Match each class to when it applies.

Choose an item, then its match on the other side.

Dark mode

dark:bg-slate-900 applies only in dark mode. By default, Tailwind follows the operating system setting through the prefers-color-scheme media query. Many sites prefer a toggle button instead, which adds a dark class to the html element. In version 4 you switch to that with one line of CSS, @custom-variant:

HTML + Tailwind
<style type="text/tailwindcss"> @custom-variant dark (&:where(.dark, .dark *));</style><div class="dark"> <div class="rounded-xl bg-white p-6 text-slate-900 dark:bg-slate-900 dark:text-slate-100"> <h2 class="text-xl font-bold">Night mode</h2> <p class="text-slate-600 dark:text-slate-400">Remove the <code>dark</code> class on the outer div.</p> </div></div>

Preview

Delete class="dark" from the outer div and the card switches to light. In a real app, a small script toggles that class on html and saves the choice.

Arbitrary values

Sometimes the design calls for a value that is not on the scale. Square brackets let you write any CSS value:

  • w-[37rem]: an exact width.
  • bg-[#1da1f2]: an exact colour.
  • grid-cols-[200px_1fr]: underscores stand for spaces, so this is 200px 1fr.
  • bg-(--brand): use a CSS custom property (version 4 shorthand for bg-[var(--brand)]).
  • [mask-type:luminance]: any property at all.

Use them for genuine one-offs, like matching the exact width of an embedded map or a colour from a partner's logo. If the same arbitrary value appears in five places, it should become a design token, which is the next lesson. Arbitrary values also accept responsive and state prefixes like any other class: md:w-[37rem] and hover:bg-[#0d8bd9] both work.

Quick check
You need a sidebar layout with a fixed 240px column and a flexible main area. Which class fits best?
Need a nudge? A hint rules out a wrong answer.

Putting it together: a nav bar

HTML + Tailwind
<header class="border-b border-slate-200"> <nav class="mx-auto flex max-w-5xl flex-wrap items-center justify-between gap-3 px-4 py-3"> <a href="#" class="text-lg font-bold tracking-tight">Larder</a> <ul class="flex flex-wrap items-center gap-1 text-sm"> <li><a href="#" class="rounded-md px-3 py-2 text-slate-600 hover:bg-slate-100 hover:text-slate-900">Recipes</a></li> <li><a href="#" class="rounded-md px-3 py-2 text-slate-600 hover:bg-slate-100 hover:text-slate-900">Plans</a></li> <li><a href="#" aria-current="page" class="rounded-md bg-slate-900 px-3 py-2 text-white">Shop</a></li> </ul> <a href="#" class="hidden rounded-lg bg-orange-500 px-4 py-2 text-sm font-semibold text-white hover:bg-orange-600 sm:block"> Start free </a> </nav></header>

Preview

flex justify-between spreads the logo, links and button apart, flex-wrap lets the links drop to a new line on tiny screens, and hidden sm:block hides the call-to-action button on phones and shows it from 640px. Change sm:block to block to see it on every size, and try moving bg-slate-900 text-white onto a different link.

Key terms
variant
A prefix like hover: or md: that applies a class only under a condition.
breakpoint
A screen width where the layout changes. Tailwind's are sm 640px, md 768px, lg 1024px, xl 1280px, 2xl 1536px.
mobile-first
Writing the smallest layout as the default and adding styles for larger screens on top.
container query
A style that depends on a parent element's size instead of the screen's. In Tailwind: @container and @md:.
group
A class on a parent so children can react to its state with group-hover: and similar variants.
arbitrary value
A custom value in square brackets, like w-[37rem], for one-off sizes or colours outside the scale.