Media Queries and Breakpoints
Learning Objectives
- Write media queries for viewport and device conditions.
- Choose breakpoints based on content needs.
- Understand orientation, hover and pointer features.
- Use feature queries and reduced-motion preferences.
1. Basic Media Query
@media (min-width: 768px) {
.layout {
display: grid;
grid-template-columns: 1fr 1fr;
}
}The rules inside the block apply when the media condition matches. Min-width is commonly used for mobile-first enhancements.
2. Max-Width and Ranges
@media (max-width: 600px) {
.sidebar {
display: none;
}
}
@media (width >= 600px) and (width < 1000px) {
.cards {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}Modern range syntax is supported in current browsers, but check compatibility requirements when targeting older browsers.
3. Breakpoints
A breakpoint is a threshold where the layout changes because the content needs it. Common values such as 600px, 768px and 1024px are starting points, not universal standards.
4. Orientation
@media (orientation: landscape) {
.hero {
padding-block: 24px;
}
}Orientation describes the viewport's aspect orientation, not necessarily the physical orientation of a particular device.
5. Hover and Pointer
@media (hover: hover) and (pointer: fine) {
.button:hover {
filter: brightness(1.1);
}
}
@media (pointer: coarse) {
.button {
padding: 12px 16px;
}
}Hover and pointer media features describe input capabilities. Avoid making essential features available only on hover.
6. Feature Queries
.card {
padding: 16px;
}
@supports (display: grid) {
.card-list {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}@supports tests whether the browser supports a CSS declaration. It is not a guarantee that every related feature behaves identically.
7. Reduced Motion
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
scroll-behavior: auto !important;
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}This is an illustrative reduced-motion override. In production, target only the animations that need reducing and preserve meaningful state changes.