CSS Media Queries
In this page:
@media media-type and (condition) {
selector {
property: value;
}
}
Media Query Syntax
Media queries are conditional statements in CSS. They consist of a media type (like screen) and features (like min-width), allowing you to apply specific layout rules when the browser window crosses a defined pixel threshold.
Example: Media Query Syntax
<style>
@media screen and (min-width: 768px) {
.container {
width: 720px;
}
}
</style>
<div class="container" style="background:#cde; border:1px solid #369">Width becomes 720px at 768px+ viewports.</div>
Mobile-First Design (min-width)
Mobile-first design is the industry standard workflow. You write your default styles for small mobile screens first, then use min-width queries to add layout columns and spacing as screen sizes expand.
Example: Mobile-First Design (min-width)
<style>
.box {
display: block;
}
@media (min-width: 768px) {
.box {
display: flex;
}
}
</style>
<div class="box" style="background:#cde"><div style="background:#fdc; padding:8px">A</div><div style="background:#cfc; padding:8px">B</div></div>
Desktop-First Design (max-width)
Desktop-first design is an alternative workflow where you write your default styles for large desktop screens first, then use max-width queries to adjust, shrink, and stack elements as screen sizes shrink.
Example: Desktop-First Design (max-width)
<style>
.box {
display: flex;
}
@media (max-width: 768px) {
.box {
display: block;
}
}
</style>
<div class="box" style="background:#cde"><div style="background:#fdc; padding:8px">A</div><div style="background:#cfc; padding:8px">B</div></div>
Common Device Breakpoints
To make your responsive designs consistent, you should align your media query breakpoints with standard, widely used device widths (like 480px for mobile, 768px for tablets, and 1024px for desktops).
Example: Common Device Breakpoints
<style>
@media (min-width: 480px) { .box { padding: 12px; } }
@media (min-width: 768px) { .box { padding: 20px; } }
@media (min-width: 1024px) { .box { padding: 32px; } }
</style>
<div class="box" style="background:#cde; border:1px solid #369">Padding grows as the viewport widens.</div>
Combining Multiple Media Conditions
You can combine multiple media features using the and and comma (which acts like or) operators to target specific screen ranges (like devices between 600px and 900px wide).
Example: Combining Multiple Media Conditions
<style>
@media (min-width: 600px) and (max-width: 900px) {
.box {
background: lightblue;
}
}
</style>
<div class="box" style="border:1px solid #369; padding:12px">Light blue only between 600px and 900px wide.</div>
- Forgetting to write standard viewport meta configuration tags in your HTML headers, which breaks media query rendering on mobile devices.
- Omitting required spaces between media condition keywords (e.g., writing and(min-width: 768px) without a space).
- Using overlapping max-width and min-width boundaries on identical breakpoints, causing layout conflicts.
- Media queries are conditional statements used to apply styling rules based on screen widths.
- A mobile-first workflow uses min-width queries to adapt small layouts for larger screens elegantly.
- Use common breakpoints (like 480px, 768px, and 1024px) to ensure layout consistency across all digital devices.
Standard CSS media queries and responsive display features are supported natively by all web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline