On a phone, a website's navbar usually holds just the logo and a Menu button. Open the same website on a laptop, and the full set of links lines up on the right. It is still one navbar, only its arrangement changes.
A responsive component changes its arrangement to fit the space it has, with nothing more than a prefix such as md:. It's like shoes at home: in a small rented room they're stacked upwards, and on a wide porch they're lined up side by side. The shoes are the same, only their arrangement follows the space. On a web page, the shoes are the component's content, and the space is the room it has, such as the screen width.
A basic navbar: logo left, links right
This is the navbar the exercise checks:
<nav id="navbar" class="flex items-center justify-between p-4">
<span>Logo</span>
<div class="flex gap-4">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
| Class | CSS it generates | Meaning |
|---|---|---|
flex |
display: flex; |
the Logo and the link group sit side by side |
items-center |
align-items: center; |
the Logo and the links line up in the middle of the navbar's height |
justify-between |
justify-content: space-between; |
the Logo stays at the left, the link group at the right |
p-4 |
padding: 1rem; |
16px of padding |
gap-4 |
gap: 1rem; |
16px between the links |
- The first three classes go on the
<nav>, the parent of the Logo and the link group. These three place the<nav>'s children. - The link group has its own
flex gap-4, so its three links sit side by side, 16px apart. - In the exercise, the checker looks for
display: flex,align-items: centerandjustify-content: space-betweenon#navbar, and at least 3<a>links inside it.
Mobile-first: no prefix first
A class with no prefix applies at every screen width, starting from phones. A class with a prefix such as md: only applies from a certain width up:
| Prefix | Applies from | Its CSS |
|---|---|---|
| (none) | 0px, every screen | a normal rule |
sm: |
640px | @media (width >= 40rem) |
md: |
768px | @media (width >= 48rem) |
lg: |
1024px | @media (width >= 64rem) |
xl: |
1280px | @media (width >= 80rem) |
This is the same navbar as in the Lab. On a phone, the links are hidden and replaced by a Menu button:
<nav id="navbar" class="flex items-center justify-between p-4">
<span class="font-bold text-lg md:text-xl lg:text-2xl">Logo</span>
<button class="md:hidden">Menu</button>
<div class="hidden md:flex gap-4">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
Before you look at the table, guess: the Menu button gets md:hidden. Does it disappear on screens narrower than 768px, or on screens of 768px and up?
| Screen width | Menu button | Link group | Logo size |
|---|---|---|---|
| 360px | shown | hidden (hidden) |
18px (text-lg) |
| 768px | hidden (md:hidden) |
shown (md:flex) |
20px (md:text-xl) |
| 1024px | hidden | shown | 24px (lg:text-2xl) |
hidden md:flex: the link group is hidden on phones, then becomes flex from 768px.md:hidden: the button shows on phones, then is gone from 768px. Somd:hiddenhides the button on screens of 768px and up. A prefix always means "from this width", never "up to this width".text-lg md:text-xl lg:text-2xl: the Logo grows at each breakpoint.text-lg, with no prefix, is for phones, and the other two classes override it on wider screens.- At exactly 768px,
md:already applies, because the rule is "at least 768px".
The Menu button above can't open anything yet. To make it work, replace the button and the link group with this code, which uses a little JavaScript:
<button id="menu-button" class="md:hidden" aria-expanded="false" aria-controls="menu-links">Menu</button>
<div id="menu-links" class="hidden md:flex gap-4">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
<script>
const button = document.getElementById("menu-button");
const menu = document.getElementById("menu-links");
button.addEventListener("click", () => {
const isOpen = !menu.classList.toggle("hidden");
button.setAttribute("aria-expanded", isOpen);
});
</script>
- Each time the button is clicked,
classList.toggle("hidden")removes thehiddenclass from the link group if it is there, or puts it back if it isn't. aria-expandedtells screen readers (programs that read the page aloud) whether the menu is open. Its value comes fromisOpen, which istruewhile the menu shows.- Another option, with no JavaScript: a
<details>element that holds<summary>Menu</summary>and the links. The browser opens and closes it by itself when the<summary>is clicked.
Container queries: follow the container, not the screen
md: always looks at the screen's width. That's a problem for a component used in many places. Say an 800px screen has two copies of the same card: one in a sidebar (a side column) 256px wide, and one in the main content, 496px wide. For both cards, md: only sees that the screen is 800px, wider than 768px. So the content of the sidebar card also sits side by side, like the card in the main content, even though its space is narrow.
For this, Tailwind v4 has container queries built in: rules that apply by the width of the container (the parent element the component sits in), not the width of the screen. Mark the container with @container, then use the @md: prefix on an element inside it:
<aside class="w-64 @container">
<div class="flex flex-col gap-3 @md:flex-row">
<img class="size-20" src="shoes.jpg" alt="Running shoes">
<p>Running Shoes, $45</p>
</div>
</aside>
@containerbecomescontainer-type: inline-size: the elements inside can now measure the<aside>'s width.@md:flex-rowbecomes a@container (width >= 28rem)rule: the photo and the text sit side by side when the container is at least 448px.- On the same screen, the card in the 256px sidebar puts the photo above the text, and the card in the 496px main content puts the photo and the text side by side.
- The sizes differ from the screen prefixes:
md:means a 768px screen,@md:means a 448px container.
⚠️ A navbar that appears on the wrong screen
- Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1.0">in the<head>, a phone lays the page out 980px wide. That is wider than 768px, so the Menu button never shows. - Forgetting
flexon the navbar. Withoutflex,items-centerandjustify-betweenare ignored. In the exercise, the checker looks fordisplay: flexon#navbar. - Thinking
sm:is for phones.sm:applies from 640px. Styles for phones are written with no prefix. md:flexwithouthidden. The links show on every screen, because nothing hides them on phones. The pair is alwayshidden md:flex.@md:without@container. There's no container to measure, so@md:flex-rownever applies.
TL;DR: A responsive component changes its arrangement to fit the space it has. A basic navbar: flex items-center justify-between p-4 on the <nav>, with a flex gap-4 link group. Tailwind is mobile-first (starting from the phone): classes with no prefix apply to every screen, sm: from 640px, md: from 768px, lg: from 1024px. hidden md:flex is hidden on phones and shows from 768px. md:hidden is the opposite: gone from 768px up. text-lg md:text-xl lg:text-2xl grows at each breakpoint. For a component used in many places, use a container query: @container on its container (the parent element the component sits in) and @md: inside it, so what's measured is the container's width, not the screen's.
⚡ A basic navbar
Write:
<nav id="navbar" class="flex items-center justify-between p-4">
<span>Logo</span>
<div class="flex gap-4">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
Meaning: flex items-center justify-between on the <nav> puts the Logo at the left edge and the link group at the right, both in the middle of the navbar's height. The link group has its own flex gap-4, so its three links line up.
In the exercise, the checker (the program that checks your answer) looks for the CSS of those three classes on #navbar, and at least 3 <a> links inside it.
⚡ A menu on phones, links on laptops
Write:
<nav id="navbar" class="flex items-center justify-between p-4">
<span class="font-bold text-lg md:text-xl lg:text-2xl">Logo</span>
<button class="md:hidden">Menu</button>
<div class="hidden md:flex gap-4">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
</nav>
Guess first: the Menu button gets md:hidden. Is it gone on narrow screens, or on screens 768px and up?
On 768px and up. A prefix such as md: always means "from this width", never "up to this width".
Meaning: hidden md:flex hides the link group on phones, then makes it flex from 768px. A class with no prefix applies on every screen, starting from phones.
⚡ Following the container's width
Meaning: md: always looks at the screen's width, but the same component can sit in a narrow place, such as a sidebar (a side column), and in a wide one. For a component like that, give its container (the parent element the component sits in) the class @container, then use @md: on an element inside it (it applies when the container is at least 448px). That way, what's measured is the container's width, not the screen's. This is called a container query.