Responsive Design
What’s all this about Responsive Design ?
This term was coined in 2010 or so by Ethan Marcotte—wrapping a name around a
-
Responsive Design – MDN
A pretty nice overview. -
Beginner’s Guide to Media Queries – MDN
Slightly overlapping, but also good. -
Using Media Queries – MDN
Okay, that’s probably enough MDN. -
Using CSS Custom Properties – MDN
Sorry, last one! For now.
Instead of building, designing, and serving a desktop site and a separate, minimal mobile version (if you even did at all)—you could instead adapt
There was a confluence of events that allowed this: modern,
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.
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.
The Viewport
There wasn’t much of a
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
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 less than ideal.
Viewport <meta> Tag
You’ll see this meta element in the head of most websites, now:
<meta name="viewport" content="width=device-width, initial-scale=1">
This meta element tells the browser
The width=device-width tells the browser to use whatever the screen’s 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.
We call the portion of the page visible at one time
Media Queries
Responsive design could only really flourish when CSS (and browsers) added the @media
These are colloquially called
Practically, these are blocks of CSS—a little bit like
These blocks are like any other CSS—if there are multiple conditions that are met, or there is a tie between properties—the rules
/* Our CSS has all been out here! */
@media some-criteria-or-rule {
/* CSS that only applies if a test passes. */
}
Width-Based Breakpoints
There are many media queries we can use, but we’ll start with width.
Width 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
This is done in steps, at different widths, that we call
If you think responsive’s simple, I feel bad for you son. We got 99 viewports, but the iPhone’s just one.
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
There are very,
In this example, we would refer to 35rem550px as our
This width rule/test/criteria uses math comparison operators—meaning you can use < > = <= >= :
width = 550px here) are rarely useful!
Height-Based, Too
You can also use height in the same way—though again, with the usual vertical scrolling paradigm, width.
This example is the same 550px as before, but now using height:
In a broader code and programming context, it can be helpful to think of media queries as conditional
We’ll talk about this in detail later with JavaScript, where conditionals are ubiquitous and powerful. You may have also heard of
Orientation
You can also be less specific about your width/height and instead use orientation—like when you rotate your phone. The queries use the wonderfully tenacious names/values of portrait or landscape:
And/Or Combinations
And speaking of and. This is often used for a range (to apply something width and height checks, together:
300px, for the second one.
You can also use comma-separated queries (similar to
and, as in the example above, by just swapping the colors. Code logic!
There is also a not logic operator—which will reverse the meaning of the media query. But this syntax gets confusing fast—especially with things like >/< rules making for double-negatives. So it is easier to avoid!
Why say not portrait when you can just say landscape?
Mobile-First Design
So this can all get very complicated, very quickly—especially with complex designs, overlapping rules, and the wide ranges of devices to consider.
-
Mobile First – A Book Apart
Luke Wroblewski wrote the book (and the deck). -
Progressive Enhancement – Wikipedia
The term coined by Steve Champeon and Nick Finck in 2003.
One of the easiest methodologies to keep things understandable is practicing
Your design constraints will be tighter and more challenging, by tackling your smallest layout first—but it is almost always easier to scale things
If it doesn’t work on mobile, it doesn’t work.
-
In Design:
Mobile-first means considering small screens andthen adding complexity, limits, or considerations for larger screens. Start from your “worst-case scenario.” -
In Code:
Similarly, this means writing your styles for mobile… first,
then addingwidth > #breakpoints (cascading below them) toprogressively enhance your design as it scales up.
main container. The inline-size here are kind of tricky—but this will be much easier with grid, we promise!
This goes “with the grain,” following the general CSS pattern/paradigm of the cascade—and is much, much,
Mobile can be the majority of your traffic—especially internationally! We’d like you to think of
Briefly, CSS Variables
Custom properties (folks almost always say
CSS Custom Properties Guide – CSS Tricks
Web guru Chris Coyier’s robust overview.
These bring another programming concept of
Changing the value of a
In your CSS, you -- prefix in front of a subjective name you make up. And you var().
:root {
--brand-color: #e42a1d; /* Declare it. */
}
.brand-color {
color: var(--brand-color); /* Reference it. */
}
You can use these as values for
You’ll often declare a set of variables for mobile—type sizes, spacing, and so on—and then adjust them, once, for desktop. No need to write all the properties out again, with all their own redundant media-queries! Variables are
They’ll help you avoid unwanted cascade (applying the same property), especially across breakpoints. But they also help to facilitate
Variables are how you build
Other Media Features
By far, the most common media queries will be @media has some more tricks up its sleeve in testing for other browser features. We’ll look at some of the handy/common ones.
@media types/features - MDN
There are many of these! Meet your users where they are.
screen vs. print
In all of our above examples, there is an implied screen—since that is usually what we are concerned with, on the web. But there is also one for print !
CSS paged media - MDN
There are also some print-specific properties available.
print style in action by going directly to the example, then hover
Another common feature is hover, used to detect whether a browser has an input device that supports
hover - MDN
Mobile devices don’t have this!
Mobile :hover CSS, “eating taps”), so you should adjust your interfaces to work in the absence of this state:
aside should be visible without interaction. On your computer, you’ll have to mouse over the div. Note how this is written with a Hover states are a good feature for
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
prefers-color-scheme - MDN
Michael prefers dark; Rijk prefers light.
Sometimes this feels appropriate—especially in products/applications, like maybe a messaging service. But sometimes the color scheme of a site is its
prefers-contrast / prefers-reduced-motion
These last two are primarily concerned with accessiblity—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.
prefers-contrast- MDNprefers-reduced-motion- MDN
You can think of both subtlety and motion as progressive enhancements.
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;
}
}
/* 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.