Media queries help your website look good on all devices by changing styles based on screen size or device features.
Media queries in CSS
@media (condition) { /* CSS rules here */ }
condition can check screen width, height, orientation, and more.and and not.@media (max-width: 600px) { body { background-color: lightblue; } }
@media (min-width: 601px) and (max-width: 1024px) { body { font-size: 1.2rem; } }
@media (orientation: portrait) { nav { display: none; } }
This page uses a media query to change background color and text size when the screen is 600px wide or smaller. Try resizing your browser window to see the effect.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Media Queries Example</title> <style> body { font-family: Arial, sans-serif; background-color: white; color: black; margin: 2rem; } h1 { font-size: 2rem; } p { font-size: 1rem; } /* Change background and text on small screens */ @media (max-width: 600px) { body { background-color: #f0f8ff; color: #003366; } h1 { font-size: 1.5rem; } p { font-size: 1.2rem; } } </style> </head> <body> <h1>Welcome to My Website</h1> <p>Resize the browser window to see the background and text size change on small screens.</p> </body> </html>
Always include the viewport meta tag in your HTML to make media queries work well on mobile devices.
Test your site on different devices or use browser DevTools to simulate screen sizes.
You can combine multiple media queries for more control over your design.
Media queries let your website adapt to different screen sizes and device features.
Use them to improve readability and usability on phones, tablets, and desktops.
They are written inside CSS with the @media rule and conditions like max-width.
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
Ready to prove it?
Pass the test and this pattern moves unknown
