Responsive Design

How do media queries help create responsive designs?

Getting Started

If you’re a web developer or designer, you’ve probably heard of responsive design.

It’s the practice of designing websites that can adapt to different screen sizes, from desktops to mobile devices.

And one of the key tools for achieving responsive design is media queries.

Media queries are CSS rules that allow you to apply different styles to different devices based on their screen size or other characteristics.

For example, you might want to display a three-column layout on a desktop, but switch to a one-column layout on a mobile device.

Learning media queries is essential for anyone who wants to create modern, responsive websites that work well on all devices.

How To

  1. Start by adding a meta viewport tag to your HTML head section. This tells the browser to use the device’s width as the width of the viewport.
  2. Next, write your CSS styles as you normally would, but include media queries to target specific devices or screen sizes. For example, you might use the following code to apply a different background color on devices with a screen width of less than 600 pixels:
  3. @media only screen and (max-width: 600px) {
        body {
          background-color: lightblue;
        }
      }
  4. Test your media queries on different devices and screen sizes to make sure they’re working as intended.
  5. Refine your media queries as needed to ensure your website looks great on all devices.

Best Practices

  • Use relative units like em or rem instead of pixels for font sizes and spacing to ensure they scale properly on different devices.
  • Start with a mobile-first approach, designing for the smallest screens first and then adding styles for larger screens.
  • Use a grid system or flexbox to create layouts that can adapt to different screen sizes.
  • Test your website on real devices, not just in a browser, to ensure it looks and works well on all devices.

Examples

Let’s say you’re designing a website for a restaurant.

You want the website to display a three-column layout on desktops, a two-column layout on tablets, and a one-column layout on mobile devices.

Here’s how you might use media queries to achieve this:

  1. First, add the meta viewport tag to your HTML head section:
  2. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  3. Next, create your CSS styles for the desktop layout:
  4. .container {
        display: flex;
        justify-content: space-between;
    }
    
    .column {
        width: 30%;
    }
  5. Now, add a media query to target tablets:
  6. @media only screen and (max-width: 768px) {
        .container {
          flex-wrap: wrap;
        }
      
        .column {
          width: 45%;
          margin: 0 2.5%;
        }
    }
  7. Finally, add a media query to target mobile devices:
  8. @media only screen and (max-width: 480px) {
        .container {
          flex-direction: column;
        }
      
        .column {
          width: 100%;
          margin: 0;
        }
    }
  9. Test your website on different devices to make sure it looks and works great on all of them!
Upload file