Responsive Card Layout with Tailwind Breakpoint Prefixes
📖 Scenario: You are building a simple card layout for a website. The cards should look good on all screen sizes, from small phones to large desktop monitors.
🎯 Goal: Create a responsive card layout using Tailwind CSS breakpoint prefixes sm, md, lg, xl, and 2xl to adjust the number of cards per row at different screen widths.
📋 What You'll Learn
Use Tailwind CSS breakpoint prefixes to control layout
Start with a single column on very small screens
Show two cards per row on small screens (sm)
Show three cards per row on medium screens (md)
Show four cards per row on large screens (lg)
Show five cards per row on extra large screens (xl)
Show six cards per row on 2xl screens
Use semantic HTML and accessible markup
💡 Why This Matters
🌍 Real World
Responsive card layouts are common on websites for product listings, blog posts, or portfolios. Using breakpoint prefixes helps the layout adapt smoothly to different devices.
💼 Career
Web developers often use Tailwind CSS to build responsive interfaces quickly. Understanding breakpoint prefixes is essential for creating layouts that work well on phones, tablets, and desktops.
Progress0 / 4 steps
1
Create the HTML structure with 6 cards
Create a <main> element with a class of container mx-auto p-4. Inside it, create a <section> with a class of grid gap-4. Inside the section, add 6 <article> elements each with a class of bg-white p-6 rounded shadow. Each article should contain an <h2> with text Card 1 through Card 6 respectively.
Tailwind
Hint
Use a main container with padding and center alignment. Inside, use a section with grid and gap. Add 6 article cards with white background, padding, rounded corners, and shadow. Each card has a heading with the card number.
2
Add the base grid columns for smallest screens
Add the Tailwind class grid-cols-1 to the section element to make the grid show 1 column on the smallest screens.
Tailwind
Hint
Add grid-cols-1 to the section's class list to show one column on the smallest screens.
3
Add breakpoint prefixes for responsive columns
Add these Tailwind classes to the section element to set columns at different breakpoints: sm:grid-cols-2, md:grid-cols-3, lg:grid-cols-4, xl:grid-cols-5, and 2xl:grid-cols-6.
Tailwind
Hint
Add the breakpoint prefixes with grid-cols-N classes to the section to control columns on different screen sizes.
4
Add accessible headings and finalize layout
Ensure each <article> has an <h2> heading with the card number text from Card 1 to Card 6. Confirm the main and section elements have the correct classes for container and grid layout with breakpoints.
Tailwind
Hint
Check that all cards have headings and the container and grid classes are correct for responsive layout.
Practice
(1/5)
1. What does the md: prefix in Tailwind CSS do?
easy
A. Applies styles on all screen sizes
B. Applies styles only on medium and larger screens
C. Applies styles only on small screens
D. Applies styles only on extra large screens
Solution
Step 1: Understand Tailwind breakpoint prefixes
Breakpoint prefixes like md: apply styles starting from that screen size and up.
Step 2: Identify what md: means
md: applies styles on medium screens (768px) and larger devices.
Final Answer:
Applies styles only on medium and larger screens -> Option B
Quick Check:
md: = medium and up [OK]
Hint: Remember: prefix applies from that size and up [OK]
Common Mistakes:
Thinking prefix applies only at exact size
Confusing sm and md breakpoints
Assuming prefix applies to smaller screens
2. Which of these is the correct way to make text center-aligned only on large screens and bigger using Tailwind?
easy
A. lg:text-center
B. text-center:lg
C. text-center-lg
D. text-center lg
Solution
Step 1: Recall Tailwind syntax for breakpoints
Breakpoint prefixes come before the utility class, separated by a colon, e.g., lg:text-center.
Step 2: Check each option
Only lg:text-center uses correct prefix + utility format.
Final Answer:
<code>lg:text-center</code> -> Option A
Quick Check:
Prefix + colon + utility = correct syntax [OK]
Hint: Prefix always goes before utility with a colon [OK]
What will be the text alignment on a screen width of 1024px?
medium
A. Left aligned
B. Center aligned
C. Right aligned
D. Justified
Solution
Step 1: Identify screen size and breakpoints
1024px is considered large (lg) screen in Tailwind (lg starts at 1024px).
Step 2: Determine which classes apply
At 1024px, lg:text-right applies, overriding smaller breakpoints.
Final Answer:
Right aligned -> Option C
Quick Check:
lg breakpoint overrides md and base [OK]
Hint: Largest matching breakpoint prefix wins [OK]
Common Mistakes:
Choosing center alignment at 1024px
Ignoring lg breakpoint
Assuming base style always applies
4. You wrote this class: sm:bg-red-500 md:bg-green-500 lg:bg-blue-500. But on a 500px wide screen, the background is still blue. What is the likely problem?
medium
A. Tailwind breakpoints are mobile-first, so larger prefixes override smaller ones
B. The order of classes is wrong; lg: should come first
C. You forgot to include the base background color without prefix
D. The lg: prefix applies to all screen sizes
Solution
Step 1: Check screen size vs breakpoints
500px is below sm (640px+), so none of the prefixed classes (sm:, md:, lg:) apply at this size.
Step 2: Identify the missing piece
However, if the background is still blue at 500px, it means the lg:bg-blue-500 class is applying at all screen sizes, which is incorrect behavior.
Step 3: Likely problem
This usually happens if the lg: prefix is missing the colon or is malformed, causing the class to be interpreted as a normal class and applied globally.
Final Answer:
The lg: prefix applies to all screen sizes -> Option D
Quick Check:
Check for correct prefix syntax to ensure breakpoint applies properly [OK]
Hint: Set base styles first for smallest screens, then override with prefixes [OK]
Common Mistakes:
Thinking sm: applies below 640px
Believing class order determines breakpoint priority
Forgetting base class for mobile screens
5. You want a button to have a small padding on phones, medium padding on tablets, and large padding on desktops and bigger. Which Tailwind classes achieve this?
hard
A. p-2 md:p-4 lg:p-6
B. p-6 md:p-4 lg:p-2
C. sm:p-2 md:p-4 xl:p-6
D. p-2 sm:p-4 md:p-6
Solution
Step 1: Understand base and breakpoint prefixes
Base class p-2 applies to all screens, then md:p-4 overrides on medium (tablet) and up, lg:p-6 overrides on large (desktop) and up.
Step 2: Check each option
p-2 md:p-4 lg:p-6 correctly sets small padding base, medium padding at md, and large padding at lg.
Final Answer:
<code>p-2 md:p-4 lg:p-6</code> -> Option A
Quick Check:
Base + md + lg prefixes for increasing sizes [OK]
Hint: Base class for smallest, then increase with md, lg [OK]
Common Mistakes:
Reversing padding sizes
Using sm prefix instead of base for smallest
Skipping breakpoint for medium screens
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.