Responsive Containers

July 15th, 2011

Media queries are clearly a huge step forward for responsive layouts. Letting you apply different CSS based on the width of the device or the viewport opens up all sorts of possibilities for adapting content to best fit a given space. Media queries work really well when you want to adjust the core layouts of the site, but they’re less suited to changing styles at a smaller more granular level.

When I’m building a specific front-end component, I take great care to make sure it is completely decoupled from any particular layout. It’s not concerned with the width at which it might be displayed at: it could be moved from a small side bar into a larger main column and its width simply adapts to whatever its parent element constrains it to. At Clearleft we take this as far as building all the content components of a site outside of any layout constraints whatsoever.

However, there are occasions when you might want to adapt the styles within the component itself depending on the width at which it is currently being displayed at. There’s a very simple example on the Fontdeck sign up page where the form labels pop up above the text inputs if the viewport is below a certain width.

The example on Fontdeck uses a media query to adjust the display of the label elements when the viewport is more than 1000px, but surely there’s a better way than that? For example, if I moved that form into the right hand column, wouldn’t it be better if it knew what width it was being rendered at and automatically adjusted its layout to the narrower width version. In other words, the layout of the component responded based on the width of its containing element, rather than on the essentially arbitrary width of the viewport.

For fun, here’s some made-up syntax (which Jeremy has dubbed ‘selector queries’) that describes this:

.signup-form @selector(min-width: 300px) {
.label {
display: block;
}
}

I think there’s a valid use case for this capability. Being able to adjust a single component based on its current layout is extremely powerful. It allows more re-usability of code, and it encourages designers to think about how an individual component adapts on its own, outside of any given layout. Decoupling individual content components from their layout is a key principle of OOCSS, and this encourages that principle.

On a current development project I’m using a JavaScript technique to achieve nearly the same result. It uses a small script (about 1.5k minified) to add class names to elements depending on their width.

Here’s how you use it:

<div class="signup-form" data-squery="min-width:300px=wide max-width:10em=small">
<label for="name">Name</label
<input type="text" id="name />
</div>

The above code tells the JavaScript to apply a class of wide when the elements offsetWidth is greater than 300 pixels and a class of small when it is less than 10 ems. It looks a little odd of course. I’d rather not have that presentational information in a data attribute (it’s hardly data)—but of course the best place for all of this would be in CSS, so how it’s exposed in the markup is kind of a moot point right now.

The script is on Github, along with an example test page (yes, try resizing the browser and changing text size). It works in all modern browsers back to and including IE6.

Responsive by default

July 7th, 2011

If you think about it, responsive layout is not a new thing. Open a simple HTML file in a web browser, and the content automatically adapts to fit the width of that browser. The web is responsive on its own—by default. It’s us that’s been breaking it all these years by placing content in fixed-width containers. Despite the fact that people have been arguing against this for years (please excuse the uncool URI).

Mark Boulton describes current thinking on this as a shift, rather than a trend. I prefer to think of it as a trend, but a trend that is coming to an end. Just as the Web Standards movement ended the unsatisfactory trend of presentational HTML and tables for layout, responsive layout ends the now old-school trend of designing for a 960px (or whatever) width Photoshop page.

The web has a way of fixing its bad habits over time. It’s smart like that.

What is responsiveness?

Responsiveness is what a website does when it’s loaded into an unknown browser on an unknown device by an unknown individual. It’s how you deal with “the most hostile software development environment ever imagined” (via Douglas Crockford). Like progressive enhancement it’s a strategy that frees you to work with the web rather than fight against it.

Optimising for One Web, instead of specific browsers/devices/individuals, is an ideal that is a profound part of being a web developer. It has to be at the heart of everything you design and build. If not, you’re doomed to write code and support an ever increasing array of client platforms from whatever the browser landscape throws at you in the coming years. Good luck with that.

You’ll note however, that I describe it as an ideal. It’s important to understand and accept that current technology and methodology won’t always get you where you need to be. Pretending we can create a perfect solution just because we’ve discovered media queries is as dangerous as ignoring responsive design altogether.

To begin with there are a bunch of things we can’t easily feature detect. Then there are bugs and quirks in the way browsers implement some of the newer technologies which make it difficult to work with them cross-browser. Then, you might find performance is so important that you want to feature detect completely through UA parsing (that’s an edge-case if ever there was one, but it’s legitimate).

Being a web application does not excuse you from these principles. I work mostly on content based sites these days, but that’s not my background. For the bulk of my career I worked on applications with 30-40,000 lines of JavaScript. The non-legacy bits that I was in charge of were built with the principles of progressive enhancement, and they worked on my fat desktop, my skinny mobile (when the CPU could keep up), and they worked when JavaScript was disabled. It wasn’t perfect, but that ideal we were aiming for normally kept us on course, and for the size of application we were dealing with we had a pretty maintainable and scalable codebase that lasted a good number of years and lived through a good number of browser and device releases (including the first generation iPhone).

The best web developers understand the medium, and work to its ideals. But they know what the current limits are and where they need to work around the problem in a way that gets the job done for 95% of users, even if it means a one-off well-understood browser detect.

For me responsive design is another example of web design getting back some of its Dao. That’s why it’s not an added extra or a feature. It’s core to what you do.

View archives