Beginner Topic 11 of 51 Web Development
Media Queries
CSS conditions applying styles by device characteristics.
How it works
Media queries apply CSS conditionally based on characteristics of the device or environment, such as viewport width, height, orientation, resolution, color scheme preference and reduced motion preference. The syntax is @media (min-width: 768px) { ... }, and queries can be combined with and, or and not. Range syntax and container queries extend the idea. Print and screen media types allow separate styles.
Step-by-step walkthrough
- The browser knows the viewport width.
- A media query tests it, @media (min-width: 768px).
- When true, the rules inside apply.
- The layout changes to two columns.
- Queries can also test dark mode, print and reduced motion.
Where it is used
Breakpoints for responsive layouts, dark mode with prefers-color-scheme, accessibility settings with prefers-reduced-motion and print stylesheets use media queries.
Common pitfall
Using max-width and min-width inconsistently creates overlapping or gap ranges. Too many device-specific breakpoints become hard to maintain. Overriding styles in many queries causes specificity problems. Prefer mobile-first min-width queries, choose breakpoints where the content breaks, and respect user preferences for motion and contrast.
Where it fits in the course
Media Queries is topic 11 of 51 in Web Development, rated Beginner. It follows Responsive Design. The next topic is JavaScript Basics.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Web Development quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Web Development quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Media Queries?
Where do people use Media Queries?
What is the most common mistake with Media Queries?
What should I learn before this topic?
How can I practice it?
Is Media Queries hard to learn?
How long does it take to learn Media Queries?
Which topics are related to Media Queries?
What should I study after Media Queries?
Related topics
- Responsive Design (Web Development)
- JavaScript Basics (Web Development)
- CSS Grid (Web Development)
- DOM (Web Development)
- SELECT Queries (Databases)