CSS Media Queries

CSS Media Queries are a formidable tool whenever you want to style a website differently based on one of several different conditions including the screen width, device type, orientation, etc. Media queries are by nature dynamic; they help you to create geography responsive designs that look good on anything from a phone to a desktop computer.

Why Use CSS Media Queries?

Historically, developers used to manually create different versions of a website for every device type. CSS Media Queries eliminate this necessity, and allow you apply styles based on the viewport size or device capabilities. Here are some reasons why you should be using CSS Media Queries:

Basic Syntax of CSS Media Queries

The basic syntax for a CSS Media Query looks like this:


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

Media queries can utilize any number of conditions, such as screen width / height and resolution. Also, orientation and media type are valuable in targeting and applying styles. The CSS rules inside the query will only apply when the condition is true.

Example of a Simple Media Query


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

This media query changes the background color to light blue when the viewport width is 768px or less (typically for tablets and mobile devices).

Common Media Query Conditions

Here are some of the most commonly used conditions in CSS Media Queries:

Examples of CSS Media Queries

Making the Layout Responsive

Here’s an example of using CSS Media Queries to create a responsive layout that changes based on the screen size:


          
            .container  {
                display: grid;
                   grid-template-columns: 1fr 1fr 1fr;
                   gap: 20px;
                }

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

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

This structure displays three equal width columns at large size, while displays two column on tablets and one column on mobile. The grid-template-columns property changes based on the screen width.

Changing Typography Based on Screen Size

Another common use case for Media Queries is adjusting typography for different screen sizes:


              h1  {
                  font-size: 36px;
              }
              
              @media (max-width: 768px) {
                  h1  {
                      font-size: 28px;              }
                  }
              
              @media (max-width: 480px) {
                  h1  {
                      font-size: 24px;              }
                  }

This example reduces the font size of h1 headers as the screen size decreases, ensuring that text doesn’t overflow on smaller devices.

Advanced Usage of Media Queries

Using Multiple Media Queries

The other flexibility with CSS Media Queries is that they are essential in responsive web design, where they give you the capability to develop fluid and responsive layouts that will adapt on any devices. CSS Media Queries provide you the opportunity to design any website to be visually pleasing and improve user experience on all screen sizes or devices. You can also use multiple Media Queries for more complex layouts. CSS Media Query rules are read from top to bottom and can be targeted for specific screen sizes across a particular range:


              @media (min-width: 1200px) {
                  /* Styles for large screens (desktops) */ 
              }

              @media (min-width: 1199px) and (min-width: 768px)  {
                  /* Styles for tablets */ 
              }

              @media (min-width: 767px) {
                  /* Styles for mobile devices */ 
              }

Media Queries with Specific Devices

You can also target specific devices using media queries based on their device characteristics. For example, you can target devices with touch capabilities:


              @media (pointer: coarse) {
                  /* Styles for touch devices */ 
              }

Best Practices for Using CSS Media Queries

Conclusion

CSS Media Queries are a great resource for responsive web design, allowing you to create "flexible and adaptive" layouts to work across multiple devices. CSS Media Queries allow you to make any site look great and improve the user experience, no matter what screen size and devices are being used.

Try it Yourself