Which responsive images solution should you use?

32 篇文章 0 订阅

Which responsive images solution should you use?

There are a bunch of techniques going around for dealing with responsive images lately. That is, solutions to help us serve the right image for the occasion (e.g. size of screen and bandwidth available). They all do things a bit differently. To keep track, Christopher Schmitt and I have created this spreadsheet of techniques.

The spreadsheet has the data, but let's digest it through thinking about it through the lens of practical questions.

To choose which technique is right for you and your project these questions may help as a guide. Many of the questions may apply to your project, so you'll have to sort out which techniques fit what scenarios and find the overlap.

Do I have legacy content?

Which really means... do I have legacy content that is impractical to update? For instance, I have thousands of pages of content on CSS-Tricks and a writing staff of one.

 Yeahhhh... I'm not going to go back and update every  <img> on the site, so I need a technique that will allow me to leave those alone.

The only technique I know of that works with absolutely no markup changes is Adaptive Images. It works by routing requests for images through a PHP file which intelligently serves (and creates if need be) images of the appropriate size for the screen width.

Another question to ask yourself is if you care about legacy content. Perhaps the vast majority of traffic to your site is for newer content in which you can make markup changes and thus take advantage of other techniques. If that's the case, read on to discover those other techniques.

If you have a small project, a brand new project, or a project with legacy content that you are able go back and update, you are also able to choose a technique which does require special markup, so also read on.

Do I care about special markup?

This is really a sub-question of the above. Many of the techniques require you to use special HTML. For example,HiSRC has you put higher resolution images as data-attributes:

<img src="200x100.png" data-1x="400x200.png" data-2x="800x400.png">

I'd say this is a clean, valid, semantic technique, but it also means that you need these attributes on every <img>on your site, which may not be possible on sites with loads of legacy content.

If you know that special markup (or specialized CSS) is not an option for you, really the only option is Adaptive Images. Even Sencha.IO requires prefixing the src attribute which may not be possible with legacy content.

Do I care about semantics?

Some responsive images techniques involve markup which isn't strictly semantic. Ultimately, there is only one way an image can be semantic. That is if the src of it points to a real image and it has alt text describing that image. Brad Frost sums it up nicely:

@stowball Unfortunately its not that simple. A picture of a horse needs to be a picture of a horse or else its not a picture of a horse. :)

— Brad Frost (@brad_frost) April 5, 2012

In other words, if the technique requires at any point the src of the image to be missing or link to a transparent GIF (or the like), that's not semantic.

So why do some responsive images techniques do this? Because having an image with a src that points to that image of a horse means that that image will start downloading as soon as that image gets parsed by the browser. There is no practical way to prevent this. Even if you super quickly swap out that src with a more appropriate version, now you're downloading two images instead of one which is a performance hit instead of a performance gain. You may decide that's acceptable (e.g. "desktop" environments typically have more bandwidth). Usually if this technique is employed, the image in the src is the smallest possible image.

If semantics is important to you, you should look at Adaptive Images (covered above) or HiSRC, a plugin by Christopher Schmitt which you can use with a semantic src attribute.

A few of the techniques use <noscript> tags in which to place a fallback <img> in the case of no JavaScript being available. I'll let you decide if that's semantic or not.

Do I care about validity?

Validity as in "Does it validate?" according to the W3C Markup Validation Service. Validation is a tool to help you find problems and write better markup. But just because something doesn't validate doesn't make it bad or wrong. If invalid code works wonderfully in all browsers, you nor anyone else should care.

If you do care about validity (perhaps a client irrationally requires it from you and is holding your paycheck randsom) then there are a few techniques that you can't use. The picturefill technique, for instance, uses the<picture> element to work its magic. This may ultimately be standarized, but it isn't yet, so it's technically invalid syntax. It's also required that <img> elements have a src attribute, so techniques that remove that to get around the double-image-request problem are invalid.

I'd recommend these techniques if validity is a requirement for you: Adaptive ImagesHiSRC, or Responsive Enhance. All of which use simple, valid <img> syntax that include a src.

Do I care about art direction?

Some responsive images techniques serve different resolution versions of the exact same image. While that makes things easier (i.e. less work) it may not acceptable. Here's a visual example:

On the left, the "mobile" and default  src. In the middle, a slightly larger image that could be used on (ahem) "tablets". On the right, the largest of the images. (credit)

These images are hand-crafted by a designer, cropped to retain meaning and impact. If you took the image on the right and just scaled it down, the people in the image would be very small and the feel of the image my be lost.

If this idea of art direction is important to your project, you'll need a technique that will allow you to specify exactly which src to serve under which circumstances. This is picture perfect (GET IT?) for picturefill which allows you to be very specific about sources and what circumstances get what.

<picture alt="description">
  <source src="small.jpg">
  <source src="medium.jpg" media="(min-width: 400px)">
  <source src="large.jpg" media="(min-width: 800px)">
</picture>

JavaScript takes it from there.

Do I care about JavaScript?

Most of these responsive image techniques utilize JavaScript to work their magic. Some only a tiny bit to set a cookie, but JavaScript none the less. Some of them have you put an <img> in a <noscript> tag so that there is a fallback image in the case that the user has JavaScript turned off. If you don't like that, and you need to make absolutely sure that your images work without JavaScript, Sencha.IO is your best bet. This service works by identifying your device through it's User Agent string and serving an appropriately sized image. So you link to the largest (reasonable) version you have of it and Sencha will squeeze it down and server smaller versions if need be (it doesn't scale up, for obvious reasons).

What about JavaScript *library* dependency?

HiSRC and rwdImages are both jQuery dependent. If your project is using a different library, these probably aren't for you. But hey, you could port it and open source that! If you aren't using a library, well, you probably should be but let's not get into that.

Do I care about Server Side Components?

Some of these techniques aren't solely JavaScript dependent. Adaptive Images works it's magic primarily through .htaccess and PHP. Well, .htaccess presupposes an Apache server. And, while of course we all know and love PHP (ahem), many websites run on technologies like Ruby or Python.

Responsive Images (the original Filament Group technique) also uses .htaccess. So if you're using something likeNginx as web server, this is either out or you'll have to port over the .htaccess component to Nginx's similar-but-different syntax.

Do I care about bandwidth testing?

Testing the browser window width and making decisions on what image to serve based on that is pretty cool and fundamental to the concept of responsive images. But it's really only half of what the decision of what image should be served should be based on. The other half is available bandwidth. If the user has a very fast internet connection speed, serving large images is OK. If the user has a very slow internet connection speed, they should get smaller images (regardless of screens size). Unfortunately native bandwidth media queries don't exist.

Two of the current techniques do bandwidth testing as part of their decision making: Foresight.js and HiSRC (both are based on the technique in Foresight.js). It works by downloading a test file and measuring how long it took (configurable). The test itself is a slight performance hit, but theoretically the savings gained by serving images based on knowing the current bandwidth is a net (GET IT?) gain.

Do I care about relying on third parties?

Sencha.IO is a completely third-party way of handling responsive images. As far as I know, it works great and hasn't been inflicted with any major downtime, but of course you always run that risk.

You might be thinking: Wow, the Sencha.IO technique is really cool but I worry about the third-party dependency. I wish I could run that on my own server. If you want to go down that road, there is the public WURFL databaseand this Server Side Responsive Images technique which puts that to work locally.

There are also third-party services like Device Atlas Cloud which does device detection for you. It's also a third-party dependency for your app. No doubt their goal and focus is staying up and fast at all times, but you have to be very careful about who and what you rely on for your business.

Is there a specific CMS with specific CMS powers involved?

Say your project is in WordPress. WordPress has a media uploader built in. When you upload an image with it, it can create multiple versions (scaling down) of that image for you. That's pretty cool and powerful and you could/should take advantage of that. Keir Whitaker talks about using that ability in his article Automatic Responsive Images in WordPress.

This isn't just a WordPress thing though. I'm sure the concepts at work here could be done (or made to be done) in any Content Management System.

Can I wait for the future?

The release of the "new iPad" (the third one, for longevity) is what sparked a lot of these techniques and conversations. Its high pixel density is great for vectors and big photos, but actually not great for things like small icons that need to be scaled up to be the correct size and can be blurry. But serving higher resolution icons means larger file sizes and slower websites. Hence, the need to only serve them in situations/environments that need them.

The world of web standards is aware of this problem. There is a whole group dedicated to talking about it. In time, they may solve it and then we can start using whatever way they come up with (assuming its awesome and better than what we have now).

It may be flipping out the src of images through CSS content like Nicolas Gallagher suggested. It might be the<picture> element. It might be a srclist attribute in HTML or src property in CSS. It might be a prefix.

 

More resources

Which responsive images solution should you use? is a post from CSS-Tricks

共享 保持为未读状态

Opt-in Typography

I recently heard Chris Eppstein give a talk (slides) about creating better stylesheets and using SASS to do it. There were a couple of surprising bits in there, one of which was about "opt-in typography." The idea was that instead of setting global styles for typographic elements like pulolh1h2, etc that you would instead apply those styles as a class, perhaps .text.

The idea is that there is a lot of places on a website where you want a clean slate to work from and not be worried about what global styles are doing. The classic example is lists, where when using them for things like navigation you likely don't want the same margin/padding/list-style as you would within an article.

Further, you might have different sets of typographic styles you may want to apply at will. So, instead of global styles like this:

p  {    }
ul {    }
ol {    }
h1 {    }
h2 {    }
/* etc */

You would scope them to a class instead (using SCSS here):

.text-article {
  p  {    }
  ul {    }
  ol {    }
  h1 {    }
  h2 {    }
  /* etc */
}

.text-module {
  p  {    }
  ul {    }
  ol {    }
  h1 {    }
  h2 {    }
  /* etc */
}

In an area of the site that's a blog post, you can snag your article typography, and in a sidebar module, you can grab the typography for that:

<section class="main-content">
  <article class="text-article">
    ...
  </article>
</section>
<aside>
  <div class="module text-module">
     Texty module
  </div>
  <div class="module">
     Module in which typography isn't needed
  </div>
</aside>

Anthony Short feels the same way.

Of course, the effectiveness of this depends on the website. What kind of site it is and what stage in it's evolution it's in. I suspect it would work better on websites that have a more "modular" architecture and that don't have a huge set of legacy content. I'd be interested in trying it on a fresh project.

Opt-in Typography is a post from CSS-Tricks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值