Why Do We Care?#

Underneath the “buzzwords” of responsive design (or something being fully responsive)⁠—what might this concept have to offer us, as designers?

Why should a designer care about “responsive design”?

  • It’s underlying premise of progressive enhancement (or mobile-first, below) will help us organize our design process.

  • It makes us unpack the reasoning and rationale for our design decisions⁠—in order for them to work in many different scenarios.

  • Starting with the tightest/​hardest constraints, first, makes later decisions much easier.

  • It’s a form of accessibility⁠—meeting our audience where they are, on whatever device they have.

  • There are so many different devices out there! And it’s always changing.

  • It sure beats designing (and building) separate things!

What’s all this about Responsive Design?#

Instead of designing, implementing, and serving separate sites for different devices and specific scenarios⁠—we can adapt just one to work across them all. This is responsive design.

This term was coined in 2010 or so by Ethan Marcotte⁠—wrapping a name around a progressive enhancement and mobile-first web design approach/​philosophy that had been growing in the mid-2000s (sometimes called liquid, flexible, fluid, or elastic design).

There was a confluence of events that allowed this: modern, self-updating browsers, and then the explosion of the mobile web⁠—precipitated, in no small part, by the iPhone in 2007.

The iPhone was the first mobile device that ran a desktop-class browser (in terms of functionality), which hadn’t been available in a small screen before. And with its crazy success⁠—and the subsequent proliferation of its paradigm in Android⁠—the web, and then world, scrambled to respond.

You’ve probably noticed this kind of pattern:

#

A typical/​example responsive layout, adjusting the content to reflow based on the device width.

#

Empty your mind. Be formless, shapeless, like water.

You put water into a cup, it becomes the cup. You put water into a bottle, it becomes the bottle. You put it into a teapot, it becomes the teapot.

Now water can flow or it can crash. Be water, my friend.

Bruce Lee, 1971

Content is like water.

Content’s going to take many forms, flow into many different containers, many of which we haven’t even imagined yet. Build from content out, not container in.

Josh Clark, 2012

The Viewport#

There wasn’t much of a mobile web, prior to the iPhone. Some sites had bare-bones WAP mobile versions, designed for the tiny screens and limited hardware of the era.

#

This is how the Times looked on your Razr. ↗

#

The iPhone’s introduction is worth a watch. Safari! ↗

When the iPhone came on the scene, most desktop websites still didn’t have narrow/​smaller (let alone flexible) layouts⁠—so the phone would instead scale or zoom out a desktop site design down to fit.

Websites at the time were often designed to a standard width (usually 960px ), which the phone shrank down to its 320px screen⁠—and then the user could zoom in or out, scrolling around to view the whole page. It somewhat worked⁠—and all the content was there, unlike most mobile sites⁠—but it was obviously less than ideal!

#

Their full desktop site back then, scaled down, on an iPhone. ↗

Viewport <meta> Tag#

So the web had to evolve for this new class of devices.

You’ll see this <meta> element in the <head> of pretty much every website, nowadays⁠—which tells the browser not to do this scaling down:

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

You’re saying “this page has a responsive design! Render it at its actual size. The content can reflow.”

The width=device-width tells the browser to use whatever the screen’s actual pixel dimension is, and the initial-scale=1 sets the starting zoom for the page to 100%. This is how the browser knows how to make the page respond, and how our CSS rules know what width to use.

#

The Times wasn’t fully responsive until 2018! They still maintained a separate mobile site and apps. ↗

We call the portion of the page visible at one time the viewport.

Media Queries#

Responsive design could really only flourish when CSS (and browsers) added the @media at-rule to work with this new device information.

CSS media queries – MDN
Checking for viewport values.

These are colloquially called media queries, and they allow us to check if our reader’s screen is a certain width or resolution (or other features, which we’ll get to)⁠—and then apply selective CSS only in that scenario/​situation. These let site layouts respond intentionally to different devices.

Practically, these are blocks of CSS⁠—a little bit like selectors that contain other selectors⁠—but which only apply conditionally when the test/​criteria is met.

Like any other CSS⁠—if there are multiple conditions that are met, or there is a tie between properties⁠—the rules cascade down and the lowest/​last one takes precedent.

/* Our CSS has all been out here! */

@media (feature = value) {
	/* CSS that only applies when true. */
}

In a broader code and programming context, it can be helpful to think of media queries as conditional if statements.

We’ll talk about this in detail later with JavaScript, where conditionals are ubiquitous and powerful! You may have also heard of If This Then That, which takes its name from this kind of logic.

Width-Based Breakpoints#

There are many media queries we can use, but we’ll start with width ⁠—which is by far the most commonly-adjusted and really the core of responsive design. Usually when folks are talking about a page or site being responsive, they primarily mean with regards to its width .

width media feature – MDN
The most common, by far.

Our design responds in steps, at different width , that we call breakpoints⁠—the window/​device/​viewport sizes where the content starts to break, if it is not adjusted.

There Is No Perfect Layout#

The horizontal axis tends to vary the most across devices⁠—from the ~ 375px – 428px of your phones, through to the ~ 1440px – 1680px of your laptops, and then on up to the ~ 2560px – 3440px you might see with large, desktop displays.

Since this width is usually our primary design constraint ( height being handled through scrolling), we need width-based media queries to adjust our layouts across this wide range, lest our designs fall.

You might add a breakpoint because lines of text get too short or too long, becoming hard to read. It might be to prevent a grid of images from becoming too small on a phone⁠—while you can have many columns on desktop, often you can only have one (or two) on mobile.

You can add as many breakpoints as you need to make your page/​design work across devices. Don’t think of these as written for specific devices; write for your design and for your content!

#

This is from more than a dozen years ago, now. It’s really only gotten worse! ↗

There are very, very few layouts that won’t need some amount of horizontal responsiveness/​breakpoints!

If you think responsive’s simple, I feel bad for you son. We got 99 viewports, but the iPhone’s just one.

Josh Brewer, 2012


Changing Properties#

In their simplest form, we just change whatever properties/​values when they need adjustment! In this example, we would refer to the viewport (width > 500px) as our breakpoint:

  • #
  • <>
  • ↗
  • Our examples now all include the viewport <meta> tag! Drag the divider to the left to see it respond to the @media query. You can also double-click to reset it.

    Adjusting Variables#

    Thinking more systematically, we can instead adjust the --variables from our design system⁠—making our intent more clear⁠—“this will change”:

  • #
  • <>
  • ↗
  • Again, drag the divider to see rules apply. Same result as before! Try removing the first --background declaration.

    Nesting Queries#

    We can also combine these with the pattern nesting to make our rules and their code self-contained, and less redundant:

    Nesting at-rules – MDN
    Don’t repeat yourself!

  • #
  • <>
  • ↗
  • You can nest queries for your “global” :root variables, too!

    Using Comparisons#

    Our queries can also use other simple math comparison operators: < > = <= >= . These replace earlier min-width syntax, and are much easier to understand:

    Query Range Syntax - CSS Tricks
    More modern, more intuitive.

  • #
  • <>
  • ↗
  • Exact matches (like the width = 500px here) are rarely useful, though!

    And Ranges#

    This new comparison syntax also allows simple, combined ranges to be specified⁠—to see if the viewport is between two lengths:

    New syntax for range media queries - web.dev
    Comparison of old vs. new syntax.

  • #
  • <>
  • ↗
  • Be mindful about “painting yourself into corners” with these! They make it easier to have unconsidered (or worse, overlapping) scenarios.

    Height-Based, Too#

    You can also use the viewport’s other axis height in the same way! Though again, with the usual vertical scrolling paradigm, height-based adjustments aren’t often as necessary or anywhere nearly as common as width .

    height media feature – MDN
    Less common, but still useful.

    This example has the same breakpoint of 500px as before, but now using height :

  • #
  • <>
  • ↗
  • These code examples are responsive, themselves⁠—stacking like this when they are narrow.

    Also, Orientation#

    Sometimes you can benefit from being less-specific about your width / height and instead use orientation ⁠—as when you rotate your phone. These queries help quickly handle many scenarios, using the wonderfully tenacious names/​values of portrait or landscape :

    orientation media feature – MDN
    Often more useful than width alone!

  • #
  • <>
  • ↗
  • Note the : , instead of = . Everything was a painting before it was a photograph or a web page!

    Combinations#

    And#

    Speaking of conditional statements⁠—you can also merge multiple media queries into one test, using and between them. This is how you used to make ranges⁠—but now it’s more for combining two different checks together, like width and height / orientation :

    Logic in CSS Media Queries – CSS Tricks
    This can all get very complicated!

  • #
  • <>
  • ↗
  • The demo here is taller than 400px , for the second one.

    “Or”#

    You can also use comma-separated queries (similar to selector lists) to apply or logic⁠—setting the same styles for different scenarios:

  • #
  • <>
  • ↗
  • Note that you could do this again with a range and swapping the colors. In code, there are often many ways to do achieve the same result!

    Not#

    There is also a not() logic operator⁠—which will reverse the meaning of the media query. But this syntax gets really confusing, fast⁠—especially with things like > / < rules making for double-negatives. So it is easier to avoid!

    Why confusingly say @media not (width < 500px) when you can just say @media (width > 500px) ?

    Or @media not (orientation: portrait) , when there is @media (orientation: landscape) ?

    Mobile-First Design#

    This can all get very complicated, very quickly⁠—especially with complex designs, overlapping rules, and the wide ranges of devices to consider.

    Progressive Enhancement#

    It will help to organize our thinking in a pattern of progressive enhancement⁠—always starting with basic functionality, then enriching the experience, when possible.

    In terms of making our work responsive, this manifests as mobile-first design (and development)⁠—the easiest methodology to keep things understandable (and resilient). This has become kind of buzzwordy in the past decade or so, but it is a good philosophy to adhere to, nonetheless.

    Your mobile design constraints will be tighter and more challenging, by tackling your smallest layout first⁠—but it is almost always easier to scale things up than scale them down. A mobile design can always work as a passable desktop one; the reverse is rarely true.

    Another way to think of it: if it doesn’t work on mobile, it doesn’t work!

    • In Design

      Mobile-first means considering small screens and then adding complexity, limits, or considerations for larger screens. Start from your “worst-case scenario” and build up your design.

    • In Code

      Similarly, this means writing your styles for mobile… first, then adding width > breakpoints (cascading below them) to progressively enhance your design as it scales up.

    Start Small, Start Simple#

    This also goes “with the grain” of CSS, following its general pattern/​paradigm of the cascade⁠—and likewise jives with our “general to specific” (or “always to sometimes”) approach to organizing our styles.

    It is also much, much, much easier than adjusting desktop front-end after the fact. (Trust us.) Always think mobile-first! Here’s how that can look:

  • #
  • <>
  • ↗
  • Note we added a main container. The calc() here are kind of tricky⁠—but this will be much easier with grid , we promise!

    Responsive Lengths#

    CSS also added some additional functions, beyond just calc(). As we consider resilient designs across varying/​unknown screen sizes, these allow us to set limits/​constraints beyond the basic min- / max- sizes⁠—and crucially, apply them to other properties, like margin , padding , font-size , etc. You’ll often use these in conjunction with viewport-relative units like vw / vh .

    CSS min() , max() , and clamp() – web.dev
    For more than just -size !

    We can use these to make sure our designs work responsively, even without explicit @media queries!

    min() #

    The min() function chooses the smallest value from its comma-separate list. Somewhat counterintuitively from its name, we can use this to set the maximum value for something⁠—as the function selects whatever is lowest. You can think of this as a ceiling on your flexible values:

    min() function - MDN
    Select the smallest value.

  • #
  • <>
  • ↗
  • Drag that divider! Note that you can do maths right within min() , no need to calc()! (Same for max() and clamp(), too.)

    max() #

    On the other end of things, the max() function chooses the largest value. We can use this to set the minimum value, as the function selects whatever is highest. It is the floor for our flexibility:

    max() function - MDN
    Or the largest.

  • #
  • <>
  • ↗
  • We can also use these functions in variable definitions themselves, like --spacing: max(5vw, 5rem);

    clamp() #

    You’ll often want to set both a floor and a ceiling on flexible units, and we can do these together with the clamp() function. It will constrain the preferred, middle value to within the range of the first (minimum) and last (maximum) boundaries:

    clamp() function - MDN
    Or within a minimum/​maximum.

  • #
  • <>
  • ↗
  • Best of both worlds! We can scale our designs⁠—but not too small and not too big.

    Use these functions to prevent your design from breaking⁠—without having to know or plan for every scenario!

    Other Media Features#

    By far, the most common media queries will be width/​height/​orientation⁠—for adjusting your layouts across different devices. But @media has some more tricks up its sleeve in testing for other browser features. We’ll look at some of the handy/​common ones, here.

    @media types/​features - MDN
    There are many of these! Meet your visitors where they are.

    screen vs. print #

    In all of our above examples, there is an implied media type of screen ⁠—since that is most often what we are concerned with, on the web. But there is also one for print ! You can use these to segment styles to one medium or the other:

    CSS paged media - MDN
    There are also some print-specific properties available.

    hover #

    Another common feature to check for is hover, detecting whether a browser has an input device that supports hovering⁠—which really just means a mouse, usually on laptop/​desktop computers. (But not always!)

    hover - MDN
    Mobile devices don’t have this!

    Perhaps obviously, mobile touch-based systems don’t have this behavior⁠—and often react oddly to :hover CSS, “eating taps.” So you should adjust your interfaces to work in the absence of this state⁠—never assume a mouse!

    If you view this on your phone, the aside should be visible without interaction! On your computer, you’ll have to mouse over the section :

  • #
  • <>
  • ↗
  • Note how this is written with a mobile-first style, only adding the hover state later/​lower for folks who have it!

    Hover states are a good feature for progressive-enhancement, as we did here⁠—to add them in after you have a working mobile design. Maybe a third to a half of your audience (depending on your project) won’t see them⁠—so don’t rely on them being seen!

    prefers-color-scheme #

    You see this one more and more these days⁠—prefers-color-scheme for switching up a site’s styles based on whether the user is in light or dark mode, popularized by the ol’ iPhone again:

    prefers-color-scheme - MDN
    Michael prefers dark ; Rijk prefers light . One of them is right!

    Sometimes this feels appropriate⁠—especially in products/​applications, like maybe a messaging service. But other times the color scheme of a site is its brand (like ours), and probably shouldn’t change based on this query. Continuing our ongoing discussion of who has the control⁠—it’s up to you:

  • #
  • <>
  • ↗
  • You’ll see this differently depending on whether your system is in light or dark mode!

    prefers-contrast /​ prefers-reduced-motion #

    These last two are primarily concerned with accessibility⁠—prefers-contrast for folks who run their device/​browser in a high-contrast mode to help with their vision, or prefers-reduced-motion for those who have animations turned off for vestibular reasons.

    Or these are just their preference! None of your business.

    :root {
    	/* Default to high contrast… */
    	--background: white;
    	--foreground: black;
    }
    
    p {
    	background-color: var(--background);
    	color: var(--foreground);
    }
    
    /* When “Increase Contrast” is *not* enabled. */
    @media (prefers-contrast: no-preference) {
    	:root {
    		/* …use designer/subtle/nuanced colors. */
    		--background: lightgray;
    		--foreground: slategray;
    	}
    }
    #

    The corresponding settings in i⁠OS.

    /* When “Reduce Motion” is *not* enabled. */
    @media (prefers-reduced-motion: no-preference) {
    	button {
    		animation: some-slick-animation;
    	}
    }
    #

    The power of the Web is in its universality.

    Access by everyone regardless of disability is an essential aspect.

    Tim Berners-Lee, 1997