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.
Responsive visibility toggling in 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.
<div class="hidden md:block">Visible on medium and larger screens</div> <div class="block md:hidden">Visible only on small screens</div>
<p class="sm:hidden">Hidden on small screens and larger</p> <p class="hidden lg:block">Visible only on large screens and bigger</p>
<button class="md:hidden">Menu</button> <nav class="hidden md:flex">Full menu</nav>
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.
<!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>
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.
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
md:hidden do to an element?Solution
Step 1: Understand the prefix
The prefixmd:md:applies styles starting from medium screen sizes (≥768px).Step 2: Interpret the
Thehiddenclasshiddenclass hides the element by settingdisplay: none.Final Answer:
Hides the element on medium and larger screens -> Option AQuick Check:
md:hiddenmeans hide on medium+ screens [OK]
- Thinking it hides only on medium screen, not larger
- Confusing
hiddenwithblock - Ignoring the screen size prefix effect
Solution
Step 1: Analyze
This means the element is hidden by default (small screens) and becomes block (visible) on large screens (≥1024px).hidden lg:blockStep 2: Check other options
block sm:hiddenshows below sm but hides from sm upwards (very small screens only).lg:hidden blockhides on large screens, opposite of requirement.sm:block hiddenshows on sm+ but hidden by default, which is contradictory.Final Answer:
hidden lg:block-> Option DQuick Check:
Hide default, show large =hidden lg:block[OK]
- Mixing order of classes causing unexpected visibility
- Using wrong screen prefix for large screens
- Assuming
smmeans large screen
<div class="hidden md:block lg:hidden">Content</div>
On which screen sizes will the content be visible?
Solution
Step 1: Interpret classes in order
hiddenhides by default (small screens).md:blockshows block from medium (≥768px).lg:hiddenhides again from large (≥1024px).Step 2: Determine visible range
Visible only between medium (768px) and before large (1024px) because it is block there but hidden outside.Final Answer:
Only on medium screens (≥768px and <1024px) -> Option AQuick Check:
Visible only between md and lg breakpoints [OK]
- Assuming visible on large screens despite lg:hidden
- Ignoring default hidden class
- Confusing screen size ranges
<button class="block lg:block">Click Me</button>
What is the error?
Solution
Step 1: Understand default visibility
The button hasblockby default, so it shows on all screens initially.Step 2: Analyze
This shows the button on large screens and above (≥1024px), but default is visible everywhere, so it shows everywhere.lg:blockStep 3: Fix by adding
To show only on large screens (≥1024px), default should behiddendefaulthiddenandlg:blockto show on large. The code is missing the defaulthidden.Final Answer:
Missinghiddenclass for default state -> Option CQuick Check:
Default hidden, then show large = addhidden[OK]
lg:block [OK]- Assuming
lg:blockhides screens below large - Not setting default visibility correctly
- Thinking class order affects visibility
Solution
Step 1: Break down requirements
Hidden on small screens (default), visible block on medium (≥768px), hidden again on extra-large (≥1280px).Step 2: Match classes to requirements
hiddenhides by default (small).md:blockshows block from medium.xl:hiddenhides again from extra-large.Step 3: Verify other options
block md:hidden xl:blockshows block by default, wrong.hidden sm:block lg:hiddenusessmandlg, not medium and extra-large.md:hidden block xl:blockhides from medium, opposite of requirement.Final Answer:
hidden md:block xl:hidden-> Option BQuick Check:
Hide default, show md, hide xl =hidden md:block xl:hidden[OK]
- Mixing breakpoint prefixes
- Forgetting default visibility
- Using wrong order of classes
Ready to prove it?
Pass the test and this pattern moves unknown
