CSS Media Queries
๐ฑ CSS Media Queries โ Simple Cheat Sheet
Media queries let you apply CSS only when certain conditions are true, like screen size, device type, or user preferences.
๐ง Basic Syntax
@media (condition) {
/* CSS rules */
}
Example:
@media (max-width: 600px) {
body {
background: lightblue;
}
}
๐ Applies only when screen width is 600px or less.
๐ Screen Width (Most Common)
Mobile First (Recommended โ )
/* Default = mobile */
@media (min-width: 768px) {
/* Tablet and up */
}
@media (min-width: 1024px) {
/* Laptop and up */
}
Desktop First
@media (max-width: 1024px) {
/* Tablet and below */
}
@media (max-width: 600px) {
/* Mobile */
}
๐ฑ Common Breakpoints (Easy to Remember)
| Device | Width |
| Small phones | โค 480px |
| Phones | โค 600px |
| Tablets | โฅ 768px |
| Laptops | โฅ 1024px |
| Large screens | โฅ 1200px |
๐ฅ๏ธ only screen Explained
@media only screen and (max-width: 600px) {
/* CSS */
}
screenโ targets screens (not print)onlyโ ignores very old browsersOptional in modern projects
๐งญ Orientation (Portrait / Landscape)
@media (orientation: portrait) {
/* Phone held vertically */
}
@media (orientation: landscape) {
/* Phone held horizontally */
}
โฟ User Preferences (Accessibility)
Reduce Motion (Very Important)
@media (prefers-reduced-motion: reduce) {
* {
animation: none !important;
transition: none !important;
}
}
๐ Respects users who dislike animations.
Dark Mode
@media (prefers-color-scheme: dark) {
body {
background: black;
color: white;
}
}
๐จ๏ธ Print Media
@media print {
nav, footer {
display: none;
}
}
๐ Applies when printing the page.
๐ Multiple Conditions
@media (min-width: 600px) and (max-width: 900px) {
/* Only tablets */
}
โ Common Mistakes
โ Forgetting px
(max-width: 600) /* WRONG */
โ Correct
(max-width: 600px)
โ Overusing too many breakpoints
๐ง Easy Mental Model
min-widthโ "From this size and bigger"max-widthโ "From this size and smaller"
โ Best Practices
โ Prefer mobile-first (min-width)
โ Keep breakpoints consistent
โ Use media queries only when layout breaks
โ Respect accessibility preferences
๐ One-Line Summary
Media queries help your website adapt to screen sizes, devices, and user preferences.