RESPONSIVE_WEB_DESIGN_Ethan_Marcotte.pdf
Document Details
Uploaded by ConsiderateBeech94
Virtual University of Pakistan
2011
Tags
Full Transcript
No. 4 Brief books for people who make websites Ethan Marcotte RESPONSIVE WEB DESIGN foreword by Jeremy Keith Ethan Marcotte RESPONSIVE WEB DESIGN Copyright © 2011 by Ethan Marcotte All rights reserved Publis...
No. 4 Brief books for people who make websites Ethan Marcotte RESPONSIVE WEB DESIGN foreword by Jeremy Keith Ethan Marcotte RESPONSIVE WEB DESIGN Copyright © 2011 by Ethan Marcotte All rights reserved Publisher: Jeffrey Zeldman Designer: Jason Santa Maria Editor: Mandy Brown Technical Editor: Dan Cederholm Copyeditor: Krista Stevens Compositor: Neil Egan ISBN 978-0-9844425-7-7 A Book Apart New York, New York http://abookapart.com 10 9 8 7 6 5 4 3 2 1 TABLE OF CONTENTS chapter 1 1 Our Responsive Web chapter 2 13 The Flexible Grid chapter 3 42 Flexible Images chapter 4 64 Media Queries chapter 5 106 Becoming Responsive 140 Acknowledgements 142 Resources 144 References 147 Index FOREWORD Language has magical properties. The word “glamour”— which was originally a synonym for magic or spell-casting— has its origins in the word “grammar.” Of all the capabilities of language, the act of naming is the most magical and powerful of all. The short history of web design has already shown us the transformative power of language. Jeffrey Zeldman gave us the term “web standards” to rally behind. Jesse James Garrett changed the nature of interaction on the web by minting the word “Ajax.” When Ethan Marcotte coined the term “responsive web design” he conjured up something special. The technologies existed already: fluid grids, flexible images, and media queries. But Ethan united these techniques under a single banner, and in so doing changed the way we think about web design. Ethan has a way with words. He is, of course, the perfect person to write a book on responsive web design. But he has done one better than that: he has written the book on respon- sive web design. If you’re hoping for a collection of tricks and tips for add- ing a little bit of superficial flair to the websites that you build, then keep looking, my friend. This little beauty operates at a deeper level. When you’ve finished reading this book (and that won’t take very long) take note of how you approach your next proj- ect. It’s possible that you won’t even notice the mind-altering powers of Ethan’s words, delivered, as they are, in his light- hearted, entertaining, sometimes downright hilarious style; but I guarantee that your work will benefit from the presti- digitation he is about to perform on your neural pathways. Ethan Marcotte is a magician. Prepare to be spellbound. —Jeremy Keith 1 “ OUR RESPONSIVE WEB Something there is that doesn’t love a wall... ” —robert frost, “Mending Wall” as i begin writing this book, I realize I can’t guarantee you’ll read these words on a printed page, holding a tiny pa- perback in your hands. Maybe you’re sitting at your desk with an electronic copy of the book up on your screen. Perhaps you’re on your morning commute, tapping through pages on your phone, or swiping along on a tablet. Or maybe you don’t even see these words as I do: maybe your computer is simply reading this book aloud. Ultimately, I know so little about you. I don’t know how you’re reading this. I can’t. Publishing has finally inherited one of the web’s central characteristics: flexibility. Book designer and publisher Craig Mod believes that his industry is quickly entering a “post- artifact” phase (http://bkaprt.com/rwd/1/), that the digital age is revising our definition of what constitutes a “book.” OUR RESPONSIVE WEB 1 fig 1.1: The canvas, even a blank one, provides a boundary for an artist’s work. (Photo by Cara StHilaire: http://bkaprt.com/rwd/2/) Of course, web designers have been grappling with this for some time. In fact, our profession has never had an “artifact” of its own. At the end of the day, there isn’t any thing produced by designing for the web, no tangible objects to hold, to cher- ish, to pass along to our children. But despite the oh-so-ethe- real nature of our work, the vocabulary we use to talk about it is anything but: “masthead,” “whitespace,” “leading,” even the much-derided “fold.” Each of those words is directly inherited from print design: just taken down from the shelf, dusted off, and re-applied to our new, digital medium. Some of that recycling is perfectly natural. We’re creatures of habit, after all: as soon as we move into a new city, or start a new job, we’re mapping previous experiences onto the new, 2 RESPONSIVE WEB DESIGN fig 1.2: The browser window, our true canvas. (For better or worse.) more foreign one, using them to gradually orient ourselves. And since the web is a young medium, it’s only natural to bor- row some terms from what we know: graphic design provides us with a rich history that spans centuries, and we’d be remiss not to use its language to help shape our industry. But our debt to print goes much deeper than language. In fact, there’s another concept we’ve borrowed, one we might not acknowledge all that often: the canvas (fig 1.1). In every other creative medium, the artist begins her work by selecting a canvas. A painter chooses a sheet of paper or fabric to work on; a sculptor might select a block of stone from a quarry. Regardless of the medium, choosing a canvas is a powerful, creative act: before the first brush stroke, before striking the chisel, the canvas gives the art a dimension and shape, a width and a height, establishing a boundary for the work yet to come. On the web, we try to mimic this process. We even call it the same thing: we create a “canvas” in our favorite image editor, a blank document with a width and height, with di- mension and shape. The problem with this approach is that we’re one step removed from our actual canvas: the browser window, and all of its inconsistencies and imperfections (fig 1.2). Because let’s face it: once they’re published online, our designs are immediately at the mercy of the people who view OUR RESPONSIVE WEB 3 fig 1.3: Deviating slightly from our “ideal” parameters can negatively impact the user… them—to their font settings, to the color of their display, to the shape and size of their browser window. So in the face of all that uncertainty, that flexibility, we begin by establishing constraints: we set our type in pixels, or create fixed-width layouts that assume a minimum screen resolution. Establishing those constraints is a bit like selecting a canvas—they give us known parameters to work from, cer- tainties that help quarantine our work from the web’s inher- ent flexibility. But the best thing—and often, the worst thing—about the web is that it defies easy definition. If I was feeling especially bitter, I’d even go so far as to say it revels in its ability to shrug off whatever constraints we place around it. And the param- eters we place on our designs are no different: they’re easily broken. If a browser drops even slightly below our expected minimum width (fig 1.3), a site’s visitor might find her reading 4 RESPONSIVE WEB DESIGN fig 1.4: …or our businesses and clients. (What’s a “reg,” you ask? That’s the “Register Now” link, hidden from view.) experience is altered by a horizontal scrollbar and clipped content. But our businesses and clients could be affected as well (fig 1.4): by relying on a minimum screen resolution, the placement of critical links or elements can be incredibly frag- ile, clipped by a viewport that obeys the user’s preferences, not ours. FASTEN THOSE SEATBELTS Over a decade ago, John Allsopp wrote “A Dao of Web Design” (http://bkaprt.com/rwd/3/), an article that, if you haven’t read it, you should absolutely check out now. (Seriously. I’ll wait.) It’s easily my favorite essay about designing for the web, and it’s just as relevant today as it was when it was first written. John argues that [t]he control which designers know in the print medium, and often desire in the web medium, is simply a function of the limitation of the printed page. We should embrace the fact that the web doesn’t have the same constraints, and design for this flexibility. But first, we must “accept the ebb and flow of things.” Now, John was writing during the web’s early years, a pe- riod of transition when designers transferred print-centered design principles onto this young, new medium. But much OUR RESPONSIVE WEB 5 of what he wrote ten years ago still rings true today. Because the web has never felt more in flux, more variable than it does right now. After all, we’ve been entering our own transition period for some time. We’re now faced with a browser landscape that’s become increasingly untethered from the desktop, with devices becoming smaller and larger simultaneously. Small- screen devices are estimated to become the dominant form of web access in a matter of years (http://bkaprt.com/rwd/4/), while modern game consoles have made a widescreen, tele- vision-centric web more readily accessible. Tablet computing has become wildly popular of late, presenting us with a mode of web access that is neither fully “mobile” nor “desktop,” but somewhere in between. The long and short of it is that we’re designing for more de- vices, more input types, more resolutions than ever before. The web has moved beyond the desktop, and it’s not turning back. Unfortunately, our early attempts at designing beyond the desktop have felt pretty similar to our old approaches, apply- ing constraints in the face of uncertainty. A few months ago, a friend emailed me a link to an article she’d just read on her phone: http://www.bbc.co.uk/news/mobile/science- environment-13095307 See the /mobile/ directory? The site’s owners had quaran- tined the “mobile experience” on a separate URL, assuming a page width of 320 pixels. But whenever that link is shared on Twitter, Facebook, or via email, visitors will be locked into that small-screen friendly view, regardless of the device they use to read it. And speaking for myself, the reading experience was, well, awful on a desktop browser. That’s not to say that mobile websites are inherently flawed, or that there aren’t valid business cases for creating them. But I do think fragmenting our content across different “device-optimized” experiences is a losing proposition, or at least an unsustainable one. As the past few years have shown 6 RESPONSIVE WEB DESIGN us, we simply can’t compete with the pace of technology. Are we really going to create a custom experience for every new browser or device that appears? And if not, what’s the alternative? RESPONSIVE ARCHITECTURE I’ve been an amateur fan of architecture for as long as I can re- member. And as a web designer, there’s something appealing about the number of constraints that architects seem to enjoy: from sketch to schematic, from foundation to façade, every step of the architectural process is more permanent than the one that preceded it. In Parentalia, the English architect Christopher Wren wrote that “architecture aims at eternity,” and there’s something to that: an architect’s creative decisions will stand for decades, perhaps centuries. After a day spent cursing at Internet Explorer, that kind of constancy sounds really, really nice. But in recent years, a relatively new design discipline called “responsive architecture” has been challenging some of the permanence at the heart of the architectural discipline. It’s a very young discipline, but this more interactive form has al- ready manifested itself in several interesting ways. Artists have experimented with surfaces that react to your voice with a music of their own (http://bkaprt.com/rwd/5/), and with living spaces that can reform themselves to better fit their occupants (http://bkaprt.com/rwd/6/). One company has produced “smart glass technology” that can become opaque once a room’s occupants reaches a certain density threshold, affording them an additional layer of privacy (fig 1.5). And by combining tensile materials and embedded robotics, a German design consultancy has created a “wall” that can bend and flex as people approach it, potentially creating more or less space as the size of the crowd requires (fig 1.6). Rather than creating spaces that influence the behavior of people that pass through them, responsive designers are in- vestigating ways for a piece of architecture and its inhabitants to mutually influence and inform each other. OUR RESPONSIVE WEB 7 fig 1.5: Now you see it, now you don’t: smart glass can be configured to become opaque automatically (http://bkaprt.com/rwd/7/). THE WAY FORWARD What’s fascinating to me is that architects are trying to over- come the constraints inherent to their medium. But web designers, facing a changing landscape of new devices and contexts, are now forced to overcome the constraints we’ve imposed on the web’s innate flexibility. We need to let go. Rather than creating disconnected designs, each tailored to a particular device or browser, we should instead treat them as facets of the same experience. In other words, we can craft sites that are not only more flexible, but that can adapt to the media that renders them. In short, we need to practice responsive web design. We can embrace the flexibility inherent to the web, without sur- rendering the control we require as designers. All by embed- ding standards-based technologies in our work, and by mak- ing a slight change in our philosophy toward online design. 8 RESPONSIVE WEB DESIGN fig 1.6: It doesn’t just make for an attractive art installation. This wall can actually detect your presence, and reshape itself to respond to your proximity (http://bkaprt.com/rwd/8/). The ingredients So what does it take to create a responsive design? Speaking purely in terms of front-end layout, it takes three core ingredients: 1. A flexible, grid-based layout, 2. Flexible images and media, and 3. Media queries, a module from the CSS3 specification. In the next three chapters, we’ll look at each in turn—the flexible grid, fluid images and media, and CSS3 media que- ries—creating a more flexible, more responsive approach to designing for the web. As we do so, we’ll have created a design that can adapt to the constraints of the browser window or device that renders it, creating a design that almost responds to the user’s needs. OUR RESPONSIVE WEB 9 But before we dive in, I should probably come clean: I’m a bit of a science fiction nut. I love me some laser pistols, androids, and flying cars, as well as movies and television shows containing copious amounts thereof. And I don’t much care about the quality of said shows and movies, honestly. Whether directed by Kubrick or sporting a budget lower than what I paid for lunch, I’ll watch it: just make sure there’s at least one rocket ship, and I’m happy. In all the sci-fi I’ve watched, good or bad, there’s a narrative device that genre writers really seem to love: the secret ro- bot. I’m sure you’ve come across yarns like this before. They always start with a plucky band of adventurers trying to over- come some faceless evil, lead by some upstanding hero type, armed with pithy one-liners and/or steely resolve. But lurking somewhere within their ranks is... a secret robot. (Cue ominous music.) This devious, devilish device is an unfeeling being, wrought from cold steel and colder calculations, but made to look like a human, and it has a decidedly nefarious purpose: to take our band of heroes down from the inside. The revelation of the secret robot is where the story gets most of its drama. You know the hero, and you know the ro- botic spy, sure. But among the rest of the characters, you’re always left asking yourself: who is, and who isn’t, a robot? Personally, I’ve never understood why this is so hard. Me, I miss the days of Johnny 5 and C-3PO, when you knew a robot by just looking at it, with none of this “skulking around in synthetic skin” nonsense. So I’ve taken matters into my own hands: to clear up some of this confusion, I’ve designed a simple little site called “Robot or Not” (fig 1.7). It’s intended to help us identify who exactly is, and is not, a robot. To help us better tell fleshy friend from ferrous foe. Okay, maybe I’m the only one who has this problem. But regardless of how useful this site will actually be, we’ll use its modest little design to demonstrate exactly how a responsive site is built. Over the next few chapters, we’ll be developing Robot or Not together, using flexible grids, flexible images, and media queries. 10 RESPONSIVE WEB DESIGN fig 1.7: The design for Robot or Not, in all its beeping, bitmappy glory. OUR RESPONSIVE WEB 11 Now, maybe you’re not one for suspense. Or, more likely, maybe you’re already tired of hearing me blather on at length, and just want to see the finished product. If that’s the case, then simply point your browser to http:// responsivewebdesign.com/robot/, and feel free to kick the tires a bit. Still here? Great. Let’s get started. 12 RESPONSIVE WEB DESIGN 2 THE FLEXIBLE GRID when i was in college, a professor once told me that every artistic movement—whether musical, literary, or from the fine arts—could be seen as a response to the one that preceded it. Filmmakers of the sixties produced Bonnie and Clyde and The Graduate to counter such old Hollywood pictures as The Sound of Music. In Paradise Lost, John Milton actually writes his liter- ary predecessors into the backdrop of hell—a not-so-subtle dig at their poetic street cred. And if it wasn’t for the tight ar- rangements of Duke Ellington and Benny Goodman, Charlie Parker might never have produced the wild-eyed experimen- tation of bebop. One artist establishes a point; another sets the coun- terpoint. And this was especially true for the artists of the Modernist period in the mid-20th century. The Modernists were looking at the creative output of their predecessors, the Romantic period of the late 19th century, with, well, a little disdain. To them, Romantic art was just laden down with all this stuff—needless, embellished ornamentation that THE FLEXIBLE GRID 13 fig 2.1: The Modernists heralded a shift away from the embellished realism of William Blake and Eugène Delacroix, to the more rational approach of Hans Hofmann and Josef Müller-Brockmann. overwhelmed the artwork, and impeded its ability to properly communicate with the viewer (fig 2.1). Now, the Modernist reaction to this took many different forms, spanning nearly every artistic medium. In painting, this meant reducing works to experiments in line, shape, and color. But graphic designers of the period, like Jan Tschichold, Emil Ruder, and Josef Müller-Brockmann, popularized this concept of a typographic grid: a rational system of columns and rows, upon which modules of content could be placed (fig 2.2). And thanks to designers like Khoi Vinh and Mark Boulton, we’ve managed to adapt this old concept to the needs of contemporary web design. In his book Grid Systems in Graphic Design, Müller- Brockmann referred to this process as “creating a typographic space on the page,” tailoring the proportions of the grid to the size of a blank piece of paper. But for a web designer, we’re missing one key component: the presence of an actual page. Our canvas, the browser window, can bend and flex to any shape or size, whether changed at the whim of the reader, or fixed by the phone or tablet they’re using to view our content. Often, the first layer of our grid-based layouts looks like this: 14 RESPONSIVE WEB DESIGN fig 2.2: When tailored to the needs of your content as well as the page’s dimensions, the typographic grid is a powerful tool, aiding designer and reader alike. #page { width: 960px; margin: 0 auto; } We create an element in our markup, give it a fixed width in our CSS, and center it in the page. But when we’re think- ing flexibly, we instead need to translate a design created in Photoshop (fig 2.3) into something more fluid, something more proportional. How do we begin? FLEXIBLE TYPESETTING To find an answer, let’s do a little role-playing. No, no—you can put away those twenty-sided dice. I had something a bit more practical (and a bit less orc-enabled) in mind. THE FLEXIBLE GRID 15 fig 2.3: Our PSD is looking pretty, but that grid’s more than slightly pixel-heavy. How can we become more flexible? 16 RESPONSIVE WEB DESIGN Pretend for a moment that you’re working as a front-end developer. (If you’re already a front-end developer, well, pretend you’re also wearing a pirate hat.) A designer on your team has asked you to convert a simple design into markup and CSS. Always game to help out, you take a quick look at the PSD she sent you (fig 2.4). fig 2.4: The mockup for our typesetting exercise. This should take, like, minutes. There’s not much content here, true. But hey—even short jobs require close attention to detail, so you begin focusing on the task at hand. And after carefully assessing the content types in the mockup, here’s the HTML you come up with: Achieve sentience with Skynet! Read More » A headline with a link embedded in it—a fine foundation of semantic markup, don’t you think? After dropping in a reset stylesheet, the content begins shaping up in your browser (fig 2.5). It’s definitely a modest start. But with our foundation in place, we can begin adding a layer of style. Let’s start by apply- ing some basic rules to the body element: body { background‐color: #DCDBD9; THE FLEXIBLE GRID 17 color: #2C2C2C; font: normal 100% Cambria, Georgia, serif; } fig 2.5: Plain, style-free markup. The stuff dreams (and websites) are made of. Nothing too fancy: We’re applying a light gray background color (#DCDBD9) to our entire document, and a fairly dark text color (#2C2C2C). And finally, we’ve dropped in the font charac- teristics: a default weight (normal) and a serif-heavy font stack (Cambria, Georgia, serif). Finally, you’ve probably noticed that the font‐size has been set to 100%. In doing so, we’ve simply set our base type size to the browser’s default, which in most cases is 16 pixels. We can then use ems to size text up or down from that rela- tive baseline. But before we do, we can see that our headline’s starting to shape up (fig 2.6). Wondering why the h1 doesn’t look, well, headline-y? We’re currently using a reset stylesheet, which overrides a browser’s default styles for HTML elements. It’s a handy way to get all browsers working from a consistent baseline. Personally, I’m a big fan of Eric Meyer’s reset (http://bkaprt. com/rwd/9/), but there are dozens of fine alternatives out there. At any rate, that’s why our h1 looks so small: it’s simply inheriting the font‐size of 100% we set on the body element, and rendering at the browser’s default type size of 16 pixels. 18 RESPONSIVE WEB DESIGN fig 2.6: With one simple CSS rule, we can set some high-level parameters for our design. Now, if we were content with pixels, we could just trans- late the values from the comp directly into our CSS: h1 { font‐size: 24px; font‐style: italic; font‐weight: normal; } h1 a { color: #747474; font: bold 11px Calibri, Optima, Arial, sans‐serif; letter‐spacing: 0.15em; text‐transform: uppercase; text‐decoration: none; } And that would be fine—there’s nothing actually wrong with setting your type in pixels. But for the purposes of our relative typesetting experiment, let’s instead start to think proportion- ally, and express those pixel-based font‐size values in rela- tive terms. So instead of pixels, we’ll use our friend the em. Contextual healing To do so, we’ll need to do a teensy bit of math: we’ll simply THE FLEXIBLE GRID 19 take the target font size from our comp, and divide it by the font‐size of its containing element—in other words, its con- text. The result is our desired font-size expressed in relative, oh-so-flexible ems. In other words, relative type sizes can be calculated with this simple formula: target ÷ context = result (Quick aside: If you’re at all like me, the word “math” causes immediate and serious panic. But speaking as someone who took a philosophy course for his college math credit, don’t run screaming into the hills quite yet. I rely on my computer’s calculator program heavily, and simply paste the result into my CSS. That keeps me from really having to, you know, do the math.) So with our formula in hand, let’s turn back to that 24px headline. Assuming that our base font‐size: 100% on the body element equates to 16px, we can plug those values directly into our formula. So if we need to express our h1’s target font size (24px) relative to its context (16px), we get: 24 ÷ 16 = 1.5 And there we are: 24px is 1.5 times greater than 16px, so our font-size is 1.5em: h1 { font‐size: 1.5em; font‐style: italic; font‐weight: normal; } And voilà! Our headline’s size perfectly matches the size speci- fied in our comp, but is expressed in relative, scaleable terms (fig 2.7). (I usually put the math behind my measurements in a com- ment to the right-hand side of the line (), 20 RESPONSIVE WEB DESIGN fig 2.7: Our headline is properly sized with flexible, scaleable ems. (But what the heck is up with that link?) which makes future adjustments much, much easier for me to make.) With that squared away, let’s turn to our lonely little “Read More” link. Actually, as you can see in figure 2.7, it’s not so little—and that’s the problem. Sized in our comp (fig 2.4) at 11 pixels in a generously kerned sans-serif, we need to scale the text down. A lot. Because at the moment, it’s simply inheriting the font‐size: 1.5em set on its containing element, the h1. And that’s important to note. Because the headline’s text is set at 1.5em, any elements inside that headline need to be expressed in relation to that value. In other words, our context has changed. So to set the font‐size of our link in ems, we’ll divide our target of 11px not by 16px, the value set on the body, but by 24px—the font size of the headline, our new context: 11 ÷ 24 = 0.458333333333333 After that little division we’re left with one of the least sexy numbers you’ve probably seen yet today. (Oh, but just you wait: the chapter’s not over yet.) Now, you might be tempted to round 0.45833333333333em to the nearest sane-looking number—say, to 0.46em. But don’t touch that delete key! It might make your eyes bleed to look THE FLEXIBLE GRID 21 at it, but 0.458333333333333 perfectly represents our desired font size in proportional terms. What’s more, browsers are perfectly adept at rounding off those excess decimal places as they internally convert the values to pixels. So giving them more information, not less, will net you a better result in the end. In the spirit of accuracy, we can just drop that homely- looking number directly into our CSS (line wraps marked »): h1 a { font: bold 0.458333333333333em Calibri, Optima, ホ Arial, sans‐serif; color: #747474; letter‐spacing: 0.15em; text‐transform: uppercase; text‐decoration: none; } fig 2.8: And with some simple math, our typesetting’s complete—without a single pixel in sight. The result? Our tiny page is finished, perfectly matching our intended design—but with text set in resizeable, scaleable ems (fig 2.8). 22 RESPONSIVE WEB DESIGN From flexible fonts to a flexible grid It’s possible you’re very, very bored right now. I mean, here you are, knee-deep in what’s supposed to be a chapter about creating flexible, grid-based layouts, and this Ethan fellow won’t stop prattling on about typesetting and math. The nerve. But the first time I had to build on a flexible grid, I had no idea where to begin. So I did what I do every time I’m faced with a problem I don’t know how to solve: I avoided it en- tirely, and started working on something else. As I started work on setting the site’s type in ems, I had a minor epiphany: namely, that we can apply the same sort of proportional thinking to layout that we apply to relative font sizes. In other words, every aspect of our grid—the rows and columns, and the modules draped over them—can be ex- pressed as proportions of their containing element, rather than in unchanging, inflexible pixels. And we can do so by recycling our trusty target ÷ context = result formula. Let’s dive in. CREATING A FLEXIBLE GRID Let’s pretend that our designer sent over another mockup, since she was so impressed with our quick turnaround on that headline we produced. We’re now tasked with coding the blog section of the Robot or Not website (fig 2.9). As it turns out, our designer likes us so darn much she’s even included a quick content inventory of the page (fig 2.10), which will save us some pre-production planning. We should really send her some cookies or something. We can handily translate her schematic into a basic markup structure, like so: Recently in The Bot Blog THE FLEXIBLE GRID 23 fig 2.9: Our new assignment: converting this blog design into a standards-based, flexible layout. fig 2.10: The content inventory for our blog module. 24 RESPONSIVE WEB DESIGN … … Our skeleton markup is lean, mean, and semantically rich, perfectly matching the high-level content inventory. We’ve created a generic container for the entire page (#page), which in turn contains our.blog module. And within.blog we’ve created two more containers: a div classed as.main for our main article content, and another div classed as.other for, um, other stuff. Poetry it ain’t, but poetry it doesn’t have to be. At this point, we’re going to skip a few steps in our exer- cise. In fact, let’s pretend that this is one of those cooking shows where the chef throws a bunch of ingredients into a pot, and then turns around to produce a fully cooked turkey. (This metaphor handily demonstrates how infrequently I watch cooking shows. Or cook turkey.) But let’s assume that we’ve already done all the CSS related to typesetting, background images, and just about every ele- ment of our design that isn’t related to layout (fig 2.11). With those other details finished, we can focus exclusively on pro- ducing our fluid grid. So how exactly do we turn those.main and.other blocks into proper columns? With our content inventory finished and some basic markup in place, let’s go back to our comp and take a closer look at the grid’s physical characteristics (fig 2.12). Reviewing the design tells us a few things: first, that the grid itself is divided into 12 columns, each measuring 69 pixels across and separated by regular 12px-wide gutters. THE FLEXIBLE GRID 25 fig 2.11: Our template is finished! Well, with the possible exception of, you know, an actual layout. fig 2.12: Grid-based layout is grid-based! Taken together, those columns and gutters give us a total width of 960 pixels. However, the blog itself is only 900 pixels wide, centered horizontally within that 960px-wide canvas. So those are the high-level details. And if we take a closer look at the two columns inside of the blog (fig 2.13), we can see that the left-hand content column (.main in our markup) is 566 pixels wide, while the right-hand, secondary column (.other) is only 331 pixels across. 26 RESPONSIVE WEB DESIGN fig 2.13: Let’s narrow our focus a bit, and measure the internal columns. Well now. Quite a few pixel values floating around so far, aren’t there? And if we were content with pixels we could simply drop them into our CSS directly. (Hello, leading statement!) #page { margin: 36px auto; width: 960px; }.blog { margin: 0 auto 53px; width: 900px; }.blog.main { float: left; width: 566px; }.blog.other { float: right; width: 331px; } THE FLEXIBLE GRID 27 fig 2.14: A few pixel-based floats later, and our design’s nearly finished. Or is it? Nice and neat: we’ve set the width of #page to 960 pixels, centered the 900px-wide.blog module within that container, set the widths of.main and.other to 566px and 331px, re- spectively, and finally floated the two columns opposite each other. And the result looks stellar (fig 2.14). But while our layout’s matched the comp perfectly, the result is downright inflexible. Fixed at a width of 960px, our page is blissfully indifferent to changes in viewport size, forc- ing a horizontal scrollbar upon the reader if the window drops even slightly below 1024 pixels (fig 2.15). In short, we can do better. From pixels to percentages Instead of pasting the pixel values from our comp directly into our CSS, we need to express those widths in relative, 28 RESPONSIVE WEB DESIGN fig 2.15: Our layout is lovely, but it’s so very inflexible. Let’s fix that. proportional terms. Once we’ve done so, we’ll have a grid that can resize itself as the viewport does, but without compromis- ing the design’s original proportions. Let’s start at the outermost #page element, which contains our design, and work our way in: #page { margin: 36px auto; width: 960px; } Nasty, evil pixels. We hates them. Okay, okay: not really. Remember, there’s absolutely noth- ing wrong with fixed-width layouts! But to move toward a more flexible grid, let’s start with a percentage value to replace that 960px: THE FLEXIBLE GRID 29 #page { margin: 36px auto; width: 90%; } I’ll confess that I arrived at 90% somewhat arbitrarily, doing a bit of trial and error in the browser window to see what looked best. By setting our #page element to a percentage of the browser window, we’ve created a container that will ex- pand and contract as the viewport does (fig 2.16). And as that container is centered horizontally within the page, we’ll be left with a comfortable five percent margin on either side. fig 2.16: Our container flexes as the browser window does. So far, so good. Moving down the markup, let’s set our sights on the.blog module itself. Previously, when we were toying with pixels, we wrote the following rule: 30 RESPONSIVE WEB DESIGN.blog { margin: 0 auto 53px; width: 900px; } Instead of a value set in pixels, we need to express.blog’s width of 900px in proportional terms: specifically, describing it as a percentage of the width of its containing element. And this is where our beloved target ÷ context = result for- mula comes back into play. We already know our target pixel width for our blog: that’s 900px, as defined in our mockup. What we want is to describe that width in relative terms, as a percentage of.blog’s con- taining element. Since.blog is nested within the #page ele- ment, we’ve got our context—namely, 960 pixels, the width of #page as it was designed in the mockup. So let’s divide our target width for.blog (900) by its context (960): 900 ÷ 960 = 0.9375 We’re left with a result of 0.9375. Doesn’t look like much, I’ll admit. But by moving the decimal over two places we’re left with 93.75%, a percentage we can drop directly into our CSS:.blog { margin: 0 auto 53px; width: 93.75%; } (Just as I did with our relative typesetting exercise, I’ve left the formula in a comment off to the right of the width prop- erty. This is a personal preference, of course, but I’ve found it to be incredibly helpful.) So that takes care of our two containing elements. But what about our content columns? THE FLEXIBLE GRID 31.blog.main { float: left; width: 566px; }.blog.other { float: right; width: 331px; } Our left-hand content column is floated to the left, and set at 566px; the additional content is floated opposite, sized at a width of 331px. Once again, let’s replace those pixel-based tar- get widths with percentages. But before we drop those values into our target ÷ context = result formula, it’s important to note that our con- text has changed. Last time, we divided the width of our blog module by 960px, the width of its container (#page). But since they’re nested inside.blog, we need to express our columns’ widths in relation to 900px—the width of the blog. So we’ll divide our two target values (566px and 331px) by 900px, our new context: 566 ÷ 900 =.628888889 331 ÷ 900 =.367777778 Once we move our decimal points we’re left with 62.8888889% and 36.7777778%, the proportional widths of.main and.other:.blog.main { float: left; width: 62.8888889%; }.blog.other { float: right; width: 36.7777778%; } 32 RESPONSIVE WEB DESIGN fig 2.17: Our flexible grid is complete. Just like that, we’re left with a flexible, grid-based layout (fig 2.17). With some simple math we’ve created a percentage-based container and two flexible columns, creating a layout that re- sizes in concert with the browser window. And as it does, the pixel widths of those columns might change—but the propor- tions of our design remain intact. FLEXIBLE MARGINS AND PADDING Now that those two columns are in place, we’re done with the top-level components of our flexible grid. Marvelous. Wonderful. Stupendous, even. But before we haul out any more adjectives, there’s quite a bit of detail work to be done. Can’t get no ventilation First and foremost, our design may be flexible, but it is in seri- ous need of some detail work. The two most grievous offend- ers? The title of our blog is flush left within its container (fig 2.18), and our two columns currently abut each other, with no margins or gutters in sight (fig 2.19). We definitely have some cleanup to do. So let’s begin with that headline. In our comp, there’s 48 pixels of space between our headline and the left edge of its container (fig 2.20). Now, we could use pixels to set a fixed padding‐left on our headline in either pixels or ems, like so: THE FLEXIBLE GRID 33 fig 2.18: Our headline is in dire need of padding. fig 2.19: Margins? We don’t need no stinking margins. (Actually, we do. We really do.).lede { padding: 0.8em 48px; } This is a decent solution. But a fixed value for that padding‐left would create a gutter that doesn’t line up with the rest of our fluid grid. As our flexible columns expand or contract, that gutter would simply ignore the rest of our de- sign’s proportions, sitting stubbornly at 48px no matter how small or wide the design became. So instead, let’s create a flexible gutter. So far, we’ve been describing various elements’ widths in propor- tional terms. But we can also create percentage-based 34 RESPONSIVE WEB DESIGN fig 2.20: According to the design, we need 48px of horizontal padding on the left edge of our headline. margins and padding to preserve the integrity of our flexible grid. And we can reuse the target ÷ context = result for- mula to do so. Before we start in with the math, it’s important to note that the context is slightly different for each, and depends on whether you’re setting flexible margins or flexible padding: 1. When setting flexible margins on an element, your context is the width of the element’s container. 2. When setting flexible padding on an element, your context is the width of the element itself. Which makes sense, if you think about the box model: we’re describing the padding in relation to the width of the box itself. Since we want to set some padding on our headline, our context is the width of our.lede title. Now since the head- line doesn’t have a declared width, its width (and the context we need for our formula) is the width of the blog module, or 900px. So out comes the calculator, and we’re left with: 48 ÷ 900 = 0.0533333333 which translates to: THE FLEXIBLE GRID 35.lede { padding: 0.8em 5.33333333%; } And there we have it: our 48px padding has been expressed in relative terms, as a proportion of our headline’s width. With that issue resolved, let’s introduce a bit of whitespace to our compacted content. To do so, it’s worth remembering that each column actually has a smaller module contained within it: the left-hand.blog column contains an.article, while the.other column contains our.recent‐entries list- ing (fig 2.21). We start with the recent entries module. Fortunately for us, our work’s over pretty quickly. Since we know the width of the element (231px) and the width of its containing column (331px), we can simply center our module horizontally:.recent‐entries { margin: 0 auto; width: 69.7885196%; } fig 2.21: Taking a look at the comp, we can quickly size up their respective widths. Pun unfortunate, but intended. 36 RESPONSIVE WEB DESIGN Now, we could take the same approach with our article. But instead, let’s make it a bit more interesting. Remember the 48px padding we set on our headline? Well, our article falls along the same column (fig 2.22). So rather than just centering our article within its container, let’s create another propor- tional gutter. Our target value is 48px. And since we’re working with relative padding, our context should be the width of the article itself. But once again, since there’s no explicit width set on.article, we can simply use 566px, the width of its parent (.blog), for our context:.article { padding: 40px 8.48056537%; } fig 2.22: Our headline and article share a common padding. Voilà! Our flexible grid’s all but finished (fig 2.23). Getting negative Let’s turn to our blog entry’s beleaguered date header. THE FLEXIBLE GRID 37 fig 2.23: Flexible padding and margins! Hooray! Currently, it’s spanning the full width of the blog entry, and that won’t do. Given what we’ve learned so far, it’s fairly straightforward to fix its width: the comp tells us our date should be floated to the left, and that it occupies one 69px column (refer back to fig 2.12). Since the date sits within the 474px-wide article, we have our context. Armed with that information, let’s write some quick CSS:.date { float: left; width: 14.556962%; } So far, so flexible, so good. But there’s one key component missing: our date is currently floating neatly against the left edge of the article, with the title and copy floating around it (fig 2.24). What we need to do is to pull that quote out of its 38 RESPONSIVE WEB DESIGN fig 2.24: Something’s rotten in Denmark. (By “Denmark,” I mean “our entry date.” And by “rotten,” I mean “entirely too close to the adjoining text.”) container, and move it across the left-hand edge of the entire module. And with negative margins, we can do exactly that. And we don’t have to change our approach because the margin is negative: just as before, we simply need to express that margin in relation to the width of the element’s container. If we look at the mockup, we can see that there are 81 pix- els from the left edge of the date over to the left edge of the article (fig 2.25). And if this was a fixed-width design, that would be our negative margin:.date { float: left; margin‐left: ‐81px; width: 69px; } But hey: we haven’t used a single pixel yet, and we’re not about to start now. Instead, we want to express that margin in relative terms, just as we did with our pull quote. It’s go- ing to be a negative margin, but that doesn’t change the math. We still want to express our target value—that 81px-wide THE FLEXIBLE GRID 39 fig 2.25: We need to draw that date out to the left by 81px. Or, you know, the relative equivalent thereof. margin—as a percentage of 474px, the width of the date’s con- taining element: 81 ÷ 474 =.170886076 Do the decimal shift and slap a minus sign on there, and we’ve got our proportional, negative margin:.date { float: left; margin‐left: ‐17.0886076%; width: 14.556962%; } Now sit back, relax, and take comfort in the fact that you’ve built your first fully flexible grid (fig 2.26). I feel a high five coming on. 40 RESPONSIVE WEB DESIGN fig 2.26: Our flexible grid is finally finished. Not a pixel value in sight, and we didn’t have to skimp on the aesthetics. Moving forward, flexibly I realize I just subjected you to a truckload of division signs. And as someone who gets headaches from balancing his checkbook, believe me: I sympathize. But building a flexible grid isn’t entirely about the math. The target ÷ context = result formula makes it easy to articulate those proportions into stylesheet-ready percentages, sure—but ultimately, we need to break our habit of translating pixels from Photoshop directly into our CSS, and focus our attention on the proportions behind our designs. It’s about becoming context-aware: better understanding the ratio-based relationships between element and container. But a fluid grid is just our foundation, the first layer of a responsive design. Let’s move onto the next step. THE FLEXIBLE GRID 41 3 FLEXIBLE IMAGES things are looking good so far: we’ve got a grid-based layout, one that doesn’t sacrifice complexity for flexibility. I have to admit that the first time I figured out how to build a fluid grid, I was feeling pretty proud of myself. But then, as often happens with web design, despair set in. Currently, our page is awash in words, and little else. Actually, nothing else: our page is nothing but text. Why is this a prob- lem? Well, text reflows effortlessly within a flexible con- tainer—and I don’t know if you’ve noticed, but the Internet seems to have one or two of those “image” things lying around. None of which we’ve incorporated into our fluid grid. So what happens when we introduce fixed-width images into our flexible design? GOING BACK, BACK TO MARKUP, MARKUP To find an answer, let’s do another quick experiment: let’s drop an image directly into our blog module, and see how our 42 RESPONSIVE WEB DESIGN layout responds. The first thing we’ll need to do is to clear some space for it in our markup. Remember our little blockquote, comfortably tucked into our blog article? Well, we’ve got way too much text on this darned page, so let’s replace it with an inset image: Lo, the robot walks Nothing fancy: an img element, followed by a brief but de- scriptive caption wrapped in a b element. I’m actually appro- priating the HTML5 figure/figcaption tags as class names in this snippet, which makes for a solidly semantic foundation. (Sharp-eyed readers will note that I’m using a b element for a non-semantic hook. Now, some designers might use a span element instead. Me, I like the terseness of shorter tags like b or i for non-semantic markup.) With that HTML finished, let’s drop in some basic CSS:.figure { float: right; margin‐bottom: 0.5em; margin‐left: 2.53164557%; width: 48.7341772%; } We’re creating a nice inset effect for our figure. It’ll be floated to the right, and will span roughly half the width of our arti- cle, or four columns of our flexible grid. Markup: check; style: check. Of course, all this HTML and CSS is for naught if there isn’t an actual image available. Now, because I love you (and robots) dearly, not just any image will do. And after scouring the web for whole minutes, I found a fantastically imposing robo-portrait (fig 3.1). The FLEXIBLE IMAGES 43 fig 3.1: An appropriately botty robot pic, courtesy of Jeremy Noble (http:// bkaprt.com/rwd/10/). beautiful thing about this image (aside from the robot, of course) is that it’s huge. I’ve cropped it slightly, but I haven’t scaled it down at all, leaving it at its native resolution of 655×655. This image is much larger than we know its flexible container will be, making it a perfect case to test how robust our flexible layout is. So let’s drop our oversized image onto the server, reload the page, and—oh. Well. That’s pretty much the worst thing on the internet (fig 3.2). Actually, the result isn’t that surprising. Our layout isn’t broken per se—our flexible container is working just fine, and the proportions of our grid’s columns remain intact. But because our image is much wider than its containing.figure, the excess content simply overflows its container, and is visible to the user. There simply aren’t any constraints applied to our image that could make it aware of its flexible environment. FLUID IMAGES But what if we could introduce such a constraint? What if we could write a rule that prevents images from exceeding the width of their container? Well, here’s the good news: that’s very easy to do: 44 RESPONSIVE WEB DESIGN fig 3.2: Our huge image is huge. Our broken layout is broken. img { max‐width: 100%; } First discovered by designer Richard Rutter (http://bkaprt. com/rwd/11/), this one rule immediately provides an incred- ibly handy constraint for every image in our document. Now, our img element will render at whatever size it wants, as long as it’s narrower than its containing element. But if it happens to be wider than its container, then the max‐width: 100% di- rective forces the image’s width to match the width of its con- tainer. And as you can see, our image has snapped into place (fig 3.3). What’s more, modern browsers have evolved to the point where they resize the images proportionally: as our flexible container resizes itself, shrinking or enlarging our image, the image’s aspect ratio remains intact (fig 3.4). I hope you’re not tired of all this good news because as it happens, the max‐width: 100% rule can also apply to most fixed-width elements, like video and other rich media. In fact, FLEXIBLE IMAGES 45 fig 3.3: Just by including max‐width: 100%, we’ve prevented our image from escaping its flexible container. On a related note, I love max‐width: 100%. we can beef up our selector to cover other media-ready ele- ments, like so: img, embed, object, video { max‐width: 100%; } Whether it’s a cute little Flash video (fig 3.5), some other embedded media, or a humble img, browsers do a fair job of 46 RESPONSIVE WEB DESIGN fig 3.4: Regardless of how wide or small its flexible container becomes, the image resizes proportionally. Magic? Who can say. resizing the content proportionally in a flexible layout. All thanks to our lightweight max‐width constraint. So we’ve cracked the problem of flexible images and me- dia—right? One CSS rule and we’re done? BECAUSE THIS JOB IS NEVER EASY Time to let the healing begin: we need to work through the pain, the tears, the rending of garments, and talk about a few browser-specific issues around flexible images. max-width in Internet Explorer The cold, hard truth is that Internet Explorer 6 and below don’t support the max‐width property. IE7 version and above? Oh, it is positively brimming with support for max‐width. But if you’re stuck supporting the (cough) venerable IE6 or lower, our approach needs refinement. Now, there are several documented ways to get max‐width support working in IE6. Most are JavaScript-driven, usually relying on Microsoft’s proprietary expression filter to dy- namically evaluate the width of an element, and to manually FLEXIBLE IMAGES 47 fig 3.5: Other media play nicely with max‐width: 100%, becoming flexible themselves. Did I mention I love max‐width: 100%? resize it if it exceeds a certain threshold. For an example of these decidedly non-standard workarounds, I’d recommend Svend Tofte’s classic blog entry on the subject (http://bkaprt. com/rwd/12/). Me? I tend to favor a more lo-fi, CSS-driven approach. Namely, all modern browsers get our max-width constraint: img, embed, object, video { max‐width: 100%; } 48 RESPONSIVE WEB DESIGN But in a separate IE6-specific stylesheet, I’ll include the following: img, embed, object, video { width: 100%; } See the difference? IE6 and lower get width: 100%, rather than the max‐width: 100% rule. A word of warning: tread carefully here, for these are dras- tically different rules. Whereas max‐width: 100% instructs our images to never exceed the width of their containers, width: 100% forces our images to always match the width of their con- taining elements. Most of the time, this approach will work just fine. For example, it’s safe to assume that our oversized robot.jpg im- age will always be larger than its containing element, so the width: 100% rule works beautifully. But for smaller images like thumbnails, or most embedded movies, it might not be appropriate to blindly up-scale them with CSS. If that’s the case, then a bit more specificity might be warranted for IE: img.full, object.full,.main img,.main object { width: 100%; } If you don’t want the width: 100% rule to apply to every piece of fixed-width media in your page, we can simply write a list of selectors that target certain kinds of images or video (img. full), or certain areas of your document where you know FLEXIBLE IMAGES 49 you’ll be dealing with oversized media (.main img,.main object). Think of this like a whitelist: if images or other media appear on this list, then they’ll be flexible; otherwise, they’ll be fixed in their stodgy old pixel-y ways. So if you’re still supporting legacy versions of Internet Explorer, a carefully applied width: 100% rule can get those flexible images working beautifully. But with that bug sorted, we’ve still got one to go. And boy, it’s a doozy. In which it becomes clear that Windows hates us If you look at our blog module with certain Windows-based browsers, our robot.jpg has gone from looking imposing to looking, well, broken (fig. 3.6). But this isn’t a browser- specific issue as much as a platform-specific one: Windows doesn’t scale images that well. In fact, when they’re resized via CSS, images quickly develop artifacts on Windows, dramatically impacting their quality. And not in a good way. For a quick test case, I’ve tossed a text-heavy graphic into a flexible container, and then resized our image with the max‐ width: 100% fix, while IE6 and below receive the width: 100% workaround. Now, you’d never actually put this amount of text in an image. But it perfectly illustrates just how badly things can get in IE7 or lower. As you can see, the image looks—if you’ll pardon the technical term—downright nasty (fig 3.7). But before you give up on the promise of scaleable, flex- ible images, it’s worth noting that this bug doesn’t affect every Windows-based browser. In fact, only Internet Explorer 7 and lower are affected, as is Firefox 2 and lower on Windows. More modern browsers like Safari, Firefox 3+, and IE8+ don’t exhibit a single problem with flexible images. What’s more, the bug seems to have been fixed in Windows 7, so that’s more good news. So with the scope of the problem defined, surely there’s a patch we can apply? Thankfully, there is—with the exception of Firefox 2. 50 fig 3.6: Seen here in IE6, our robot image has developed some unsightly artifacts. Guess Windows doesn’t much care for our flexible images. fig 3.7: In certain Windows-based browsers, the image quickly develops too many artifacts to be readable. Now, this grizzled old browser was released in 2006, so I think it’s safe to assume it isn’t exactly clogging up your site’s traffic logs. At any rate, a patch for Firefox 2 would require some fairly involved browser-sniffing to target specific ver- sions on a specific platform—and browser-sniffing is unreliable at best. But even if we did want to perform that kind of detec- tion, these older versions of Firefox don’t have a switch that could fix our busted-looking images. Internet Explorer, however, does have such a toggle. (Pardon me whilst I swallow my pride for this next section title.) FLEXIBLE IMAGES 51 Hail AlphaImageLoader, the conquering hero Ever tried to get transparent PNGs working in IE6 and below? Chances are good you’ve encountered AlphaImageLoader, one of Microsoft’s proprietary CSS filters (http://bkaprt.com/ rwd/13/). There have since been more robust patches created for IE’s lack of support for the PNG alpha channel (Drew Diller’s DD_belatedPNG library is a current favorite of mine: http://bkaprt.com/rwd/14/), but historically, if you had a PNG attached to an element’s background, you could drop the fol- lowing rule into an IE-specific stylesheet:.logo { background: none; filter: progid:DXImageTransform.Microsoft. » AlphaImageLoader(src="/path/to/logo.png", » sizingMethod="scale"); } This AlphaImageLoader patch does a few things. First, it removes the background image from the element, then inserts it into an AlphaImageLoader object that sits “between” the proper background layer and the element’s content. But the sizingMethod property (http://bkaprt.com/rwd/15/) is the clev- er bit, dictating whether the AlphaImageLoader object should crop any parts of the image that overflow its container, treat it like a regular image, or scale it to fit it within its containing element. I can hear you stifling your yawns by now: after all, what does an IE-specific PNG fix have to do with our broken image rendering? Quite a bit, as it turns out. At one point I discovered that applying AlphaImageLoader to an image dramatically im- proves its rendering quality in IE, bringing it up to par with, well, every other browser on the planet. Furthermore, by setting the sizingMethod property to scale, we can use our AlphaImageLoader object to create the illusion of a flexible image. 52 RESPONSIVE WEB DESIGN So I whipped up some JavaScript to automate that process. Simply download the script (available at http://bkaprt.com/ rwd/16/) and include it on any page with flexible images; it will scour your document to create a series of flexible, high-quality AlphaImageLoader objects. And with that fix applied, the difference in our rendered images is noticeable (fig 3.8): in our example we’ve gone from an impossibly distorted image to an immaculately rendered one. And it works wonderfully in a flexible context. fig 3.8: Our image is now perfectly legible, and resizing wonderfully. A dab of AlphaImageLoader’ll do ya. (It’s worth mentioning that many of Microsoft’s proprietary filters, and AlphaImageLoader in particular, have some perfor- mance overhead associated with them—Stoyan Stefanov cov- ers the pitfalls in more detail on the YUI blog: http://bkaprt. com/rwd/17/. What does this mean for you? Just be sure to test the fix thoroughly on your site, gauge its effect on your users, and evaluate whether or not the improved rendering is worth the performance tradeoff.) With the max‐width: 100% fix in place (and aided by our width: 100% and AlphaImageLoader patches), our inset image FLEXIBLE IMAGES 53 is resizing beautifully across our target browsers. No matter the size of the browser window, our image scales harmoni- ously along with the proportions of our flexible grid. But what about images that aren’t actually in our markup? FLEXIBLY TILED BACKGROUND IMAGES Let’s say our dearly esteemed designer sends over a revised mockup of our blog module. Notice anything different about it? (fig 3.9) fig 3.9: Our blog’s sidebar is now sporting a background graphic. Hot. Up until now, our blog’s content has been sitting on a rath- er unassuming near-white background. But now the design has been modified slightly, adding a two-toned background to the blog entry to provide more contrast between the left- and right-hand columns. What’s more, there’s actually a subtle level of noise added to the background, adding an extra level of texture to our design (fig 3.10). So: how do we actually add this new background image to our template? Back in 2004, Dan Cederholm wrote a brilliant article showing how a vertically repeating background graphic could be used to create a “faux column” effect (http://bkaprt.com/ 54 RESPONSIVE WEB DESIGN fig 3.10: A detailed look at our new background treatment. rwd/18/). The technique’s genius is in its simplicity: by tiling a colored background graphic vertically behind our content, we can create the illusion of equal height columns. In Dan’s original technique, the background graphic was simply centered at the top of the content area and then tiled vertically, like so:.blog { background: #F8F5F2 url("blog‐bg.png") repeat‐y 50% 0; } And that technique works beautifully. But Dan’s technique as- sumes that your design is a fixed width, creating a graphic that matches the width of your design. Then how, pray, are we supposed to work in a background image that tiles over two flexible columns? Thanks to some early research by designer Doug Bowman (http://bkaprt.com/rwd/19/), we can still apply the faux col- umn technique. It just requires a little bit of extra planning, as well as a dash of your favorite formula, target ÷ context = result. First, we’ll begin by taking a look at our mockup, to find the transition point in our background graphic, the exact pixel at which our white column transitions into the gray. And from the look of things, that switch happens at the 568 pixel mark (fig 3.11). Armed with that information, we can now adapt the “faux columns” approach to our fluid grid. First, we’ll convert that FLEXIBLE IMAGES 55 fig 3.11: Our white column switches over to gray at the 568px mark. That’s our transition point. transition point into a percentage-based value relative to our blog module’s width. And to do so, our target ÷ context = result formula comes into play yet again. We have our target value of 568px, and the width of the design—our context—is 900px. And if we plug those two values into our stalwart formula: 568 ÷ 900 = 0.631111111111111 That’s right: another impossibly long number, which converts to a percentage of 63.1111111111111%. Keep that percentage in the back of your mind for a mo- ment. Now, let’s open up your favorite image editor, and create a foolishly wide document—say, one that’s 3000 pixels across (fig 3.12). And since we’re going to tile this image verti- cally, its height is only 160px tall. In a moment, we’re going to turn this blank document into our background graphic. But why is it so large? Well, this image needs to be larger than we can reasonably assume the browser window will ever be. And unless you’re reading this from the 25th century on your wall-sized display made of, I don’t know, holograms or whatever, I’m assuming your moni- tor’s not quite that wide. To create the columns themselves, we’ll need to apply the transition point percentage (63.1111111111111%) to our new, wider canvas. So if we’re working with a graphic that’s 3000px across, we simply need to multiply that width by the percent- age, like so: 56 RESPONSIVE WEB DESIGN fig 3.12: A monstrously large canvas that we’ll (shortly) turn into our background graphic. 3000 x 0.631111111111111 = 1893.333333333333 We’re left with 1893.333333333333 as our result. And since Photoshop doesn’t deal in anything less than whole pixels, let’s round that down to 1893 pixels. Armed with that number, we’ll recreate our textures in our blank image, switching from white to gray at the 1893rd pixel (fig 3.13). fig 3.13: We’ve applied that percentage to our oh-so-wide background graphic, creating our tile-ready columns. How does that help us? Well, what we’ve just done is to proportionally scale our transition point up to this new, wider canvas. So we can take that new pixel value, and use it to cre- ate our columns: the white column will be 1893px wide, with the gray column filling up the remainder. So now there’s only one thing left to do: drop our newly minted graphic into our stylesheet. FLEXIBLE IMAGES 57.blog { background: #F8F5F2 url("blog‐bg.png") repeat‐y » 63.1111111111111% 0; } As in Dan’s original technique, we’re still positioning the graphic at the very top of our blog, and then repeating it vertically down the width of the module (repeat‐y). But the background‐position value reuses our transition point per- centage (63.1111111111111% 0), keeping the columns firmly in place as our design resizes itself. And with that, we’ve got faux columns working beautifully in a fluid layout (fig 3.14). All thanks to Dan Cederholm’s orig- inal approach, augmented with a little proportional thinking. Fully flexible background images? Of course, our flexible faux column isn’t really flexible: we’re simply using percentages to position a background image in such a way that the columns appear to resize with their con- tainer. The image’s dimensions haven’t changed at all. But what about a background image that actually does need to resize with the layout? Perhaps you’ve placed a logo on an h1 element’s background, or used sprites to create rollovers for your site’s navigation. Can we resize images that need to live in the background? Well, sort of. There is a CSS3 property called background‐ size (http://bkaprt.com/rwd/20/), which would allow us to create truly flexible background images, but—you guessed it— browser support is still pretty immature. In the interim, there are some rather ingenious JavaScript- based solutions out there: for example, Scott Robbin’s jQuery Backstretch plugin (http://bkaprt.com/rwd/21/) simulates resiz- able background images on the body element. And as you’ll see in the next chapter, CSS3 media queries could also be used to apply different background images tailored to different resolution ranges. So while background‐size might not be available yet, the sky is, as the kids say, the limit. 58 RESPONSIVE WEB DESIGN fig 3.14: Our flexibly faux columns. LEARNING TO LOVE OVERFLOW There are a few other options for working fixed-width im- ages into a fluid context. In fact, you might consider brows- ing through Richard Rutter’s experiments with wide images placed in flexible layouts (http://bkaprt.com/rwd/11/). There are a number of promising experiments listed there, some of which might prove useful to you as you start tinkering with flexible layouts. One method I’ve used on a few occasions is the overflow property. As we saw earlier in the chapter, wide images will, by default, simply bleed out of their containing elements. And in most cases, the max‐width: 100% rule is the best way to constrain them, snapping them back down to a manage- able size. But alternately, you could simply clip off that excess FLEXIBLE IMAGES 59 image data by applying overflow: hidden. So rather than set- ting our inset image to resize itself automatically:.feature img { max‐width: 100%; } We could instead simply clip off all that excess, overflowing data like so:.feature { overflow: hidden; }.feature img { display: block; max‐width: auto; } And there you have it: one image, cropped to fit inside its con- tainer (fig 3.15). The image is all still there, but the excess bits have just been hidden from view. Now, as you can see, this isn’t really a workable solution. In fact, I’ve found that in the overwhelming majority of cases, overflow is generally less useful than scaling the image via max‐width. But still, it’s an option to be considered, and one you might find some use for. fig 3.15: And with a dash of æ©̋øÙæ¬オ"̶Ƹ¸̋º applied to our image’s container, we’re left with an image that’s... well, cropped. Yay, I guess? 60 RESPONSIVE WEB DESIGN NEGOTIATE THAT CONTENT It’s worth noting that both the overflow and max‐width: 100% approaches to flexible images are actually pretty robust, and work remarkably well for most kinds of media. In fact, I’ve used them successfully on a number of complex fluid grids. However, both approaches are ultimately “content-blind.” Each establishes some basic rules for the way an image in- teracts with its container: max‐width: 100% scales oversized images down to match the width of their containers, while controlling overflow allows the designer to conceal any image data that might bleed out of its containing element. But what about especially complex graphics? If your im- age is especially information-rich (fig 3.16), simply scaling or fig 3.16: This rich infographic from the BBC News site (http://bkaprt.com/rwd/22/) contains information critical to the page’s content. Simply scaling it down could prove counterproductive. FLEXIBLE IMAGES 61 cropping it might be less than desirable—in fact, those ap- proaches might actually impede your readers’ ability to under- stand the content contained in that image. If that’s the case, it might be worth investigating ways of delivering different versions of the same image to different resolution ranges. In other words, you could create multiple versions of your infographic—say, one ideal for desktop browsers, as well as another, more linearized version for small-screen devices. With those options established, a server- side solution could intelligently serve the most appropriate image for that resolution range. Creating such a solution is beyond the scope of this book (and beyond the skill of your humble author), but designer/ developer Bryan Rieger has outlined one possible approach on his blog (http://bkaprt.com/rwd/23/), and made his solution available for download. If you decide to implement a back-end solution, it could be augmented by the various client-side techniques we’ve discussed so far. For example, you could serve images to a lim- ited number of resolutions, and then use max‐width: 100% to smooth the transition to other devices, browsers, and resolu- tion ranges on an as-needed basis. fig 3.17: Two chapters later, and we’ve finally got a completed grid-based layout that can expand and contract with a changing viewport. 62 RESPONSIVE WEB DESIGN FLEXIBLE GRIDS AND IMAGES, UP IN THE PROVERBIAL TREE At this point, we’ve explored everything you need to build complex but flexible grid-based layouts: the simple math be- hind flexible grids and some strategies for working images and other media into that framework. While we’ve been focusing on building a fairly simple blog module, we can actually use this to build the rest of the Robot or Not site, creating a design that’s founded on a system of proportions and percentages, with nary a pixel in sight (fig 3.17). With this flexible foundation in place, we’re ready to add the final ingredient to our responsive design. (And no, it’s not mixed metaphors.) FLEXIBLE IMAGES 63 4 MEDIA QUERIES for most of my career, I’ve been a staunch proponent of non-fixed layouts. Flexible or completely fluid, it didn’t matter: I felt that building some measure of fluidity into our designs better prepared them for the changes inherent to the web: changes in the user’s browser window size, in display or device resolution. What’s more, I’d often use words like “fu- ture-proof ” and “device-agnostic” when describing the need for this flexibility. Often while standing alone at parties. But at some point, everything breaks. As flexible as the Robot site is right now, it’s not completely bulletproof. Sure, its fluid grid makes it pretty resilient to changes in window size and screen resolution—much more so than a fixed layout would. But even slight changes to the size and shape of the browser window will cause our layout to warp, bend, and possibly break outright. Here’s the thing, though: that’s okay. 64 RESPONSIVE WEB DESIGN LET THE HEALING BEGIN As painful as it might be, let’s look at some of the areas where our design breaks as it reshapes itself. By identifying the prob- lems we’re facing, we’ll be in a better position to apply the needed fixes. Even if we shed a tear or three in the process. Since we’re working with a flexible layout, we can simply resize the browser window to a few different widths. Now, this is no substitute for actually testing our work on separate devices. But it allows us to quickly assess how our design handles several different resolution ranges, and simulate how users on capable phones, tablets, or other devices might expe- rience our design. A question of emphasis Let’s begin by bringing the browser window in a bit, from around 1024 pixels wide to roughly 760 pixels or so (fig 4.1). Pretty quickly, a number of problems appear. fig 4.1: By adjusting the size of our browser window, we can get a quick sense of how our design performs at different resolutions. MEDIA QUERIES 65 Our initial design was all about emphasis: large, imposing headlines, a prominent lead image, and generous margins. All of which still scale inside our flexible layout—but visually speaking, the priorities have gone way off. Look at the top of our site, where the lead image now dominates the entire page (fig 4.2). Since we’re cropping the image with the overflow property, it isn’t scaling with the rest of our flexible grid. What’s more, the subject of the image, our beloved robot, is actually getting pretty severely clipped. So we’re left with an image that’s not only huge, but barely com- prehensible. Fantastic. fig 4.2: It’s not exactly sunshine and puppies at the top of our design. Whatever that means. Sitting in the shadow of that gigantic graphic, our logo has scaled down to a nearly microscopic size. And what little pad- ding we enjoyed between the navigation and the lead image has been lost, making the entire masthead feel claustrophobic. As much as I hate to say it, our visual hierarchy is reduced to shambles as soon as we move slightly below the resolution we originally designed for. 66 RESPONSIVE WEB DESIGN Miniature grid, monster problems And that’s not the worst of it. If we bring the browser win- dow in a bit more to around 600 pixels—the width of a small browser window, or of newer tablet computers held in por- trait mode—the headaches just keep coming (fig 4.3). At the top of the screen, our visual hierarchy’s still a mess: the lead image is now cropped to the point of incoherence, and our poor logo is even more of a thumbnail. But now our primary navigation is wrapping in a fairly embarrassing manner. Surely we can do better than that? fig 4.3: Every visitor to our site will absolutely love this broken-looking navigation. No, trust me. They totally will. Moving down the page, our blog is really starting to suffer (fig 4.4). Where the two-column layout once provided easy access to some additional information, it now makes content in each column feel constricted. In particular, the article’s lines are uncomfortably short, making for a decidedly awful read- ing experience. And the photo set within our blog entry looks inconsequential, the content of the picture almost hard to discern. Finally, to conclude our little sideshow of tears, the photo module at the bottom of the page is probably the worst of all (fig 4.5). You thought the image in our blog entry was bad? These photos are comically small, and nearly indecipherable. MEDIA QUERIES 67 fig 4.4: Reading this entry / feels like scanning a haiku: / painfully short lines. fig 4.5: Tiny pictures, monstrous margins. A match made in... well, somewhere not great. The generous margins we initially used to frame those pic- tures now seem wildly out of proportion, drowning our pho- tos in a sea of whitespace. 68 RESPONSIVE WEB DESIGN Widescreen woes Our problems aren’t isolated to the smaller end of the resolu- tion spectrum, however. If we maximize our browser win- dow, a whole new slew of design issues present themselves. The intro (fig 4.6) doesn’t look awful, but the image is now smaller than the space allotted for it. That aside, things don’t look terrible up top—far from ideal, I admit, but not utterly abysmal either. In general, our flexible grid looks okay up there. fig 4.6: That intro is just uncomfortably wide. So let’s quash those good feelings by scrolling down to look at the blog (fig 4.7). Remember how clipped our entry’s lines felt in the smaller window? I’m almost missing those cramped spaces, because these lines are just frighteningly long: the width of that article column is entirely too generous at this level. As much as my eye loves to spend hours circling back to the beginning of the next line I’m supposed to read, there has to be a better way. MEDIA QUERIES 69 fig 4.7: Moving down the page, the blog doesn’t fare quite so well. Long lines, tiny images, sad Ethan. And finally, our photo gallery completely dominates the bottom of the page (fig 4.8). The images themselves look fine, but they’re cartoonishly large. In fact, on my monitor there’s no hint of any content above or below the module. Is this re- ally the best way to present this information to our readers? THE PROBLEM AT HAND We’ve identified a host of visual problems. But there’s a larger issue coming into focus. As we move beyond the resolution for which it was originally designed, our grid becomes a li- ability to our content. Its proportions constrict our content at smaller resolutions, and isolate it in a sea of whitespace at higher ones. This isn’t a problem unique to flexible layouts, however. No design, fixed or fluid, scales well beyond the context for which it was originally designed. So how can we create a design that can adapt to changes in screen resolution and viewport dimensions? How can our 70 RESPONSIVE WEB DESIGN fig 4.8: These images are, to use a technical term, large ’n‘ chunky. page optimize itself for the myriad browsers and devices that access it? In other words, how can our designs become more responsive? SLOUCHING TOWARD RESPONSIVENESS Thankfully, the W3C has been wrestling with this question for some time. To better understand the solution they eventu- ally presented, it’s worth reviewing a bit of the backstory. Meet the media types Their first stab at a solution was media types, part of the CSS2 specification (http://bkaprt.com/rwd/24/). Here’s how they were first described: On occasion, however, style sheets for different media types may share a property, but require different values for that property. MEDIA QUERIES 71 For example, the “font‐size” property is useful both for screen and print media. The two media types are different enough to require different values for the common property; a document will typically need a larger font on a computer screen than on paper. Therefore, it is necessary to express that a style sheet, or a section of a style sheet, applies to certain media types. Okay, yeah. That’s a bit obtuse, isn’t it? Let’s try it in non-robot terms. Ever written a print stylesheet (http://bkaprt.com/rwd/25/)? Then you’re already familiar with the concept of designing for different kinds of media. The ideal browsing experience couldn’t differ more between desktop browsers and printers, or between handheld devices and speaking browsers. To address this the W3C created a list of media types (http://bkaprt.com/ rwd/26/), attempting to classify each browser or device under a broad, media-specific category. The recognized media types are: all, braille, embossed, handheld, print, projection, screen, speech, tty, and tv. Some of these media types, like print or screen, or per- haps even projection, are probably ones you’ve used before. Perhaps others like embossed (for paged braille printers) or speech (for speaking browsers and interfaces) seem new. But all of these media types were created so that we could better design for each type of browser or device, by conditionally loading CSS tailored for each. So a screen-based device would ignore CSS loaded with the print media type, and vice versa. And for style rules meant to apply to all devices, the specifica- tion created the all supergroup. In practice, that meant customizing the media attribute of a link: Or perhaps creating an @media block in your stylesheet, and associating it with a particular media type: 72 RESPONSIVE WEB DESIGN @media screen { body { font‐size: 100%; } } @media print { body { font‐size: 15pt; } } In each case, the specification suggests the browser would identify itself as belonging to one of the media types. (“I’m a desktop browser! I belong to the screen media type.” “I smell like ink cartridges and toner: I’m print media.” “I’m your video game console’s browser: I’m media tv.” And so on.) Upon loading the page, the browser would then render only the CSS pertaining to its particular media type, and disregard the rest. And in theory, this is a fantastic idea. Theory being, of course, the last thing hard-working web designers need. Miscast types Several problems with media types became evident when all these little small-screen browsers, like phones and tablets, ar- rived on the scene. According to the specification, designers could have targeted them simply by creating a stylesheet for the handheld media type: The problem with this approach is, well, us—at least in part. Early mobile devices didn’t have sufficiently capable browsers so we largely ignored them, choosing instead to MEDIA QUERIES 73 design compelling screen- or print-specific stylesheets. And when capable small-screen browsers finally did appear, there weren’t a lot of handheld CSS files scattered about the web. As a result, many mobile browser makers decided to default to reading screen-based stylesheets. But what’s more, media types paint with an incredibly broad brush. Is one handheld stylesheet really suited to ad- dress the challenges of designing for an iPhone and a five year- old feature phone? Enter the media query Realizing some of the failings of media types, the W3C used their work on CSS3 to take another crack at the problem. The result was media queries (http://bkaprt.com/rwd/27/), an incredibly robust mechanism for identifying not only types of media, but for actually inspecting the physical characteristics of the devices and browsers that render our content. Let’s take a look: @media screen and (min‐width: 1024px) { body { font‐size: 100%; } } Now, every media query—including the one above—has two components: 1. Each query still begins with a media type (screen), drawn from the CSS2.1 specification’s list of approved media types (http://bkaprt.com/rwd/26/). 2. Immediately after comes the query itself, wrapped in pa- rentheses: (min‐width: 1024px). And our query can, in turn, be split into two components: the name of a feature (min‐width) and a corresponding value (1024px). Think of a media query like a test for your browser. When a browser reads your stylesheet, the screen and (min‐width: 74 RESPONSIVE WEB DESIGN 1024px) query asks two questions: first, if it belongs to the screen media type; and if it does, if the browser’s viewport is at least 1024 pixels wide. If the browser meets both of those criteria, then the styles enclosed within the query are ren- dered; if not, the browser happily disregards the styles, and continues on its merry way. Our media query above is written as part of an @media declaration, which enables us to put queries directly inside a stylesheet. But you can also place queries on link elements by inserting them into the media attribute: Or you can attach them to @import statements: @import url("wide.css") screen and (min‐width: 1024px); I personally prefer the @media approach since it keeps your code consolidated in a single file, while reducing the num- ber of extraneous requests the browser has to make to your server. But no matter how you write your queries, the result in each scenario is the same: if the browser matches the media type and meets the condition outlined in our query, it applies the enclosed CSS. Otherwise, it won’t. Meet the features It’s not just about testing for width and height. There are a host of features listed in the specification our queries can test. But before we dive in, it’s worth noting that the language used to describe the features can be a bit... dense. Here are two quick guidelines that helped me sort it out: 1. In the spec’s language, every device has a “display area” and “rendering surface.” Clear as mud, that. But think of it this way: the browser’s viewport is the display area; the entire display is the rendering surface. So on your laptop, the dis- MEDIA QUERIES 75 play area would be your browser window; the rendering surface would be your screen. (I don’t makes the terms. I just explains ’em.) 2. To test values above or below a certain threshold, some features accept min‐ and max‐ prefixes. A fine example is width: you can serve CSS conditionally to viewports above 1024 pixels by writing (min‐width: 1024px), or below 1024 pixels with (max‐width: 1024px). Got all that? Fantastic. With those two points out of the way, let’s dive into the features the specification says we can use in our queries (http://bkaprt.com/rwd/28/) (table 4.1). table 4.1: A list of the device features we can test in our media queries. FEATURE NAME DEFINITION HAS min‐ AND max‐ PREFIXES width The width of the display area. height The height of the display area. device‐width The width of the device’s rendering surface. device‐height The height of the device’s rendering surface. 76 RESPONSIVE WEB DESIGN FEATURE NAME DEFINITION HAS min‐ AND max‐ PREFIXES orientation Accepts portrait or landscape values. aspect‐ratio Ratio of the display area’s width over its height. For example: on a desktop, you’d be able to query if the browser window is at a 16:9 aspect ratio. device‐aspect‐ Ratio of the device’s ratio rendering surface width over its height. For example: on a desktop, you’d be able to query if the screen is at a 16:9 aspect ratio. color The number of bits per color component of the device. For example, an 8-bit color device would successfully pass a query of (color: 8). Non- color devices should return a value of 0. color‐index The number of entries in the color lookup table of the output device. For example, @media screen and (min‐ color‐index: 256). MEDIA QUERIES 77 FEATURE NAME DEFINITION HAS min‐ AND max‐ PREFIXES monochrome Similar to color, the monochrome feature lets us test the number of bits per pixel in a monochrome device. resolution Tests the density of the pixels in the device, such as screen and (resolution: 72dpi) or screen and (max‐resolution: 300dpi). scan For tv-based browsing, measures whether the scanning process is either progressive or scan. grid Tests whether the device is a grid-based display, like feature phones with one fixed-width font. Can be expressed simply as (grid). What’s really exciting is that we can chain multiple queries together with the and keyword: @media screen and (min‐device‐width: 480px) and (orientation: landscape) { … } This allows us to test for multiple features in a single query, creating more complex tests for the devices viewing our designs. 78 RESPONSIVE WEB DESIGN Know thy features Feeling drunk with power yet? Well, I should take this oppor- tunity to mention that not all @media-aware browsers support querying for all features outlined in the specification. Here’s a quick example: when Apple’s iPad first launched, it shipped with media query supp