Bird
Raised Fist0
Tailwindmarkup~5 mins

Responsive visibility toggling in Tailwind

Choose your learning style10 modes available
Introduction

Responsive visibility toggling helps show or hide parts of a webpage depending on the screen size. This makes your site look good on phones, tablets, and computers.

Show a simple menu button only on small screens and a full menu on larger screens.
Hide large images on small phones to save data and speed up loading.
Display extra information only on big screens where there is space.
Make buttons easier to tap on small devices by showing bigger versions.
Switch between different layouts by hiding or showing sections based on device size.
Syntax
Tailwind
hidden md:block
block md:hidden

hidden hides an element completely.

block shows an element as a block.

Adding prefixes like md: applies the style only on medium (tablet) screens and larger.

Examples
The first div is hidden on small screens and visible on medium and bigger screens.The second div is visible on small screens and hidden on medium and bigger screens.
Tailwind
<div class="hidden md:block">Visible on medium and larger screens</div>
<div class="block md:hidden">Visible only on small screens</div>
The first paragraph is hidden starting from small screens.The second paragraph appears only on large screens and bigger.
Tailwind
<p class="sm:hidden">Hidden on small screens and larger</p>
<p class="hidden lg:block">Visible only on large screens and bigger</p>
The button shows only on small screens.The navigation menu shows only on medium and larger screens using flex layout.
Tailwind
<button class="md:hidden">Menu</button>
<nav class="hidden md:flex">Full menu</nav>
Sample Program

This page shows a "Menu" button only on small screens. On medium and larger screens, it hides the button and shows a full navigation menu instead.

The paragraphs below also change text depending on screen size to explain what is visible.

Tailwind
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Responsive Visibility Example</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="p-6 font-sans">
  <header class="mb-6">
    <button class="md:hidden bg-blue-600 text-white px-4 py-2 rounded" aria-label="Open menu">Menu</button>
    <nav class="hidden md:flex space-x-4" aria-label="Main navigation">
      <a href="#" class="text-blue-700 hover:underline">Home</a>
      <a href="#" class="text-blue-700 hover:underline">About</a>
      <a href="#" class="text-blue-700 hover:underline">Contact</a>
    </nav>
  </header>
  <main>
    <p class="block md:hidden">You are on a small screen. The menu button is visible.</p>
    <p class="hidden md:block">You are on a medium or larger screen. The full menu is visible.</p>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Use aria-label on buttons that only show icons or simple text for better accessibility.

Test your design by resizing the browser or using device simulation in browser DevTools.

Combine visibility classes with layout classes like flex or grid for better control.

Summary

Responsive visibility toggling shows or hides elements based on screen size.

Use Tailwind's hidden and block classes with screen prefixes like md:.

This helps create user-friendly layouts for phones, tablets, and desktops.

Practice

(1/5)
1. What does the Tailwind class md:hidden do to an element?
easy
A. Hides the element on medium and larger screens
B. Shows the element only on medium screens
C. Hides the element on all screen sizes
D. Shows the element on all screen sizes

Solution

  1. Step 1: Understand the prefix md:

    The prefix md: applies styles starting from medium screen sizes (≥768px).
  2. Step 2: Interpret the hidden class

    The hidden class hides the element by setting display: none.
  3. Final Answer:

    Hides the element on medium and larger screens -> Option A
  4. Quick Check:

    md:hidden means hide on medium+ screens [OK]
Hint: Remember: prefix means from that size and up [OK]
Common Mistakes:
  • Thinking it hides only on medium screen, not larger
  • Confusing hidden with block
  • Ignoring the screen size prefix effect
2. Which Tailwind class combination correctly hides an element on small screens but shows it on large screens?
easy
A. block sm:hidden
B. sm:block hidden
C. lg:hidden block
D. hidden lg:block

Solution

  1. Step 1: Analyze hidden lg:block

    This means the element is hidden by default (small screens) and becomes block (visible) on large screens (≥1024px).
  2. Step 2: Check other options

    block sm:hidden shows below sm but hides from sm upwards (very small screens only). lg:hidden block hides on large screens, opposite of requirement. sm:block hidden shows on sm+ but hidden by default, which is contradictory.
  3. Final Answer:

    hidden lg:block -> Option D
  4. Quick Check:

    Hide default, show large = hidden lg:block [OK]
Hint: Default hidden, then show with large prefix [OK]
Common Mistakes:
  • Mixing order of classes causing unexpected visibility
  • Using wrong screen prefix for large screens
  • Assuming sm means large screen
3. Given this HTML snippet:
<div class="hidden md:block lg:hidden">Content</div>

On which screen sizes will the content be visible?
medium
A. Only on medium screens (≥768px and <1024px)
B. Only on large screens (≥1024px)
C. On all screen sizes
D. Never visible

Solution

  1. Step 1: Interpret classes in order

    hidden hides by default (small screens). md:block shows block from medium (≥768px). lg:hidden hides again from large (≥1024px).
  2. Step 2: Determine visible range

    Visible only between medium (768px) and before large (1024px) because it is block there but hidden outside.
  3. Final Answer:

    Only on medium screens (≥768px and <1024px) -> Option A
  4. Quick Check:

    Visible only between md and lg breakpoints [OK]
Hint: Check each prefix range and last applied class [OK]
Common Mistakes:
  • Assuming visible on large screens despite lg:hidden
  • Ignoring default hidden class
  • Confusing screen size ranges
4. This code is intended to show a button only on large screens but it shows on all screens:
<button class="block lg:block">Click Me</button>

What is the error?
medium
A. The block class overrides sm:hidden on larger screens
B. The sm:hidden class hides on small screens instead of showing
C. Missing hidden class for default state
D. The order of classes should be reversed

Solution

  1. Step 1: Understand default visibility

    The button has block by default, so it shows on all screens initially.
  2. Step 2: Analyze lg:block

    This shows the button on large screens and above (≥1024px), but default is visible everywhere, so it shows everywhere.
  3. Step 3: Fix by adding hidden default

    To show only on large screens (≥1024px), default should be hidden and lg:block to show on large. The code is missing the default hidden.
  4. Final Answer:

    Missing hidden class for default state -> Option C
  5. Quick Check:

    Default hidden, then show large = add hidden [OK]
Hint: Default hidden, then show on large with lg:block [OK]
Common Mistakes:
  • Assuming lg:block hides screens below large
  • Not setting default visibility correctly
  • Thinking class order affects visibility
5. You want a navigation menu that is hidden on small screens, visible as a block on medium screens, and hidden again on extra-large screens. Which Tailwind class set achieves this?
hard
A. md:hidden block xl:block
B. hidden md:block xl:hidden
C. hidden sm:block lg:hidden
D. block md:hidden xl:block

Solution

  1. Step 1: Break down requirements

    Hidden on small screens (default), visible block on medium (≥768px), hidden again on extra-large (≥1280px).
  2. Step 2: Match classes to requirements

    hidden hides by default (small). md:block shows block from medium. xl:hidden hides again from extra-large.
  3. Step 3: Verify other options

    block md:hidden xl:block shows block by default, wrong. hidden sm:block lg:hidden uses sm and lg, not medium and extra-large. md:hidden block xl:block hides from medium, opposite of requirement.
  4. Final Answer:

    hidden md:block xl:hidden -> Option B
  5. Quick Check:

    Hide default, show md, hide xl = hidden md:block xl:hidden [OK]
Hint: Chain visibility classes with correct breakpoints [OK]
Common Mistakes:
  • Mixing breakpoint prefixes
  • Forgetting default visibility
  • Using wrong order of classes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard