Tailwind CSS Tutorial menu

Tailwind CSS Responsive Design

In Tailwind, you make a design responsive by adding a breakpoint prefix in front of any class.

Breakpoint Prefixes

PrefixScreen widthTypical device
(none)All sizesPhones and up
sm:40rem (640px) and widerLarge phones
md:48rem (768px) and widerTablets
lg:64rem (1024px) and widerLaptops
xl:80rem (1280px) and widerDesktops
2xl:96rem (1536px) and widerLarge screens

A class like md:text-xl means "use text-xl when the screen is at least 768px wide".

Mobile First

Tailwind is mobile-first. A class with no prefix works on every screen, including phones. A prefixed class only starts working from that width upwards.

Example

html
<h1 class="text-2xl md:text-4xl lg:text-5xl">Welcome</h1>
ScreenFont size used
Phonetext-2xl
Tablet (768px+)text-4xl
Laptop (1024px+)text-5xl

A common mistake is to use sm: for phones. sm: does not mean "small screens only". Write phone styles with no prefix, then add sm:, md: and lg: for bigger screens.

A Responsive Card Grid

Resize the result window and watch the columns change.

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="bg-gray-100 p-4">

<h1 class="text-2xl md:text-4xl font-bold text-center mb-6">Our Courses</h1>

<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
  <div class="bg-white p-5 rounded-lg shadow-sm">Web Development</div>
  <div class="bg-white p-5 rounded-lg shadow-sm">Graphic Design</div>
  <div class="bg-white p-5 rounded-lg shadow-sm">Digital Marketing</div>
  <div class="bg-white p-5 rounded-lg shadow-sm">Python</div>
</div>

</body>
</html>
Try it Yourself »

On a phone the cards are in one column, on large phones and tablets two, and on laptops four.

Changing Layout Direction

A very common pattern: stack items on phones, put them side by side on bigger screens.

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-4">

<div class="flex flex-col md:flex-row gap-6 items-center bg-cyan-50 p-6 rounded-xl">
  <div class="w-full md:w-1/3 h-40 bg-cyan-400 rounded-lg"></div>
  <div class="w-full md:w-2/3 text-center md:text-left">
    <h2 class="text-2xl font-bold">Free Coding Workshop</h2>
    <p class="text-gray-600 mt-2">Saturday at 4 pm, Orangi Town. Bring your laptop if you have one.</p>
    <button class="mt-4 w-full md:w-auto bg-cyan-600 text-white px-6 py-2 rounded-lg">Reserve a seat</button>
  </div>
</div>

</body>
</html>
Try it Yourself »

Notice how one element can have several responsive classes: the button is full width on phones (w-full) and normal width from tablets (md:w-auto).

Showing and Hiding

  • hidden md:block hides an element on phones and shows it from tablets
  • md:hidden shows an element on phones only

Example

html
<button class="md:hidden">Menu</button>
<nav class="hidden md:flex gap-6">
  <a href="#">Home</a>
  <a href="#">Courses</a>
</nav>

Max-width Variants

Sometimes you want a style only below a size. Use max- in front: max-md:hidden hides an element on screens smaller than 768px.

Exercise

Exercise

What does the class lg:grid-cols-3 do?

Learn Tailwind CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp