Breakpoint prefixes help you make your website look good on different screen sizes. They let you change styles depending on the device width.
Breakpoint prefixes (sm, md, lg, xl, 2xl) in Tailwind
prefix:utility
// Example:
md:text-center
// This means apply 'text-center' style only on medium screens and larger.Breakpoint prefixes are added before the utility class, separated by a colon.
They apply styles starting from that screen size and up (mobile-first).
sm:bg-red-500 md:bg-green-500 lg:bg-blue-500
text-sm md:text-lg xl:text-xl
hidden md:block
This page shows a heading and paragraph. Their text size and color change at different screen widths using breakpoint prefixes.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Breakpoint Prefixes Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <h1 class="text-base sm:text-lg md:text-xl lg:text-2xl xl:text-3xl 2xl:text-4xl font-bold"> Responsive Heading </h1> <p class="mt-4 text-gray-700 sm:text-red-500 md:text-green-500 lg:text-blue-500"> Watch my color and size change as you resize the browser window. </p> </body> </html>
Breakpoint prefixes follow a mobile-first approach: styles apply from the prefix size and up.
Common breakpoints: sm (640px), md (768px), lg (1024px), xl (1280px), 2xl (1536px).
You can combine multiple breakpoint prefixes to create complex responsive designs.
Breakpoint prefixes let you apply styles based on screen size.
They help make your website look good on phones, tablets, and desktops.
Use them by adding prefix: before utility classes, like md:text-center.
Practice
md: prefix in Tailwind CSS do?Solution
Step 1: Understand Tailwind breakpoint prefixes
Breakpoint prefixes likemd:apply styles starting from that screen size and up.Step 2: Identify what
md:meansmd:applies styles on medium screens (768px) and larger devices.Final Answer:
Applies styles only on medium and larger screens -> Option BQuick Check:
md: = medium and up [OK]
- Thinking prefix applies only at exact size
- Confusing sm and md breakpoints
- Assuming prefix applies to smaller screens
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
Onlylg:text-centeruses correct prefix + utility format.Final Answer:
<code>lg:text-center</code> -> Option AQuick Check:
Prefix + colon + utility = correct syntax [OK]
- Putting prefix after utility
- Using dash instead of colon
- Separating prefix and utility with space
<div class="text-left md:text-center lg:text-right">Hello</div>
What will be the text alignment on a screen width of 1024px?
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-rightapplies, overriding smaller breakpoints.Final Answer:
Right aligned -> Option CQuick Check:
lg breakpoint overrides md and base [OK]
- Choosing center alignment at 1024px
- Ignoring lg breakpoint
- Assuming base style always applies
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?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 thelg:bg-blue-500class is applying at all screen sizes, which is incorrect behavior.Step 3: Likely problem
This usually happens if thelg:prefix is missing the colon or is malformed, causing the class to be interpreted as a normal class and applied globally.Final Answer:
Thelg:prefix applies to all screen sizes -> Option DQuick Check:
Check for correct prefix syntax to ensure breakpoint applies properly [OK]
- Thinking sm: applies below 640px
- Believing class order determines breakpoint priority
- Forgetting base class for mobile screens
Solution
Step 1: Understand base and breakpoint prefixes
Base classp-2applies to all screens, thenmd:p-4overrides on medium (tablet) and up,lg:p-6overrides on large (desktop) and up.Step 2: Check each option
p-2 md:p-4 lg:p-6correctly 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 AQuick Check:
Base + md + lg prefixes for increasing sizes [OK]
- Reversing padding sizes
- Using sm prefix instead of base for smallest
- Skipping breakpoint for medium screens
Ready to prove it?
Pass the test and this pattern moves unknown
