Responsive Components — Tailwind CSS

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…

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

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)

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>

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>

⚠️ A navbar that appears on the wrong screen

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.

What you will learn