📖 Scenario: You are building a simple webpage that shows a box. This box should change its maximum width depending on the screen size. This helps the box look good on phones, tablets, and desktops.
🎯 Goal: Create a box using Tailwind CSS that has different max-width values for small, medium, and large screens using responsive variants.
📋 What You'll Learn
Use Tailwind CSS classes to set max-width
Set max-width to max-w-xs on small screens
Set max-width to max-w-md on medium screens (from 768px)
Set max-width to max-w-lg on large screens (from 1024px)
Use semantic HTML with a main container
Add accessible text inside the box
💡 Why This Matters
🌍 Real World
Responsive design is essential for websites to look good on phones, tablets, and desktops. Using Tailwind's responsive variants makes this easy.
💼 Career
Front-end developers often use Tailwind CSS to quickly build responsive layouts that adapt to different devices.
Progress0 / 4 steps
1
Create the HTML skeleton with a box
Create a main element with a div inside it. Give the div a class bg-blue-200 and add the text Responsive Box inside the div.
Tailwind
Hint
Use a main tag for the container and a div with the class bg-blue-200 for the box.
2
Add a base max-width for the box
Add the Tailwind class max-w-xs to the div to set the maximum width on small screens.
Tailwind
Hint
Add max-w-xs inside the class attribute of the div.
3
Add medium screen max-width variant
Add the Tailwind class md:max-w-md to the div so that on medium screens (768px and up) the max-width changes to max-w-md.
Tailwind
Hint
Use the prefix md: before max-w-md to apply it on medium screens.
4
Add large screen max-width variant
Add the Tailwind class lg:max-w-lg to the div so that on large screens (1024px and up) the max-width changes to max-w-lg.
Tailwind
Hint
Use the prefix lg: before max-w-lg to apply it on large screens.
Practice
(1/5)
1. What does the Tailwind class md:max-w-lg do?
easy
A. Sets the max-width to large only on medium screens and larger
B. Sets the max-width to large on all screen sizes
C. Sets the max-width to medium on large screens only
D. Sets the max-width to large only on small screens
Solution
Step 1: Understand the prefix md:
The prefix md: means the style applies from medium screen sizes and up.
Step 2: Interpret max-w-lg
The class max-w-lg sets the maximum width to a large size.
Final Answer:
Sets the max-width to large only on medium screens and larger -> Option A
Quick Check:
Prefix md: + max-w-lg = Sets the max-width to large only on medium screens and larger [OK]
Hint: Remember prefixes apply styles from that screen size up [OK]
Common Mistakes:
Thinking the class applies on all screen sizes
Confusing md: with lg:
Assuming it applies only on small screens
2. Which of these is the correct Tailwind syntax to set max-width to medium on large screens only?
easy
A. lg:max-w-md
B. max-w-lg
C. md:max-w-md
D. sm:max-w-lg
Solution
Step 1: Identify the breakpoint for large screens
The prefix for large screens is lg:.
Step 2: Match max-width size to medium
The class for medium max-width is max-w-md.
Final Answer:
lg:max-w-md -> Option A
Quick Check:
Large screen prefix + medium max-width = lg:max-w-md [OK]
Hint: Use lg: for large screens, then max-width size [OK]
B. The syntax is invalid because of multiple max-width classes
C. The max-width decreases on larger screens, which is unusual
D. The classes will not apply because of missing lg: prefix
Solution
Step 1: Analyze the max-width values at breakpoints
At small screens, max-width is large (sm:max-w-lg), but at medium screens it drops to small (md:max-w-sm).
Step 2: Understand typical responsive behavior
Usually, max-width increases or stays the same on larger screens, not decreases.
Final Answer:
The max-width decreases on larger screens, which is unusual -> Option C
Quick Check:
Responsive max-width should not shrink on bigger screens [OK]
Hint: Max-width usually grows or stays same on bigger screens [OK]
Common Mistakes:
Thinking multiple max-width classes cause syntax errors
Believing sm: is invalid for max-width
Expecting missing lg: to cause errors
5. You want a container that is max-width small on mobile, medium on tablets, and extra-large on desktops. Which Tailwind class combination achieves this?
hard
A. max-w-sm sm:max-w-md md:max-w-lg
B. max-w-md sm:max-w-lg lg:max-w-xl
C. sm:max-w-sm md:max-w-md xl:max-w-xl
D. max-w-sm md:max-w-md lg:max-w-xl
Solution
Step 1: Assign max-width for mobile (default)
Use max-w-sm for small max-width on mobile (no prefix).
Step 2: Assign max-width for tablets (medium screens)
Use md:max-w-md to set medium max-width starting at medium breakpoint.
Step 3: Assign max-width for desktops (large screens)
Use lg:max-w-xl to set extra-large max-width starting at large breakpoint.
Final Answer:
max-w-sm md:max-w-md lg:max-w-xl -> Option D
Quick Check:
Default small + medium at md + extra-large at lg = max-w-sm md:max-w-md lg:max-w-xl [OK]
Hint: Set default, then md: and lg: for tablet and desktop widths [OK]
Common Mistakes:
Using sm: for mobile default instead of no prefix
Skipping the large breakpoint for desktop
Mixing up breakpoint order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.