πŸ“‘ @media β€” The Heart of Responsive CSS

🌐 What is @media?

@media is a CSS at-rule used to apply styles only when certain conditions (like screen width, orientation, resolution, or device type) are met. It is the core of responsive design, enabling layouts to adapt to different screens.

>>β€œ@media makes your CSS react to the user’s device β€” not the other way around.”

🎯 Why Use @media?

  • πŸ“± Adjust layout for different screen sizes
  • πŸ’» Improve readability on large displays
  • 🌍 Make designs mobile-friendly
  • βš™οΈ Apply conditional styling for orientation or dark mode
  • πŸ–₯️ Optimize spacing, font sizes, and grids across breakpoints

πŸ“Œ Basic Syntax

Basic @media Syntax

@media (condition) {
  /* CSS rules here */
}

🎯 Most Common Type β€” Width-Based Media Queries

πŸ‘‰ Max-width (Mobile-first responsive design)

max-width Example

@media (max-width: 768px) {
  .card {
    padding: 10px;
    font-size: 14px;
  }
}

Styles apply when the screen width is **768px or below**.

πŸ‘‰ Min-width (Progressive enhancement)

min-width Example

@media (min-width: 1024px) {
  .container {
    max-width: 1200px;
    padding: 40px;
  }
}

Styles apply when width is **1024px or above** β€” perfect for desktop enhancements.

πŸ“ Common Breakpoints (Industry Standard)

Device TypeBreakpoint
Mobilemax-width: 480px
Small Tabletsmax-width: 768px
Large Tabletsmax-width: 1024px
Laptopsmin-width: 1200px

Note

πŸ’‘ These breakpoints are **guidelines**, not rules. Base decisions on your layout β€” not specific devices.

🧠 Combining Conditions

πŸ‘‰ Width + Orientation

Orientation Example

@media (max-width: 768px) and (orientation: landscape) {
  .banner {
    height: 200px;
  }
}
πŸ‘‰ Dark Mode Detection

Dark Mode Media Query

@media (prefers-color-scheme: dark) {
  body {
    background: #111;
    color: #eee;
  }
}
πŸ‘‰ High-Resolution Displays (Retina Screens)

High DPI Example

@media (min-resolution: 2dppx) {
  .logo {
    background-image: url("logo@2x.png");
  }
}

πŸ§ͺ Practical Example β€” Responsive Navigation

Responsive Navbar

.nav {
  display: flex;
  gap: 20px;
}

@media (max-width: 600px) {
  .nav {
    flex-direction: column;
    gap: 10px;
  }
}

Navbar switches from horizontal β†’ vertical layout on mobile screens.

πŸ”₯ Practical Example β€” Responsive Grid

Responsive Grid with @media

.gallery {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

@media (max-width: 900px) {
  .gallery {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 600px) {
  .gallery {
    grid-template-columns: 1fr;
  }
}

Grids shrink from 4 β†’ 2 β†’ 1 column based on device size.

πŸ“± Mobile-First vs Desktop-First

ApproachUsesQuery Type
Mobile-FirstStart small, scale upmin-width
Desktop-FirstStart large, scale downmax-width

Note

πŸ’‘ Modern CSS prefers mobile-first because it creates cleaner, efficient code.

⚠️ Common Mistakes

  • Using too many breakpoints β€” keep it simple
  • Using device-specific widths like 375px or 414px
  • Not testing on real devices or simulators
  • Writing contradictory min-width & max-width rules
  • Forgetting that @media applies after base styles

πŸ”₯ Summary

@media is the foundation of responsive design. By controlling styles based on screen size, orientation, resolution, and preferences, you can build interfaces that look great everywhere.

>>β€œMaster @media, and your website becomes fully adaptable β€” truly responsive.”

Learn more β†’MDN Docs πŸ“˜