• What responsive design is — designing web layouts so that they are flexible and work well across different device screen sizes, resolutions, etc.
  • The relationship between modern layout tools such as grid and flexbox, and responsive design.
  • The concepts behind using media queries for responsive design, including mobile-first and breakpoints.
  • Why <meta viewport=""> is needed to get web documents to display appropriately on mobile devices.
  • Precursor to responsive design: mobile web design

    Before responsive web design became the standard approach for making websites work across different device types, web developers used to talk about mobile web design, mobile web development, or sometimes, mobile-friendly design. These are basically the same as responsive web design — the goals are to make sure that websites work well across devices with different physical attributes (screen size, resolution) in terms of layout, content (text and media), and performance.

    The difference is mainly to do with the devices involved, and the technologies available to create solutions:

  • We used to talk about desktop or mobile, but now there are many different types of device available such as desktop, laptop, mobile, tablets, watches, etc. Instead of catering for a few different screen sizes, we now need to design sites defensively to cater for common screen sizes and resolutions, plus unknowns.
  • Mobile devices used to be low-powered in terms of CPU/GPU and available bandwidth. Some didn't support CSS or even HTML, and as a result, it was common to perform server-side browser sniffing to determine device/browser type before then serving a site that the device would be able to cope with. Mobile devices often had really simple, basic experiences served to them because it was all they could handle. These days, mobile devices are able to handle the same technologies as desktop computers, so such techniques are less common.
  • You should still use the techniques discussed in this article to serve mobile users a suitable experience, as there are still constraints such as battery life and bandwidth to worry about.
  • User experience is still a concern. A mobile user of a travel site might just want to check flight times and delay information, for example, and not be presented with a 3D animated globe showing flight paths and your company history.
  • Modern technologies are much better for creating responsive experiences. For example, responsive images/media technologies now allow appropriate media to be served to different devices without having to rely on techniques like server-side sniffing.
  • Introducing responsive web design

    HTML is fundamentally responsive, or fluid . If you create a web page containing only HTML, with no CSS, and resize the window, the browser will automatically reflow the text to fit the viewport.

    While the default responsive behavior may sound like no solution is needed, long lines of text displayed full screen on a wide monitor can be difficult to read. This problem can be solved with CSS, for example by creating narrow columns to limit the line length. However, this can create new problems for users that narrow their browser window or view the site on a mobile device — the columns will look squashed and become harder to read.

    Creating a non-resizable web page by setting a fixed width doesn't work either; that leads to scroll bars on narrow devices and too much empty space on wide screens.

    Responsive web design, or RWD, is a design approach that addresses the full range of available devices and device sizes, enabling automatic adaption to the screen, whether the content is viewed on a tablet, phone, television, or watch.

    Responsive web design isn't a separate technology — it is an approach. It is a term used to describe a set of best practices used to create a layout that can respond to any device being used to view the content.

    The term responsive design , coined by Ethan Marcotte in 2010 , described using fluid grids, fluid images, and media queries to create responsive content.

    At the time, the recommendation was to use CSS float for layout and media queries to query the browser width, creating layouts for different breakpoints. Fluid images are set to not exceed the width of their container; they have their max-width property set to 100% . Fluid images scale down when their containing column narrows but do not grow larger than their intrinsic size when the column grows. This enables an image to scale down to fit its content, rather than overflow it, but not grow larger and become pixelated if the container becomes wider than the image.

    Modern CSS layout methods are inherently responsive, and, since the publication of Marcotte's article, we have a multitude of features built into the web platform to make designing responsive sites easier.

    The rest of this article will explain the various web platform features you might want to use when creating a responsive site.

    Media Queries

    Media queries allow us to run a series of tests (for example, whether the user's screen is greater than a certain width or resolution) and apply CSS selectively to style the page appropriately for the user's needs.

    For example, the following media query tests to see if the current web page is being displayed as screen media (therefore not a printed document) and the viewport is at least 80rem wide. The .container rule will only be applied if these two things are true.

    css
    @media screen and (width >= 80rem) {
      .container {
        margin: 1em 2em;
    

    You can add multiple media queries within a stylesheet, tweaking your whole layout or parts of it to best suit the various screen sizes. The points at which a media query is introduced, and the layout changes, are known as breakpoints.

    A common approach when using media queries is to create a simple single-column layout for narrow-screen devices (for example, mobile phones), then check for wider screens and implement a multiple-column layout when you know that you have enough screen width to handle it. Designing for mobile first is known as mobile first design.

    If using breakpoints, best practices encourage defining media query breakpoints with relative units rather than absolute sizes of an individual device.

    There are different approaches to the styles defined within a media query block; ranging from using media queries to <link> style sheets based on browser size ranges to only including custom properties variables to store values associated with each breakpoint.

    Media queries can help with RWD, but are not a requirement. Flexible grids, relative units, and minimum and maximum unit values can be used without media queries.

    Note: Scrimba has a tutorial called Aside: Media queries MDN learning partner, which provides an interactive introduction to media queries plus a challenge to test that you understand the basics.

    Responsive layout technologies

    Responsive sites are built on flexible grids, meaning you don't need to target every possible device size with pixel perfect layouts.

    By using a flexible grid, you can change a feature or add in a breakpoint and change the design at the point where the content starts to look bad. For example, to ensure line lengths don't become unreadably long as the screen size increases you can use columns; if a box becomes squashed with two words on each line as it narrows you can set a breakpoint.

    Several layout methods — including Flexbox, and CSS Grid — are responsive by default. They all assume that you are trying to create a flexible grid and give you easier ways to do so.

    Flexbox

    In flexbox, flex items shrink or grow, distributing space between the items according to the space in their container. By changing the values for flex-grow and flex-shrink you can indicate how you want the items to behave when they encounter more or less space around them.

    In the example below the flex items will each take an equal amount of space in the flex container, using the shorthand of flex: 1 as previously discussed (see Flexbox: Flexible sizing of flex items).

    css
    .container {
      display: flex;
    .item {
      flex: 1;
    

    Here's how we could use flexbox with a media query for responsive design.

    html
    <div class="wrapper">
      <div class="col1">
          This layout is responsive. See what happens if you make the browser window
          wider or narrow.
      <div class="col2">
          One November night in the year 1782, so the story runs, two brothers sat
          over their winter fire in the little French town of Annonay, watching the
          grey smoke-wreaths from the hearth curl up the wide chimney. Their names
          were Stephen and Joseph Montgolfier, they were papermakers by trade, and
          were noted as possessing thoughtful minds and a deep interest in all
          scientific knowledge and new discovery.
          Before that night—a memorable night, as it was to prove—hundreds of
          millions of people had watched the rising smoke-wreaths of their fires
          without drawing any special inspiration from the fact.