By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Media queries are a fundamental tool in responsive web design, allowing developers to apply CSS styles based on the characteristics of the user's device, such as screen size and orientation. Mastering min-width, max-width, and orientation is crucial for creating adaptable and user-friendly web designs. These queries help tailor the user experience across various devices, from smartphones to desktops. If you get it wrong, your website may look broken or be unusable on certain devices, leading to a poor user experience and potential loss of engagement.
@media
css @media (min-width: 600px) { body { background-color: lightblue; } }
⚠️ Pitfall: Forgetting to include the @media rule will result in the styles not being applied conditionally.
Use min-width:
css @media (min-width: 768px) { .container { display: flex; } }
⚠️ Pitfall: Using min-width for smaller screens can lead to styles not being applied as expected.
Use max-width:
css @media (max-width: 600px) { .container { display: block; } }
⚠️ Pitfall: Using max-width for larger screens can result in styles not being applied correctly.
Combine min-width and max-width:
css @media (min-width: 600px) and (max-width: 1200px) { .container { display: grid; } }
⚠️ Pitfall: Incorrect range values can lead to styles not being applied to the intended screen sizes.
Use orientation:
css @media (orientation: portrait) { .header { height: 50px; } }
Experts view media queries as a tool for creating a seamless user experience across all devices. They think in terms of breakpoints and user needs, rather than specific device sizes. This approach allows them to design flexible and adaptable layouts that work well on any screen.
Exam trap: Questions that mix up min-width and max-width usage.
The mistake: Forgetting to include the @media rule.
Exam trap: Identifying incorrect media query syntax.
The mistake: Not considering both portrait and landscape orientations.
Exam trap: Scenarios that require handling both orientations.
The mistake: Using incorrect range values for combined min-width and max-width.
Scenario: A website needs to display a navigation bar differently on tablets and smartphones.Question: Write the media queries to apply the correct styles.Solution: 1. Define the media query for tablets using min-width.2. Define the media query for smartphones using max-width.
@media (min-width: 768px) { .navbar { display: flex; } } @media (max-width: 767px) { .navbar { display: block; } }
Answer: The navigation bar will display as a flexbox on tablets and as a block on smartphones.Why it works: min-width targets tablets, and max-width targets smartphones, applying the correct styles for each device.
Scenario: A webpage needs to adjust its layout when the device is in landscape mode.Question: Write the media query to apply the correct styles.Solution: 1. Define the media query for landscape orientation.
@media (orientation: landscape) { .content { display: grid; } }
Answer: The content will display as a grid in landscape mode.Why it works: The orientation media query applies styles specifically for landscape mode, enhancing usability.
@media (min-width: value)
@media (max-width: value)
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.