Responsive Design

Responsive Design

In modern web development, creating responsive designs that adapt to different devices is crucial for providing an optimal user experience. Media queries are a powerful tool for implementing responsive design. They allow developers to apply CSS styles conditionally based on certain criteria, such as the viewport's dimensions, screen resolution, or device capabilities.

Before diving into media queries, it's important to understand the units em and rem, which are commonly used in responsive design, these are discussed in detail here on the web design sizes guide.

Using em can create flexible layouts because the font size is based on the parent element's size. However, it can lead to unexpected scaling if there is no consistent base font size, making it challenging to maintain the desired layout on different devices.

To ensure that your website is responsive on mobile devices, it is important to include the viewport meta tag in the <head> section of your HTML. The viewport meta tag controls the layout of the page on mobile browsers, determining how the page's dimensions are calculated and how the content is scaled.

<meta name="viewport" content="width=device-width, initial-scale=1">

  • width=device-width: This tells the browser to set the viewport width to the width of the device's screen, ensuring that the layout fits within the screen without requiring horizontal scrolling.
  • initial-scale=1: This sets the initial zoom level when the page is loaded, ensuring that it is displayed at its natural size.

By using this meta tag, you can ensure that your website is responsive and that it scales appropriately on devices with different screen sizes.

Media Queries

Media queries are the cornerstone of responsive web design. They allow you to apply different styles based on various device characteristics, such as screen width, height, resolution, and orientation. Media queries can be used to optimise the layout for different screen sizes, ensuring that users have a pleasant experience regardless of the device they're using.

A basic syntax for a media query looks like this:

@media only screen and (max-width: 480px) {
    /* Styles for screens smaller than or equal to 480px */
}

Here, @media specifies that the following styles should only apply when the specified condition is true. In this case, the condition is that the viewport width is less than or equal to 480px. The only screen part is used to prevent older browsers from applying styles.

Media queries can also target different ranges, such as minimum and maximum widths, heights, and more. This allows you to define multiple breakpoints to ensure your website adjusts to various screen sizes:

  • min-width: Applies styles for screen widths greater than or equal to the specified value.
  • max-width: Applies styles for screen widths less than or equal to the specified value.
  • min-height: Applies styles for screen heights greater than or equal to the specified value.
  • max-height: Applies styles for screen heights less than or equal to the specified value.
  • min-resolution: Triggers styles on screens with a resolution at or above a threshold. Units can be dpi, dppx or dpcm.
  • max-resolution: Triggers styles on screens with a resolution at or below a threshold.
  • resolution: Targets a specific resolution.
  • min-aspect-ratio: Triggers styles on screens at or above a specific aspect-ratio.
  • max-aspect-ratio: Triggers styles on screens at or below a specific aspect-ratio.
  • aspect-ratio: Targets a specific aspect ratio (e.g. 16/9).
/* For screens 600px or smaller */
    @media only screen and (max-width: 600px) {
        body {
            font-size: 14px;
        }
    }
    
/* For screens larger than 600px */
@media only screen and (min-width: 601px) {
    body {
        font-size: 18px;
    }
}

/* Target screen size within width limits */
@media (min-width: 600px) and (max-width: 1024px) {
    body {
        font-size: 20px;
    }
}

/* For screens with a height of at least 800px */
@media only screen and (min-height: 800px) {
    body {
        background-color: lightblue;
    }
}

/* For screens with a height of 600px or less */
@media only screen and (max-height: 600px) {
    body {
        background-color: lightgreen;
    }
}

/* Target screen size within height limits */
@media (min-height: 600px) and (max-height: 1024px) {
    body {
        background-color: yellow;
    }
}

/* Target high resolution retina displays */
@media (min-resolution: 192dpi) {
    .logo {
        background-image: url('logo@2x.png');
    }
}

You can use comma-separated lists within media queries to apply multiple conditions, similar to an "OR" condition. This allows you to apply styles for any of the listed conditions.

/* Apply styles for devices with a max-width of 480px or a max-height of 600px */
@media only screen and (max-width: 480px), (max-height: 600px) {
    body {
        font-size: 12px;
    }
}

This media query will apply styles for devices with a width of 480px or less OR a height of 600px or less. If either condition is true, the styles will be applied.