Max-width responsive variants help you control how wide elements can get on different screen sizes. This keeps your design neat and easy to read on phones, tablets, and desktops.
Max-width responsive variants in Tailwind
max-w-{size}
sm:max-w-{size}
md:max-w-{size}
lg:max-w-{size}
xl:max-w-{size}
2xl:max-w-{size}Replace {size} with Tailwind's predefined max-width sizes like xs, sm, md, lg, xl, 2xl, full, min, max, or custom values.
Prefix with screen size like sm: or lg: to apply max-width only on that screen size and up.
max-w-md
sm:max-w-lg
md:max-w-xl
lg:max-w-2xl
This example creates a white box centered on the page with padding and border. The box's maximum width changes as the screen gets bigger:
max-w-smon very small screenssm:max-w-mdon small screens and upmd:max-w-lgon medium screens and uplg:max-w-xlon large screens and up
This keeps the box from getting too wide on small devices but allows it to grow on bigger screens.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Max-width Responsive Variants Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-100 p-6"> <main class="mx-auto bg-white p-4 border rounded max-w-sm sm:max-w-md md:max-w-lg lg:max-w-xl"> <h1 class="text-xl font-bold mb-2">Responsive Max-Width Box</h1> <p>This box changes its maximum width depending on your screen size.</p> <p>Resize the browser window to see it get wider on bigger screens.</p> </main> </body> </html>
Use max-w-full if you want the element to never be wider than its container.
Combine max-width with padding and margin to keep content nicely spaced.
Test your design by resizing the browser or using browser DevTools device simulator.
Max-width responsive variants control how wide elements can be on different screen sizes.
Use prefixes like sm:, md:, lg: to apply max-width at specific breakpoints.
This helps keep your layout clean and readable on all devices.
Practice
md:max-w-lg do?Solution
Step 1: Understand the prefix
The prefixmd:md:means the style applies from medium screen sizes and up.Step 2: Interpret
The classmax-w-lgmax-w-lgsets the maximum width to a large size.Final Answer:
Sets the max-width to large only on medium screens and larger -> Option AQuick Check:
Prefixmd:+max-w-lg= Sets the max-width to large only on medium screens and larger [OK]
- Thinking the class applies on all screen sizes
- Confusing
md:withlg: - Assuming it applies only on small screens
Solution
Step 1: Identify the breakpoint for large screens
The prefix for large screens islg:.Step 2: Match max-width size to medium
The class for medium max-width ismax-w-md.Final Answer:
lg:max-w-md -> Option AQuick Check:
Large screen prefix + medium max-width = lg:max-w-md [OK]
lg: for large screens, then max-width size [OK]- Using
md:instead oflg: - Omitting the prefix and applying to all screens
- Mixing max-width sizes with wrong prefixes
<div class="max-w-sm md:max-w-lg lg:max-w-xl">Content</div>
What max-width applies when the screen width is 1024px (large breakpoint)?
Solution
Step 1: Identify the screen size breakpoint
1024px corresponds to the large (lg) breakpoint in Tailwind.Step 2: Determine which max-width applies at large breakpoint
At large screens,lg:max-w-xloverrides smaller breakpoints.Final Answer:
max-w-xl -> Option BQuick Check:
At 1024px,lg:classes apply = max-w-xl [OK]
- Choosing the smallest max-width instead of largest
- Ignoring the breakpoint order
- Thinking multiple max-widths apply simultaneously
<div class="max-w-md sm:max-w-lg md:max-w-sm">Box</div>
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 CQuick Check:
Responsive max-width should not shrink on bigger screens [OK]
- Thinking multiple max-width classes cause syntax errors
- Believing
sm:is invalid for max-width - Expecting missing
lg:to cause errors
Solution
Step 1: Assign max-width for mobile (default)
Usemax-w-smfor small max-width on mobile (no prefix).Step 2: Assign max-width for tablets (medium screens)
Usemd:max-w-mdto set medium max-width starting at medium breakpoint.Step 3: Assign max-width for desktops (large screens)
Uselg:max-w-xlto set extra-large max-width starting at large breakpoint.Final Answer:
max-w-sm md:max-w-md lg:max-w-xl -> Option DQuick Check:
Default small + medium at md + extra-large at lg = max-w-sm md:max-w-md lg:max-w-xl [OK]
- Using
sm:for mobile default instead of no prefix - Skipping the large breakpoint for desktop
- Mixing up breakpoint order
Ready to prove it?
Pass the test and this pattern moves unknown
