React / 56
Responsive design, states and dark mode
Prefixes like md:, hover: and dark: switch a class on only at a screen size, in a state or in dark mode. Square brackets handle one-off values.
Mobile-first breakpoints
A prefix like md: means "from this width up". Without a prefix, a class applies everywhere.
| Prefix | Applies when the screen is at least |
|---|---|
| (none) | 0px: every screen |
sm: | 640px |
md: | 768px |
lg: | 1024px |
xl: | 1280px |
2xl: | 1536px |
So you write the phone layout first, then add changes for bigger screens. Drag the width and watch which class wins:
Drag the screen width
Try it- grid-cols-1wins
- sm:grid-cols-2not yet
- lg:grid-cols-4not yet
- text-centerwins
- sm:text-leftnot yet
Today's menu
<section>
<h2 className="text-center text-2xl sm:text-left">Today's menu</h2>
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
{dishes.map((d) => <DishCard key={d.id} {...d} />)}
</div>
</section>Need a style only between two sizes? Stack a breakpoint with a max- variant: md:max-lg:flex applies from 768px up to (not including) 1024px.
States: hover, focus and friends
The same prefix idea works for interaction states:
<button
disabled={cartIsEmpty}
className="rounded-lg bg-orange-600 px-4 py-2 text-white
hover:bg-orange-700 active:scale-95
focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-orange-600
disabled:opacity-50"
>
Place order
</button>| Prefix | When |
|---|---|
hover: | pointer is over it (only on devices that can hover) |
focus-visible: | focused from the keyboard. Prefer it over focus: for outlines |
active: | being pressed |
disabled: | the element has disabled |
group-hover: | a parent marked group is hovered |
group styles a child based on its parent:
<a href="/dishes/dosa" className="group block rounded-xl p-4 hover:bg-orange-50">
<h3 className="font-semibold group-hover:underline">Masala Dosa</h3>
<p className="text-sm text-gray-600">Crispy, with chutney</p>
</a>Prefixes stack, read left to right: md:hover:bg-orange-700 means "on md screens and up, when hovered".
Dark mode
dark: applies when the operating system is in dark mode:
<div className="bg-white text-gray-900 dark:bg-gray-900 dark:text-gray-100">…</div>Want a toggle button instead? Tell Tailwind to follow a dark class, then add or remove that class on <html> from your code:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));document.documentElement.classList.toggle("dark");One-off values with brackets
When the design needs a value that isn't on the scale, use square brackets. These are called arbitrary values:
<>
<div className="w-[327px] bg-[#1da1f2]">An exact width and colour</div>
<div className="grid grid-cols-[1fr_2fr]">Write _ where CSS has a space</div>
<div className="bg-(--brand-color)">Read a CSS variable</div>
</>Use them for genuine one-offs. If a value shows up twice, give it a name in @theme so it joins the scale.
Gotchas
Recall
Can you answer these?
0/5
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
