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.
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.
| Tailwind | CSS |
|---|---|
flex | display: flex |
flex-col | flex-direction: column |
flex-wrap | flex-wrap: wrap |
items-center | align-items: center |
justify-between | justify-content: space-between |
gap-4 | gap: 1rem |
flex-1 | grow and shrink to fill the space |
shrink-0 | never shrink (good for icons and avatars) |
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.
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.
| Prefix | Applies from | Roughly |
|---|---|---|
| (none) | 0px | every 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.
On a 390px phone only the base class applies: one column.
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">
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.
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.
flex-col md:flex-row do?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.
| Variant | Applies 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 |
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-100means "from 768px up, and only on hover".dark:md:text-whiteworks the same way. Read them left to right as a chain of conditions. peeris the sibling version ofgroup. Mark an elementpeer, and a later sibling can react to its state withpeer-checked:,peer-invalid:orpeer-focus:. A classic use is an error message that appears only when the input before it is invalid:peer-invalid:blockon a hidden paragraph.- Form states have their own variants.
checked:,invalid:,required:andplaceholder-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:andmotion-reduce:follow theprefers-reduced-motionsetting, so you can writemotion-safe:transitionand respect people who turn animation off.
Match each class to when it applies.
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:
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 is200px 1fr.bg-(--brand): use a CSS custom property (version 4 shorthand forbg-[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.
Putting it together: a nav bar
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.
- variant
- A prefix like
hover:ormd:that applies a class only under a condition. - breakpoint
- A screen width where the layout changes. Tailwind's are
sm640px,md768px,lg1024px,xl1280px,2xl1536px. - 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:
@containerand@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.