02. Media Queries (@media, Breakpoints & Accessibility)
🟡 Intermediate
1. Definition
Media Queries (@media (min-width: 768px) { ... }) apply conditional CSS rules based on device viewport width, orientation, hover capability (@media (hover: hover)), or accessibility preferences (prefers-reduced-motion, forced-colors).
2. Why It Is Used
Media Queries allow overriding base layout rules for tablet and desktop viewports, creating multi-column grids on large screens while retaining clean single-column stacks on mobile.
3. How It Works
The browser continuously evaluates media query expressions against window size or system settings. When conditions evaluate to true, the enclosed CSS rule set is appended to the cascade.
4. Syntax
/* Mobile-First Min-Width Breakpoint */
@media (min-width: 768px) {
.grid-container {
grid-template-columns: 1fr 1fr;
}
}
5. Media Feature Queries Reference Table
| Media Feature | Condition Syntax | Best Practice Usage |
|---|---|---|
| Viewport Width | @media (min-width: 768px) | Mobile-first layout enhancements |
| Hover Capability | @media (hover: hover) | Styles hover effects ONLY for mouse pointers (prevents sticky mobile hover) |
| Motion Sensitivity | @media (prefers-reduced-motion: reduce) | Disables heavy animations for accessible users |
| Color Scheme | @media (prefers-color-scheme: dark) | Dark mode operating system preference detection |
6. Beginner Example
@media (min-width: 640px) {
.nav-links { display: flex; }
}
7. Intermediate Example
/* Hover styles restricted to real mouse pointers */
@media (hover: hover) and (pointer: fine) {
.btn:hover {
background-color: #1d4ed8;
transform: translateY(-2px);
}
}
8. Advanced Accessible Breakpoint System Example
/* Mobile Base */
.card-grid { display: flex; flex-direction: column; gap: 16px; }
/* Tablet */
@media (min-width: 768px) {
.card-grid { display: grid; grid-template-columns: repeat(2, 1fr); }
}
/* Desktop */
@media (min-width: 1024px) {
.card-grid { grid-template-columns: repeat(4, 1fr); }
}
9. Line-by-Line Code Explanation
@media (hover: hover) and (pointer: fine): Ensures hover animations run strictly on desktop mouse setups, preventing sticky tap-hover bugs on touchscreen phones.
10. Expected Terminal / Browser Output
A multi-column desktop interface that smoothly collapses into a single-column layout on mobile devices.
11. Common Mistakes & Pitfalls
- Using desktop-first
max-widthqueries everywhere: Writingmax-widthmedia queries leads to high selector specificity confusion. Use mobile-firstmin-widthinstead.
12. Important Rules & Limitations
- Order
min-widthmedia queries in ascending pixel order (e.g. 640px → 768px → 1024px → 1280px).
13. Multilingual Explanation: English
Media Queries apply conditional styling based on screen width or system preferences. Use min-width for mobile-first designs.
14. Multilingual Explanation: Hindi (Roman Script)
Media Queries @media (min-width: 768px) screen size ke anusar styles change karte hain. Mobile-first strategy ke sath min-width use karein.
15. Multilingual Explanation: Marathi (Roman Script)
Media Queries mule screen resolution nusar layout badalto. min-width cha waparn kara.
16. Practice Questions
- Why does
@media (hover: hover)prevent touch device hover bugs?
17. Practical Coding Challenge
Write a mobile-first media query that changes .site-header background to #0f172a at min-width: 1024px.