Skip to main content

Command Palette

Search for a command to run...

CSS Media Queries

Published
โ€ข2 min read

๐Ÿ“ฑ 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)

/* 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)

DeviceWidth
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 browsers

  • Optional 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.


More from this blog

Frontend ki baat cheet

9 posts