What media queries are and why they matter
Media queries are a CSS feature that allow content rendering to adapt to different screen sizes, resolutions, or device characteristics.
@media (max-width: 768px) {
body {
background-color: lightblue;
}
}
Note: You can target various properties like min-width, orientation, aspect-ratio, etc.
Commonly used query conditions
@media (min-width: 1024px) and (max-width: 1200px) {
.container {
padding: 2rem;
}
}
@media (orientation: landscape) {
body {
flex-direction: row;
}
}
@media (min-resolution: 2dppx) {
.icon {
background-image: url('high-res.png');
}
}
Using and, or, not in media queries
@media (min-width: 600px) and (orientation: portrait) {
/* styles */
}
@media not all and (monochrome) {
/* styles for color devices */
}
Writing clean and efficient queries
/* Default (mobile) */
body {
font-size: 14px;
}
/* Larger screen */
@media (min-width: 768px) {
body {
font-size: 16px;
}
}
Group shared styles outside the media query to reduce duplication.
Reference for features and values
Feature | Example Value | Description |
---|---|---|
width | 600px | Viewport width |
orientation | landscape | Device orientation |
resolution | 300dpi | Screen DPI |
Media query behavior with image layouts
@media (max-width: 768px) {
.image-container {
grid-auto-flow: row;
}
}