📖 Scenario: You are building a simple webpage that shows a colored box. The box should have a base background color on small screens and change colors on medium and large screens. This helps the box look good on phones, tablets, and desktops.
🎯 Goal: Create a responsive colored box using Tailwind CSS. The box's background color changes at different screen sizes using responsive utility overrides.
📋 What You'll Learn
Create a div with a base background color of bg-red-500.
Add a medium screen override to change the background color to bg-yellow-500.
Add a large screen override to change the background color to bg-green-500.
Set the box size to width: 20rem and height: 20rem using Tailwind classes.
Center the box horizontally and vertically on the page.
Use semantic HTML and include accessibility attributes where relevant.
Make sure the page is responsive and the color changes happen at the correct breakpoints.
💡 Why This Matters
🌍 Real World
Responsive design is essential for websites to look good on phones, tablets, and desktops. Using Tailwind's responsive utilities lets developers quickly change styles based on screen size.
💼 Career
Front-end developers often use Tailwind CSS to build responsive interfaces efficiently. Understanding responsive utility overrides is key to creating adaptable layouts.
Progress0 / 4 steps
1
Create the HTML skeleton with a colored box
Create a basic HTML5 page with lang="en" and charset="UTF-8". Inside the <body>, add a div with the Tailwind class bg-red-500 and fixed size classes w-80 and h-80 to make it 20rem by 20rem.
Tailwind
Hint
Start with a simple HTML page and add a div with the classes bg-red-500 w-80 h-80 inside the body.
2
Add a container to center the box vertically and horizontally
Wrap the existing div inside a main element. Add Tailwind classes flex, items-center, justify-center, and min-h-screen to the main to center the box both vertically and horizontally on the full viewport height.
Tailwind
Hint
Use a main tag with Tailwind flexbox classes to center the box vertically and horizontally.
3
Add medium screen override to change background color to yellow
Add the Tailwind responsive utility class md:bg-yellow-500 to the div so that on medium screens and larger, the box background color changes to yellow.
Tailwind
Hint
Add the class md:bg-yellow-500 to the box's div to change its color on medium screens.
4
Add large screen override to change background color to green
Add the Tailwind responsive utility class lg:bg-green-500 to the same div so that on large screens and above, the box background color changes to green.
Tailwind
Hint
Add the class lg:bg-green-500 to the box's div to change its color on large screens.
Practice
(1/5)
1. What does the md:bg-red-500 class do in Tailwind CSS?
easy
A. It applies a red background only on medium screens and larger.
B. It applies a red background on all screen sizes.
C. It applies a red background only on small screens.
D. It applies a red background only on large screens.
Solution
Step 1: Understand the prefix md:
The md: prefix in Tailwind means the style applies starting from medium screen sizes (usually 768px and up).
Step 2: Apply the utility with the prefix
The utility bg-red-500 sets the background color to red. With md:, it only applies on medium and larger screens.
Final Answer:
It applies a red background only on medium screens and larger. -> Option A
Quick Check:
Prefix md: = medium and up [OK]
Hint: Remember prefixes apply styles from that screen size up [OK]
Common Mistakes:
Thinking md: applies on all screen sizes
Confusing md: with sm: or lg:
Assuming it applies only on medium screens, not larger
2. Which of the following is the correct way to apply a blue text color only on large screens using Tailwind?
easy
A. text-blue-500 lg:
B. text-lg-blue-500
C. lg:text-blue-500
D. blue-lg-text-500
Solution
Step 1: Identify the correct prefix syntax
In Tailwind, responsive prefixes come before the utility, separated by a colon, e.g., lg:.
Step 2: Combine prefix with utility
The correct format is lg:text-blue-500 to apply blue text on large screens and up.
Final Answer:
lg:text-blue-500 -> Option C
Quick Check:
Prefix + utility = lg:text-blue-500 [OK]
Hint: Prefix always goes before utility with a colon [OK]
The class list has two sm: prefixed background colors: sm:bg-blue-200 and sm:bg-red-400.
Step 2: Understand conflict in overrides
Both apply at small screen sizes, but only one can take effect. The last one sm:bg-red-400 will override the first, making the first redundant or confusing.
Final Answer:
Duplicate sm: prefixes with conflicting colors -> Option D
Quick Check:
Duplicate prefixes cause override conflicts [OK]
Hint: Avoid repeating same prefix with different utilities [OK]
Common Mistakes:
Ignoring duplicate prefixes
Thinking multiple same prefixes combine
Assuming missing colon error
5. You want a button that is green on small screens, yellow on medium, and red on large screens. Which Tailwind class list achieves this correctly?
hard
A. bg-green-500 md:bg-yellow-500 lg:bg-red-500
B. bg-green-500 lg:bg-yellow-500 md:bg-red-500
C. md:bg-green-500 bg-yellow-500 lg:bg-red-500
D. lg:bg-green-500 md:bg-yellow-500 bg-red-500
Solution
Step 1: Assign base color for smallest screens
Base utility without prefix applies to all screens by default, so bg-green-500 sets green on small screens.
Step 2: Override colors for medium and large screens
md:bg-yellow-500 applies yellow from medium screens up, and lg:bg-red-500 applies red from large screens up, overriding previous colors.
Step 3: Check order and prefixes
The order and prefixes in bg-green-500 md:bg-yellow-500 lg:bg-red-500 correctly apply colors in increasing screen sizes.
Final Answer:
bg-green-500 md:bg-yellow-500 lg:bg-red-500 -> Option A