What if your website could magically change its look to fit any screen perfectly?
Why Media queries in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you create a website that looks great on your big desktop screen. Then you try to view it on a phone, but the text is tiny and buttons are too close together.
If you try to fix this by making separate versions of your site for each device and switching them manually, it becomes confusing and takes a lot of time. You might forget to update one version or make mistakes.
Media queries let your website automatically adjust its style based on the screen size or device type. This means one site can look good everywhere without extra copies.
/* Desktop styles only */
body { font-size: 16px; }
/* No changes for small screens, looks bad */@media (max-width: 600px) { body { font-size: 14px; } }
Media queries enable your website to respond and look great on any device, from phones to large monitors.
Think of a news website that shows a full menu on desktop but switches to a simple hamburger menu on phones for easy navigation.
Manual styling for each device is slow and error-prone.
Media queries let styles change automatically based on screen size.
This makes websites flexible and user-friendly everywhere.
Practice
@media queries?Solution
Step 1: Understand what
@mediadoes@mediaqueries let CSS change styles depending on device features like screen width.Step 2: Compare options to this purpose
Only To apply different styles based on device screen size or features describes applying styles based on screen size or features, which matches@mediausage.Final Answer:
To apply different styles based on device screen size or features -> Option CQuick Check:
Media queries = responsive styles [OK]
- Confusing media queries with animations
- Thinking media queries link CSS files
- Mixing media queries with CSS variables
Solution
Step 1: Recall correct media query syntax for max-width
The correct syntax uses@media (max-width: 600px)with parentheses and colon.Step 2: Check each option
@media (max-width: 600px) { /* styles here */ } matches the correct syntax. @media screen and (min-width: 600px) { /* styles here */ } uses min-width, which is opposite. @media (width < 600px) { /* styles here */ } uses invalid syntax. @media max-width: 600px { /* styles here */ } misses parentheses.Final Answer:
@media (max-width: 600px) { /* styles here */ } -> Option BQuick Check:
Use parentheses and colon for max-width [OK]
- Omitting parentheses around conditions
- Using min-width instead of max-width for smaller screens
- Writing conditions without colon
body have on a screen 500px wide?body { background-color: white; } @media (max-width: 600px) { body { background-color: lightblue; } }Solution
Step 1: Understand media query condition
The media query applies styles when screen width is 600px or less. 500px is less than 600px, so it applies.Step 2: Determine which background color applies
The media query sets background to lightblue, overriding the default white for this screen size.Final Answer:
Lightblue -> Option AQuick Check:
Screen 500px ≤ 600px uses media query color [OK]
- Ignoring media query and picking default style
- Confusing max-width with min-width
- Assuming no style applies if media query exists
@media max-width: 800px { p { font-size: 1.2rem; } }Solution
Step 1: Check media query syntax
The condition must be inside parentheses:@media (max-width: 800px).Step 2: Verify other parts
The selectorpis valid, and semicolon is present. Using max-width is correct if targeting screens 800px or less.Final Answer:
Missing parentheses around the condition -> Option DQuick Check:
Media query conditions need parentheses [OK]
- Forgetting parentheses around conditions
- Confusing max-width and min-width usage
- Missing semicolons inside CSS blocks
Solution
Step 1: Understand the requirement
Font size should be 1.5rem on screens wider than 900px, and 1rem on smaller screens.Step 2: Analyze each option
p { font-size: 1.5rem; } @media (max-width: 900px) { p { font-size: 1rem; } } sets default font size to 1.5rem, then uses a media query with max-width 900px to reduce font size to 1rem on smaller screens. This matches the requirement.Final Answer:
p { font-size: 1.5rem; } @media (max-width: 900px) { p { font-size: 1rem; } } -> Option AQuick Check:
Default large, smaller inside max-width media query [OK]
- Reversing min-width and max-width logic
- Setting default smaller and overriding larger incorrectly
- Missing default style outside media query
