The mobile-first approach 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 approach in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
@media (min-width: 600px) { /* styles for screens 600px and wider */ }
@media (min-width: ...) to add styles for larger screens./* Base styles for mobile */ body { font-size: 1rem; background-color: white; } /* Larger screens */ @media (min-width: 600px) { body { font-size: 1.2rem; background-color: lightgray; } }
/* Mobile first button style */ button { padding: 0.5rem 1rem; font-size: 1rem; } @media (min-width: 768px) { button { padding: 1rem 2rem; font-size: 1.25rem; } }
This page uses the mobile-first approach. The base styles are for small screens. When the screen is at least 600 pixels wide, the styles inside the media query make the text bigger, add background color, and make the button larger.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Mobile-first Example</title> <style> body { font-family: Arial, sans-serif; background-color: #ffffff; color: #333333; padding: 1rem; font-size: 1rem; } header { background-color: #007acc; color: white; padding: 1rem; text-align: center; border-radius: 0.5rem; } main { margin-top: 1rem; } button { background-color: #007acc; color: white; border: none; padding: 0.5rem 1rem; font-size: 1rem; border-radius: 0.3rem; cursor: pointer; } button:hover { background-color: #005f99; } /* Larger screens styles */ @media (min-width: 600px) { body { background-color: #f0f4f8; font-size: 1.125rem; } header { padding: 2rem; border-radius: 1rem; } button { padding: 1rem 2rem; font-size: 1.25rem; } } </style> </head> <body> <header> <h1>Welcome to Mobile-first Site</h1> </header> <main> <p>This page looks good on phones first, then adjusts for bigger screens.</p> <button>Click Me</button> </main> </body> </html>
Always include the meta viewport tag in your HTML to make mobile styles work correctly.
Write your CSS starting with the smallest screen styles first, then add media queries for bigger screens.
Test your site on different devices or use browser DevTools to see how it looks on various screen sizes.
Mobile-first means start styling for small screens first.
Use @media (min-width: ...) to add styles for bigger screens.
This approach helps your site work well on phones and desktops.
Practice
Solution
Step 1: Understand mobile-first approach
Mobile-first means you write CSS targeting small screens first, like phones.Step 2: Add styles for bigger screens
Then you use media queries withmin-widthto add or change styles for tablets and desktops.Final Answer:
Start styling for small screens first, then add styles for larger screens -> Option BQuick Check:
Mobile-first = Start small, add bigger [OK]
- Confusing mobile-first with desktop-first
- Using max-width instead of min-width for bigger screens
- Ignoring media queries completely
Solution
Step 1: Identify mobile-first media query
Mobile-first usesmin-widthto add styles for bigger screens.Step 2: Choose correct syntax with media type
Using@media screen and (min-width: 600px)targets screens wider than 600px correctly.Final Answer:
@media screen and (min-width: 600px) { ... } -> Option CQuick Check:
Mobile-first uses min-width with media type [OK]
- Using max-width instead of min-width for bigger screens
- Using 'width' instead of 'min-width'
- Confusing min-width and max-width
body { background: white; }
@media (min-width: 600px) { body { background: blue; } }
@media (min-width: 800px) { body { background: green; } }Solution
Step 1: Check base style
Base background is white for all screen sizes.Step 2: Check media queries for 700px width
700px is >= 600px but < 800px, so blue applies, green does not.Final Answer:
Blue -> Option AQuick Check:
700px triggers min-width 600px but not 800px [OK]
- Choosing green because 700 > 600 but ignoring 800px limit
- Choosing white ignoring media queries
- Confusing min-width with max-width
body { font-size: 14px; }
@media (max-width: 600px) { body { font-size: 16px; } }Solution
Step 1: Identify media query type
The snippet usesmax-width: 600px, which targets small screens.Step 2: Check mobile-first rule
Mobile-first uses base styles for small screens andmin-widthfor bigger screens, so this is reversed.Final Answer:
Media query should use min-width for mobile-first approach -> Option DQuick Check:
Mobile-first uses min-width, not max-width [OK]
- Thinking max-width is correct for mobile-first
- Ignoring base styles importance
- Assuming no error in reversed media query
Solution
Step 1: Set base style for small screens
Mobile-first means base style is for phones, so width 100% is base.Step 2: Use min-width media query for bigger screens
Use@media (min-width: 768px)to set width 200px for larger screens.Final Answer:
button { width: 100%; } @media (min-width: 768px) { button { width: 200px; } } -> Option AQuick Check:
Base small, min-width bigger = mobile-first [OK]
- Using max-width for bigger screens
- Setting desktop styles as base
- Reversing widths in media queries
