Mobile-first means designing websites starting with small screens first. This helps make sure the site works well on phones before adding styles for bigger screens.
Mobile-first philosophy in Tailwind
/* Tailwind uses small screen styles by default. Use prefixes like 'md:', 'lg:' to add styles for bigger screens. */ <div class="p-4 bg-blue-500 md:bg-green-500 lg:bg-red-500"> Content here </div>
Tailwind applies styles without prefixes to all screen sizes starting from mobile.
Use prefixes like md: or lg: to change styles on medium or large screens.
<div class="text-sm md:text-lg">
Text size changes from small on mobile to large on medium screens.
</div><button class="bg-blue-500 hover:bg-blue-700 md:bg-green-500">
Click me
</button><div class="p-2 md:p-6 lg:p-10">
Padding grows as screen size increases.
</div>This page shows a colored box that changes color and text size as the screen gets bigger. On a phone, the box is blue with smaller text. On a tablet, it turns green with medium text. On a desktop, it becomes red with larger text.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Mobile-first Tailwind Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="bg-gray-100"> <main class="max-w-md mx-auto p-4"> <h1 class="text-xl font-bold text-center mb-4">Mobile-first Design</h1> <div class="bg-blue-400 text-white p-4 rounded md:bg-green-500 lg:bg-red-600"> <p class="text-base md:text-lg lg:text-xl"> This box is blue on small screens, green on medium, and red on large screens. </p> </div> </main> </body> </html>
Always include the viewport meta tag for proper mobile scaling.
Start styling without prefixes for mobile, then add prefixes for bigger screens.
Test your design by resizing the browser or using device simulation in browser DevTools.
Mobile-first means design for small screens first, then add styles for bigger screens.
Tailwind applies styles mobile-first by default, using prefixes like md: for bigger screens.
This approach helps make websites fast and easy to use on phones.
Practice
Solution
Step 1: Understand mobile-first philosophy
Mobile-first means starting design with small screens like phones, then adding styles for bigger screens.Step 2: Compare options
Only Designing for small screens first, then adding styles for larger screens correctly describes this approach; others describe desktop-first or fixed layouts.Final Answer:
Designing for small screens first, then adding styles for larger screens -> Option AQuick Check:
Mobile-first = Start small screens first [OK]
- Thinking mobile-first means designing desktop first
- Confusing mobile-first with ignoring screen sizes
- Believing mobile-first means only designing for phones
Solution
Step 1: Understand Tailwind screen prefixes
In Tailwind,md:applies styles at medium screen size and above.Step 2: Identify correct prefix for medium screens
md:bg-blue-500 usesmd:, so it applies on medium and larger screens. Others apply on small, large, or all screens.Final Answer:
md:bg-blue-500 -> Option AQuick Check:
md:means medium screens and up [OK]
- Confusing sm: with md:
- Using no prefix and expecting medium-only styles
- Mixing lg: with md:
<div class="bg-red-500 md:bg-green-500">Box</div>What background color will the box have on a small screen?
Solution
Step 1: Analyze classes on small screen
The classbg-red-500applies to all screens by default. Themd:bg-green-500applies only on medium and larger screens.Step 2: Determine active class on small screen
On small screens,md:bg-green-500is ignored, so background stays red.Final Answer:
Red -> Option BQuick Check:
Default class applies on small screens [OK]
- Thinking md: classes apply on small screens
- Ignoring default classes
- Assuming no color if md: class exists
<p class="md:text-red-500 text-blue-500">Hello</p>
Solution
Step 1: Understand class application order
Tailwind applies base classes first, then overrides with media prefixes likemd:.Step 2: Check if code matches goal
text-blue-500applies on all screens, overridden bymd:text-red-500on medium+ screens, matching the goal.Final Answer:
No error; it works as intended -> Option CQuick Check:
Base then md: override works [OK]
- Thinking order of classes matters in Tailwind
- Believing md: prefix must be on base class
- Assuming Tailwind can't override colors
Solution
Step 1: Understand mobile-first width classes
Usew-fullfor full width on small screens (default). Usemd:w-[200px]to set width 200px on medium+ screens.Step 2: Check options for correct order and values
w-full md:w-[200px] sets full width by default, then overrides to 200px on medium screens. Others reverse or duplicate incorrectly.Final Answer:
w-full md:w-[200px] -> Option DQuick Check:
Default full width, md override 200px [OK]
- Reversing order of width classes
- Using md: on full width instead of fixed width
- Duplicating conflicting width classes
Ready to prove it?
Pass the test and this pattern moves unknown
