Lesson 8 / 1267%

Lesson 8

Responsive Design

22 min

Breakpoints, the mobile-first approach and adapting your layout to the screen size.

Definition

Responsive design is an approach that automatically adapts the layout and style of a site to the screen size (mobile, tablet, computer). With Tailwind, you achieve it by adding a breakpoint prefix in front of a class, for examplemd:grid-cols-2: the class only applies from the indicated screen size and up, without writing a media query.

Responsive design with Tailwind

Today, a site is viewed on phones, tablets, laptops and large screens. Rather than creating a different version for each one, you design a single page that adapts. Tailwind makes this very simple thanks to class prefixes directly in the HTML.

In the previous lessons, you have already come across classes likemd:px-10 or lg:grid-cols-3. This lesson finally explains how they work and how to use them well.

Before you start: the viewport tag

For responsive design to work on mobile, your page must contain this tag in the <head>. Without it, phones display the page as if on a large screen, shrunk down:

In the head of your page
<meta name="viewport" content="width=device-width, initial-scale=1" />

Projects created with Astro or Vite already include it by default, but remember to check for it if you write an HTML page by hand.

Tailwind's breakpoints

Tailwind defines five screen sizes, called breakpoints , each associated with a prefix:

  • sm:: from 640px wide
  • md:: from 768px
  • lg:: from 1024px
  • xl:: from 1280px
  • 2xl:: from 1536px

The "mobile-first" principle

This is the most important point of the lesson. Tailwind works mobile-first : a class without a prefix applies to all screen sizes, and a class with a prefix applies to that size and all larger sizes .

From smallest to largest
<p class="text-base md:text-xl lg:text-2xl">
  This text grows with the screen
</p>

This paragraph is text-base by default (mobile), switches totext-xl from 768px, then to text-2xl from 1024px. So you always write the base style for the small screen, then adjust it for wider screens.

Changing the number of columns depending on the screen

This is where Grid and Tailwind's responsive design shine together. You combine several grid-cols-* classes prefixed with a screen size:

1 column on mobile, 2 on tablet, 3 on desktop
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
  <div class="bg-white rounded-xl shadow p-4">Card A</div>
  <div class="bg-white rounded-xl shadow p-4">Card B</div>
  <div class="bg-white rounded-xl shadow p-4">Card C</div>
</div>

Result

Preview of a grid of three cards that changes its number of columns depending on the screen width

This technique is exactly the one used to display the course cards on this site: a single column on mobile to stay readable, then two or three columns as the screen gets larger.

Changing the direction of a layout

A great classic: elements stacked on mobile, side by side on large screens. You just change the Flexbox direction depending on the size:

<div class="flex flex-col md:flex-row gap-4">
  <div class="md:w-1/3 bg-slate-100 p-4">Image</div>
  <div class="md:w-2/3 bg-slate-100 p-4">Text</div>
</div>

On mobile, the two blocks stack (flex-col). From 768px, they sit side by side (md:flex-row) and share the width as one third and two thirds.

Showing or hiding depending on the screen

Some elements only make sense on certain screen sizes, like a full menu on a computer and a "burger" button on mobile:

<nav class="hidden md:flex gap-6">Full menu</nav>
<button class="md:hidden">Burger menu</button>

hidden md:flex hides the navigation by default then shows it as Flexbox from 768px, while md:hidden makes the button disappear from that same size.

Adapting spacing and images

Spacing also benefits from prefixes: reduced padding on mobile and more generous on large screens. For images, two classes are enough to make them fluid:

<section class="px-4 py-8 md:px-10 md:py-16">
  <img src="/photo.webp" alt="A photo" class="w-full h-auto rounded-xl" />
</section>

w-full forces the image to follow the width of its container, andh-auto preserves its proportions. The image therefore never overflows, whatever the screen size.

Targeting only small screens with max-*

Sometimes you want to apply a style only below a certain size. Tailwind offers themax-sm:, max-md:, max-lg: variants for this, etc. For example, max-md:hidden hides an element on screens smaller than 768px. This remains an exception: the mobile-first approach with the regular prefixes is suitable in the vast majority of cases.

Combining several prefixes

Breakpoint prefixes combine with Tailwind's other variants, such ashover: or dark:. You simply write them one after the other, separated by colons:

<button class="bg-indigo-600 md:hover:bg-indigo-700 dark:bg-indigo-500">
  Send
</button>

Here, the hover only changes the color from 768px, which makes sense because a touchscreen has no hover in the strict sense.

Complete example: a responsive hero section

Here is how to put together everything you have just learned in a classic hero section, with a heading that grows, buttons that stack then line up, and spacing that adapts:

A hero section
<section class="px-4 py-10 md:px-10 md:py-20 text-center">
  <h1 class="text-3xl md:text-5xl font-extrabold text-slate-900">
    Learn Tailwind CSS
  </h1>
  <p class="mt-4 text-base md:text-lg text-slate-600 max-w-2xl mx-auto">
    A step-by-step course to build modern interfaces.
  </p>
  <div class="mt-8 flex flex-col sm:flex-row justify-center gap-3">
    <a href="#" class="px-5 py-3 rounded-lg bg-indigo-600 text-white">Get started</a>
    <a href="#" class="px-5 py-3 rounded-lg border border-slate-300">Learn more</a>
  </div>
</section>

On mobile, the heading is modest, the padding is reduced and the two buttons stack. From 640px, the buttons sit side by side, and from 768px the heading and the spacing become more generous.

A few best practices

  • Test often : check your page at several widths while you build, not just at the end.
  • Keep text readable : combine max-w-* andmx-auto to avoid overly long lines on large screens.
  • Think touch : on mobile, buttons at least 44px tall are easier to tap.

The default breakpoints suit most projects, but it is possible to change them in the theme configuration. We will cover this in the lesson dedicated to customization.

Common beginner mistakes

  • Thinking that sm: means "mobile" : this is wrong. sm: targets screens of 640px and up. The mobile style is the one without a prefix.
  • Reasoning from large screen to small : first write the mobile version, then add the prefixes for wider screens.
  • Forgetting the viewport tag : without it, no prefix seems to work on a real phone.
  • Multiplying breakpoints : two or three are often enough. The more there are, the more complicated maintenance gets.

Testing your responsive design

You do not need a real phone to check your work. In your browser, open the developer tools (F12) then enable "device" mode (Ctrl + Shift + M, orCmd + Shift + M on Mac). You can choose a phone model or resize the window freely and observe at which point your responsive classes kick in.

Practical exercise

Build a grid of 6 cards withgrid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4, then make the very first card take up 2 columns on desktop using lg:col-span-2. Then resize the window to check the behavior at each breakpoint.