CSS CSS Tutorial CSS Advanced CSS Responsive Web Design(RWD) CSS Grid CSS Properties Sass Tutorial Sass Functions



CSS Media Queries

CSS Media Queries are a powerful tool that allows web developers to create responsive designs that adapt to different screen sizes and devices. With media queries, you can specify different styles for different devices, such as desktops, tablets, and smartphones.

Media queries work by checking the characteristics of the device that is being used to view the website, such as the screen size, resolution, and orientation. Based on these characteristics, the browser applies the appropriate styles to the page.

Media queries are written using the @media rule in CSS. The @media rule specifies the media type and the conditions that must be met for the styles to be applied. Here is an example of a media query that applies styles to screens that are 768 pixels wide or less:

  
    @media screen and (max-width: 768px) {
      /* Styles for screens 768px wide or less */
    }
  

In this example, the media type is "screen", which means that the styles will only be applied to devices with a screen, such as desktops, laptops, tablets, and smartphones. The condition is "(max-width: 768px)", which means that the styles will only be applied to screens that are 768 pixels wide or less.

Media queries can also be used to specify different styles for different orientations, such as landscape and portrait. Here is an example of a media query that applies styles to screens that are in landscape orientation:

  
    @media screen and (orientation: landscape) {
      /* Styles for screens in landscape orientation */
    }
  

Media queries can also be combined to create more complex conditions. Here is an example of a media query that applies styles to screens that are between 768 and 1024 pixels wide:

  
    @media screen and (min-width: 768px) and (max-width: 1024px) {
      /* Styles for screens between 768px and 1024px wide */
    }
  

Media queries are an essential tool for creating responsive designs that work well on all devices. By using media queries, you can ensure that your website looks great and functions well, no matter what device your visitors are using.

References

Activity