Responsive Design with CSS3 Media Queries

Intermediate

๐Ÿ“ฑ CSS3 Media Queries for Responsive Design

Media queries are a cornerstone of responsive web design, enabling styles to adapt to various device characteristics.


๐Ÿงช Basic Media Query Example

@media (max-width: 768px) {
  body {
    background-color: lightgray;
  }
}

โœ… Applies styles only when the screen width is 768px or less.


๐Ÿ“ Common Media Query Features

  • min-width / max-width: screen size
  • orientation: portrait or landscape
  • resolution: pixel density

๐Ÿ“ Combine with Flexible Units

Use %, vw, vh, em, and rem for scalable layouts:

.container {
  width: 90vw;
  padding: 2em;
}

๐Ÿงฑ Example: Responsive Layout Shift

/* Desktop */
@media (min-width: 1024px) {
  .content {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}

/* Mobile */
@media (max-width: 768px) {
  .content {
    display: block;
  }
}

โœ… Switches from two columns to a single column on smaller screens.


๐ŸŽฏ Benefits of Responsive Design

  • ๐Ÿ”“ Improves accessibility
  • ๐Ÿค Enhances user experience
  • ๐Ÿ” Boosts SEO performance
  • ๐Ÿ“ฑ Optimizes content for all device sizes

Mastering media queries ensures your websites are flexible, accessible, and device-friendly.