Tuesday, August 27, 2013

Responsive Images with Apropos

When Square's front-end engineering team was building our Japanese website, we decided early on that we needed to support a wide variety of devices, and that responsive design was the right way to do that. (Who wants to support a completely separate “mobile site”?) However, we weren't sure how we would serve every visitor the appropriate version of the gorgeous images shot by our talented photographers.

A whole lotta Sass

We took a look around at existing solutions for serving responsive images, but none of them quite met our criteria. Using Javascript meant extra downloads of unused images. Using cookies meant losing cacheability and made load time worse. We quickly decided that we should rely on CSS, taking advantage of the browser's ability to parse media queries in stylesheets and only download the resources it needs on a particular device.

However, as we started prototyping early versions of pages, we noticed that the amount of CSS we were writing was really adding up, despite the built-in conciseness of Sass. We had settled on three breakpoints, and wanted to support both standard and high-resolution “retina” displays, so we had six different media queries to write for each image. We also planned to support more languages with the same design, which would add yet another dimension of variation.

Apropos

To solve these problems, we built a Compass extension called Apropos. The basic principle is simple: you set up your variants, which could represent different sizes, resolutions, or localizations of an image, and you define extra file extensions which map to those. You end up with a simple file naming system that looks like this:

We also added features such as generating a CSS height property at each breakpoint, calculated from the image file, so that you have more flexibility to adapt your layout at each breakpoint. And we built Apropos so that it’s easy to extend to handle localization and other variations. For more information, check out the documentation.

Today we're releasing Apropos as an open-source project. Check it out, give it a try on your site, and let us know what you think!