CSS Media Queries — The Complete Guide for Responsive Web Design

CSS Media Queries allow you to apply CSS rules only when certain conditions are met — such as screen width, device orientation, color scheme, or resolution.

They’re the foundation of responsive web design.

Basic Syntax

@media (condition) {
  /* styles apply when condition is true */
}

Why Media Queries Matter

Most Common Media Queries

Start styling for small screens, then go up.

/* Phones */
@media (min-width: 480px) {}

/* Tablets */
@media (min-width: 768px) {}

/* Laptops */
@media (min-width: 1024px) {}

/* Desktops */
@media (min-width: 1280px) {}

Popular Screen Size Breakpoints

DeviceBreakpoint
Small Phones320px
iPhone (modern)375px
Large Phones414px
Tablets600–768px
Tablets Landscape1024px
Laptops1280px
Desktops1440px
Full HD monitors1920px

Recommended Breakpoints to Use:

@media (max-width: 600px)  { /* mobile */ }
@media (min-width: 600px)  { /* tablets */ }
@media (min-width: 900px)  { /* tablet landscape */ }
@media (min-width: 1200px) { /* laptop */ }
@media (min-width: 1800px) { /* large desktops */ }

Media Query Types and Conditions

1. Width-based queries (most common)

Max-width

@media (max-width: 600px) {
  body { background: lightblue; }
}

Min-width

@media (min-width: 1024px) {
  body { background: green; }
}

2. Orientation Queries

Detects device position.

@media (orientation: portrait) {
  .image { height: 400px; }
}

@media (orientation: landscape) {
  .image { height: 200px; }
}

3. Dark Mode Queries

@media (prefers-color-scheme: dark) {
  body { background: #111; color: #eee; }
}

4. High DPI / Retina Displays

@media (min-resolution: 2dppx) {
  img { image-rendering: crisp-edges; }
}

Useful for:

5. Hover & Pointer Detection

Great for touch vs mouse devices.

@media (hover: none) {
  a:hover { text-decoration: none; }
}

@media (pointer: coarse) {
  /* touch devices */
  button { padding: 18px; }
}

Combining Multiple Conditions

You can join them using and, or, not.

@media (min-width: 768px) and (orientation: landscape) {
  /* tablet landscape */
}
@media (max-width: 600px), (orientation: portrait) {
  /* applies if either condition is true */
}

Mobile-First vs Desktop-First

Mobile First (recommended)

Use min-width queries.

@media (min-width: 768px) {
  /* bigger screens */
}

Desktop First

Use max-width queries.

@media (max-width: 1024px) {
  /* smaller screens */
}

Advanced Media Queries (Modern CSS)

Container Queries (2023+)

Changes styling based on the size of a container, not the viewport.

@container (min-width: 500px) {
  .card { display: flex; }
}

Forced Colors Mode

For accessibility and high-contrast themes.

@media (forced-colors: active) {
  * { forced-color-adjust: none; }
}

Reduced Motion

Detect users who prefer minimal animations.

@media (prefers-reduced-motion: reduce) {
  .animation { animation: none; }
}

Best Practices for Media Queries

Ready-to-Use Media Query Boilerplate

/* Mobile First Layout */
@media (min-width: 480px) { /* small phones */ }
@media (min-width: 768px) { /* tablets */ }
@media (min-width: 1024px) { /* laptops */ }
@media (min-width: 1280px) { /* desktops */ }
@media (min-width: 1536px) { /* large desktops */ }

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *