📖 Scenario: You are building a simple webpage that shows a welcome message and a special note. The note should only be visible on medium and larger screens, but hidden on small screens like phones.
🎯 Goal: Create a webpage using Tailwind CSS that has a heading and a paragraph. The paragraph should be hidden on small screens and visible on medium and larger screens using responsive visibility toggling.
📋 What You'll Learn
Use Tailwind CSS classes for styling and responsive visibility
Create a heading with the text 'Welcome to Our Site!'
Create a paragraph with the text 'This note is visible on medium and larger screens only.'
Make the paragraph hidden on small screens and visible on medium and larger screens
💡 Why This Matters
🌍 Real World
Responsive visibility toggling is common in websites to show or hide content depending on the device screen size, improving user experience.
💼 Career
Web developers often use responsive design techniques like Tailwind's visibility classes to build mobile-friendly and accessible websites.
Progress0 / 4 steps
1
Create the basic HTML structure with heading and paragraph
Create an HTML file with a <!DOCTYPE html> declaration, <html> element with lang="en", and inside the <body> add a <h1> with the text Welcome to Our Site! and a <p> with the text This note is visible on medium and larger screens only.
Tailwind
Hint
Start by writing the basic HTML page structure with a heading and a paragraph inside the body.
2
Add Tailwind CSS link to the head
Add the Tailwind CSS CDN link inside the <head> section using the <script> tag with src="https://cdn.tailwindcss.com".
Tailwind
Hint
Use the official Tailwind CSS CDN script tag inside the head to enable Tailwind classes.
3
Add Tailwind classes to toggle paragraph visibility responsively
Add the Tailwind CSS classes hidden md:block to the <p> element to make it hidden on small screens and visible as a block on medium and larger screens.
Tailwind
Hint
Use hidden to hide on small screens and md:block to show as block on medium and larger screens.
4
Add basic styling for better readability and accessibility
Add Tailwind CSS classes text-center mt-10 text-2xl font-semibold to the <h1> and text-center mt-4 text-lg text-gray-700 to the <p> for better styling and readability.
Tailwind
Hint
Use Tailwind classes to center text, add margin top, set font size and color for better look.
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
Step 1: Understand the prefix md:
The prefix md: applies styles starting from medium screen sizes (≥768px).
Step 2: Interpret the hidden class
The hidden class hides the element by setting display: none.
Final Answer:
Hides the element on medium and larger screens -> Option A
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
Step 1: Analyze hidden lg:block
This means the element is hidden by default (small screens) and becomes block (visible) on large screens (≥1024px).
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.
Final Answer:
hidden lg:block -> Option D
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
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
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).
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 A
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
Step 1: Understand default visibility
The button has block by default, so it shows on all screens initially.
Step 2: Analyze lg:block
This shows the button on large screens and above (≥1024px), but default is visible everywhere, so it shows everywhere.
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.
Final Answer:
Missing hidden class for default state -> Option C
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
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
hidden hides by default (small). md:block shows block from medium. xl:hidden hides again from extra-large.
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.