CSS Tutorial

Media Queries

Media queries allow you to adapt your website's design to different devices and screen sizes. They define breakpoints, allowing you to specify different styles for different ranges of screen sizes.

Key Concepts:

  • @media screen and (max-width: ...): The media query syntax. It specifies the media type (screen), the feature (max-width), and the breakpoint value.
  • Media types: Define the type of device (screen, print, speech).
  • Media features: Describe specific device characteristics (width, height, aspect ratio).
  • Breakpoint definitions: The specific screen size values used to define where one set of styles ends and another begins.

Example:

@media screen and (max-width: 768px) {
  /* Styles for mobile devices */
}

@media screen and (min-width: 769px) and (max-width: 1024px) {
  /* Styles for tablets */
}

@media screen and (min-width: 1025px) {
  /* Styles for desktop devices */
}

This example applies different styles based on screen width:

  • Mobile: Up to 768px
  • Tablet: 769px to 1024px
  • Desktop: 1025px and above

Accessibility and Ease of Use:

Media queries enhance accessibility by ensuring that users on different devices can access and navigate your website with ease. By adapting your design to different screen sizes, you make your website more inclusive and usable.