With sites being accessed by an increasing array of devices and browsers, users deserve a quality experience no matter the size of their display. Ethan Marcotte explains how our designs can become more responsive
A professor of mine once told me that every artistic movement was a response to the one before it. Bebop threw off the melodic strictures of swing, while film noir countered the glossy sheen of big studio comedies with gritty antiheroes. George Lucas watched a few too many Kurosawa films and a bunch of old Westerns, and Star Wars was the result.
Influence and inheritance: it’s an old song. The still-young field of web design is no exception, and is very much influenced by its predecessor, print.
Unlike print, though, much is out of our control: browser incompatibilities, a reliance on locally installed fonts, an ever-changing browser window: it’s enough to drive a poor designer mad. So in the face of every inconsistency, we impose constraints on our medium to better establish a sense of control. Fixed-width layouts and minimum screen resolution are two such constraints: they better help us isolate the problems we feel we can solve from those we can’t. Divide, quarantine and conquer.
But the web is becoming more unpredictable, not less. There are more devices, more browsers than ever before. The explosive adoption of excellent small-screen browsers such as Mobile WebKit has further compounded the effect.
Unfortunately, our early attempts at designing beyond the desktop have felt all too similar to our divide-and-quarantine approach from the past. Here’s a quick example: I was working on my laptop when a friend emailed me a link to an article she’d just read on her phone:
Note the webkit subdomain: the site’s owners had quarantined the ‘mobile experience’ on a separate subdomain, fixing the width to 320px. Once that link was shared (via email, or saved to Delicious or Instapaper), readers were locked into that context, regardless of browser. As you might guess, the reading experience was less than optimal (read: “awful”) on a desktop browser.
Fragmented content
You could argue that this example simply suffers from poor execution, that some simple browser sniffing could easily redirect users to the “full” article. But I think it’s symptomatic of our general approach to designing beyond the desktop: in the face of the many uncertainties, we place constraints on our designs, such as a minimum screen resolution, to better isolate the problems we can fix from those we feel we can’t. But fragmenting our content across different “device-optimised” domains is a losing proposition. We simply can’t compete with the pace of technology, updating our sites each time a new, competent browser hits the market.
Rather than creating disconnected designs, each tailored to a particular device or browser, we need to treat them as facets of the same experience. Responsive web design means creating an adaptive design that’s aware of the context it’s viewed in and optimises its display accordingly. And we can do so simply by embedding standards-based technologies in our work. By starting with a flexible grid (with flexible media), we can incorporate media queries to create that responsive design.
To illustrate this, I’ve built a simple little (fake) site called ‘Robot or Not’ (see above). It’s the definitive answer to who is and isn’t a robot. Your cat? Not a robot. K-9? Totally a robot.

First ingredient: the fluid grid
If you’ve ever sized text with ems, you’re well prepared to answer that question. Let’s assume my body element has a font-size of 100% – which, by default, is equivalent to 16px. Further down in the document, I have a headline that’s supposed to be sized at 22px in the comp, but I want to set it in nice, relative ems. To do so, we simply take the target value for the headline’s font-size in pixels, and divide it by the font-size of its container – ie, its context. The result is our desired font-size, expressed in proportional, em-ready terms. In other words: target ÷ context = result.We can take our pixel values and plug them into this formula. To properly set our headline in relative terms, divide the target value (22px) by the font-size of its context (16px): 22 ÷ 16 = 1.375.

We've just described a font size not as pixels, but as proportions measured against an element’s container. But every element of our grid – and the elements laid upon it – can be expressed proportionally in exactly the same way. In other words, we’re looking not just at the desired size of a particular element, but the relationship of that size to the element’s container. Doing so will allow us to construct a grid whose pixel-based width might change, but whose intrinsic proportions will remain intact as it resizes. All by reusing our trusty type formula: target ÷ context = result.
Looking at the design (see below), we can see that the content area is designed to be 637px wide, with two significant elements inside it: the main content, floated to the right, is 420px wide; the metadata is floated to the left, and sized at 204px. And if we were content with pixels, we’d be done with our job by now:
#blog .main {float: right;
width: 420px;
}
#blog .aside {float: left;
width: 204px;
}
#blog .main {float: right;
width: 65.9340659%; /* 420px / 637px */
}

#blog .aside {float: left;
width: 32.025117%; /* 204px / 637px */
}

#blog blockquote {float: right;
width: 32.025117%; /* 204px / 637px */
}

Second ingredient: flexible images
With our grid in place and resizing beautifully, let’s focus on incorporating some fixed-width elements. After all, it’s painfully simple for text to reflow in a flexible container; what happens when we introduce fixed-width elements? Well, I hear images are going to be just huge on the web, so let’s start there.What happens when we drop in an oversized graphic we know to be larger than its container? Awful, unspeakable things. Our image is over a thousand pixels wide, and there aren’t any constraints on its width. It’s simply overflowing its container, and breaking our page pretty thoroughly. But what if we could say that our image should reflow and resize itself proportionally, and should never exceed the width of its container? With a quick dab of CSS, we can get exactly that:
img {max-width: 100%;
}
img, video, object {max-width: 100%;
}
img {width: 100%;
}

img.full, .entry img, .entry object {width: 100%;
}
Third ingredient: media queries
Flexible or fixed, no design scales well beyond the context for which it was originally designed. When made smaller, the background sprite used for the navigation becomes clipped at smaller resolutions, and our content becomes illegibly narrow. Conversely, when viewed on a widescreen display, the images grow freakishly large.Enter the media query. Part of the CSS3 spec, a media query enables us to inspect the physical characteristics of the device rendering our design. If the device passes the test outlined in the query, then the CSS contained within is loaded. As our flexible layout resizes, we can apply media queries at different resolution ranges to surgically correct issues as they appear – whether in a widening desktop browser window or on mobile devices.
For example, let’s tackle some of the smaller screen displays. Since issues crop up when my browser window is 660px or lower, I can create a media query that targets that range:
@media screen and (max-width: 660px) {…
}

@media screen and (max-width: 660px) {#logo a {display: block;
float: none;
margin: 0 auto;
}
#nav {clear: both;
padding-top: 0;
}
#nav li {margin-right: 1.5053763%;
width: 23.87096774%;
}
}
@media screen and (max-width: 340px) {#nav li {width: 49.247311827956989247%;
/* 458px / 930px */
}
}
Working together
It’s tempting to reduce the concept of responsive web design to one of its component parts, that it’s simply about ‘triggering alternate layouts with media queries’, or perhaps ‘designing for mobile.’ In fact, the three components are inseparable: devices or browsers without support for media queries can still avail themselves of a flexible layout that affords a measure of resolution independence.Of course, that’s not the end of the challenges before us. How does a highly art directed layout adapt to a changing browser window? How do we rethink complex, interaction-heavy interfaces on a smaller display? Responsive web design offers us a foundation, a framework for tackling these problems – both now, and in the years to come.
0 comments:
Post a Comment