Responsive utility overrides let you change styles on different screen sizes easily. This helps your website look good on phones, tablets, and desktops.
Responsive utility overrides in Tailwind
sm:<utility> md:<utility> lg:<utility> xl:<utility> 2xl:<utility>Use prefixes like sm:, md:, lg: to apply styles at different screen widths.
These prefixes override the base utility styles when the screen is at least that size.
<div class="text-center md:text-left">Hello</div><button class="p-2 lg:p-4">Click me</button><img class="hidden sm:block" src="image.jpg" alt="Example image">
This page shows a heading and a button that change size based on screen width. The heading is centered on small screens and left-aligned on medium and bigger screens, becoming very large on large screens. The button text size also grows on bigger screens. The paragraph text aligns center on small screens and left on bigger screens.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Utility Overrides Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-center text-sm md:text-left md:text-lg lg:text-3xl font-bold mb-4"> Responsive Heading </h1> <button class="bg-blue-500 text-white px-4 py-2 rounded text-xs sm:text-sm md:text-base lg:text-lg"> Responsive Button </button> <p class="mt-4 text-gray-700 text-center sm:text-left"> Resize the browser window to see text alignment and size change. </p> </body> </html>
Always test your responsive styles by resizing the browser or using device simulation in browser DevTools.
Use meaningful class names and keep your HTML clean by combining base and responsive utilities.
Remember that responsive prefixes apply styles only if the screen is at least that size.
Responsive utility overrides let you change styles for different screen sizes easily.
Use prefixes like sm:, md:, lg: before utilities to apply them at certain screen widths.
This helps make your website look good and work well on phones, tablets, and desktops.
Practice
md:bg-red-500 class do in Tailwind CSS?Solution
Step 1: Understand the prefix
Themd: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 utilitybg-red-500sets the background color to red. Withmd:, it only applies on medium and larger screens.Final Answer:
It applies a red background only on medium screens and larger. -> Option AQuick Check:
Prefixmd:= medium and up [OK]
- Thinking
md:applies on all screen sizes - Confusing
md:withsm:orlg: - Assuming it applies only on medium screens, not larger
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 islg:text-blue-500to apply blue text on large screens and up.Final Answer:
lg:text-blue-500 -> Option CQuick Check:
Prefix + utility =lg:text-blue-500[OK]
- Placing prefix after the utility
- Combining prefix and utility without colon
- Using incorrect order or spelling
<div class="bg-green-200 md:bg-yellow-300 lg:bg-red-400">Box</div>
What background color will the box have on a screen width of 800px?
Solution
Step 1: Determine screen size category for 800px
800px is between medium (768px) and large (1024px) screen sizes.Step 2: Check which background color applies
Basebg-green-200applies always.md:bg-yellow-300overrides from 768px up.lg:bg-red-400applies from 1024px up, so not yet active.Final Answer:
Yellow (md:bg-yellow-300) -> Option BQuick Check:
800px = medium range, somd:styles apply [OK]
- Choosing base color ignoring overrides
- Assuming large styles apply before 1024px
- Confusing order of overrides
<div class="sm:bg-blue-200 md:bg-green-300 sm:bg-red-400">Content</div>
Solution
Step 1: Look for repeated prefixes
The class list has twosm:prefixed background colors:sm:bg-blue-200andsm:bg-red-400.Step 2: Understand conflict in overrides
Both apply at small screen sizes, but only one can take effect. The last onesm:bg-red-400will override the first, making the first redundant or confusing.Final Answer:
Duplicatesm:prefixes with conflicting colors -> Option DQuick Check:
Duplicate prefixes cause override conflicts [OK]
- Ignoring duplicate prefixes
- Thinking multiple same prefixes combine
- Assuming missing colon error
Solution
Step 1: Assign base color for smallest screens
Base utility without prefix applies to all screens by default, sobg-green-500sets green on small screens.Step 2: Override colors for medium and large screens
md:bg-yellow-500applies yellow from medium screens up, andlg:bg-red-500applies red from large screens up, overriding previous colors.Step 3: Check order and prefixes
The order and prefixes inbg-green-500 md:bg-yellow-500 lg:bg-red-500correctly apply colors in increasing screen sizes.Final Answer:
bg-green-500 md:bg-yellow-500 lg:bg-red-500 -> Option AQuick Check:
Base + md: + lg: overrides = correct responsive colors [OK]
- Mixing prefix order incorrectly
- Placing larger screen prefixes before smaller
- Not setting base style for smallest screens
Ready to prove it?
Pass the test and this pattern moves unknown
