Media Queries Basics
- CSS Basics
- Selectors & Specificity
- Box Model & Units
- Typography & Text Styling
- Colors & Color Spaces
- Positioning & Stacking Context
- Flexbox
- Grid
- Grid Advanced
- Responsive Design
- Container Queries
- Pseudo-classes & Pseudo-elements
- Modern Selectors
- CSS Custom Properties
- CSS Functions
- Transitions & Animations
- Transforms
- Forms & Input Styling
- CSS Architecture & Methodologies
- Accessibility in CSS
- Performance & Optimization
- View Transitions & Modern APIs
- Cascade Layers & Scope
What a Media Query Does
A media query applies a block of CSS only when a condition about the browser or device is true - most commonly, the width of the viewport. This is the core mechanism behind responsive design.
.sidebar {
width: 300px;
}
@media (max-width: 768px) {
.sidebar {
width: 100%;
}
}Here, the sidebar is 300px wide normally, but becomes full width once the viewport shrinks to 768px or narrower.
Common Conditions to Test
| Condition | Applies when... |
|---|---|
max-width: 768px | Viewport is 768px or narrower |
min-width: 1024px | Viewport is 1024px or wider |
orientation: landscape | Width is greater than height |
prefers-color-scheme: dark | User's system is set to dark mode |
The Viewport Meta Tag Prerequisite
<meta name="viewport" content="width=device-width, initial-scale=1">Without this tag in the HTML head, mobile browsers render the page at a zoomed-out desktop width by default, and media queries based on viewport width will not behave as expected.
Combining Multiple Conditions
@media (min-width: 600px) and (max-width: 900px) {
/* applies only in this specific range - a "tablet" band */
}