Skip to main content

Responsive Images 101, Part 8: CSS Images

By Jason Grigsby

Published on June 3rd, 2015

Topics

Most of the time when people refer to responsive images, they are referring to inline images, not CSS images.

This is because before <picture> and srcset there were no good solutions for inline responsive images. When it comes to CSS images, we could always use media queries. So why worry?

But now it is time to revisit responsive CSS images and look at the solutions anew based on what we’ve learned about inline images.

Just like when we’re working with inline images, one of the first questions we’ll need to ask ourselves is whether we’re dealing with the resolution switching or art direction use case.

For resolution switching, we should strive to provide the browser with options and let the browser pick the best possible image. The browser is in a better position to know what image will work best based on user preference, network conditions, etc.

To provide the browser with options, we should use the image-set() syntax.

image-set() syntax repeated below

You may notice some similarity between image-set() and srcset. In fact, srcset was modeled after image-set().

background-image: image-set( "foo.png" 1x, "foo-2x.png" 2x);
Code language: CSS (css)

Like srcset, image-set’s value contains a comma-separated list of image URIs along with a display density descriptor. If a display density descriptor isn’t provided, it is assumed to be 1x.

However, image-set() does not support width descriptors yet. The plan is to refine image-set() to provide feature parity with srcset.

While most of the examples you will see for image-set() show it applied to background-image, it can be applied to any CSS property that accepts images.

image-set() was the first responsive images specific syntax, and as mentioned, it was the foundation for srcset.

However, because we had solutions for CSS responsive images using media queries, image-set() was ignored by nearly everyone. The Responsive Images Community Group didn’t spend much time discussing it. Browsers didn’t prioritize implementing it.

Once we were nearing completion of the <picture> and srcset standards, we looked around and realized that we had neglected image-set(). Work is underway to increase the functionality of image-set() to bring it in line with srcset.

But as of publication, despite being the first responsive images standard, browser support for image-set() is lacking. It is available with a webkit prefix in Chrome, Opera, and Safari. Neither Firefox nor Microsoft have implemented it yet.

So why include it in this Responsive Images 101 series?

Because image-set() is the correct solution for resolution switching. When image-set() is widely supported, we should use it for all of the same reasons we use srcset instead of <picture> with the media attribute when we’re dealing with resolution switching.

Until image-set() is widely supported, you’ll probably end up using the CSS art direction solution for resolution switching.

What is the CSS solution for art direction? Media queries.

It’s that simple. In fact, I’m going to assume you know media queries so no syntax sample here.

But as long as I’ve got your ear, make sure your media queries for images don’t overlap or you’ll end up with duplicate downloads. If you have any doubts, check out Tim Kadlec’s Media Query & Asset Downloading Results.

If you want to support high density displays in art direction, you’ll probably want to use the new resolution media queries.

Resolution media query syntax. Repeated below.

The resolution media query allows you to apply specific CSS rules to devices that meet the display density that you define.

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
/* High density stuff here */
}
Code language: CSS (css)

(Much thanks to CSS Tricks for this syntax sample.)

The first thing you’ll notice in the syntax above is that we’re including a -webkit prefixed media query. This is for devices that support the old device-pixel-ratio syntax. The only devices that got in the wild with this syntax used the -webkit prefix which is why it is the only one listed.

The syntax going forward is the resolution media query. In our example, we’re using min-resolution, but as you probably guessed, there is a complimentary max-resolution feature that can be used instead.

The resolution media query can check one of three things:

  • dpi — dots per inch
  • dpcm — dots per centimeter
  • dppx — dots per px unit

The first two are fairly straight-forward, but I found dppx confusing. The Mozilla Developer Network documentation defines ddpx thusly:

This unit represents the number of dots per px unit. Due to the 1:96 fixed ratio of CSS in to CSS px, 1dppx is equivalent to 96dpi, that corresponds to the default resolution of images displayed in CSS as defined by image-resolution.

Confused? I certainly was when I first read it.

Here’s the way I’ve begun to think about it, the idea of 1x, 2x, 3x, etc. is based on an imprecision. The value of 1x on some devices is different than others because some devices are 72dpi or 96dpi or whatever.

But from a CSS perspective, these differences don’t matter. The CSS Working Group has decided that there will always be a 1:96 fixed ratio of CSS inches to CSS pixels.

So while 1x might leave things up to interpretation because of 72dpi vs. 96dpi screens, 1dppx will always be what you and I think of as “1x”.

You may be asking yourself, why was 1x sufficient for srcset and image-set, but for min-resolution, it was necessary to use dppx?

I don’t know. All I know is that you can think of 1dppx as 1x, 2dppx as 2x, and so on. At this point, I’ve just accepted the inconsistency and decided to move on with my life. I recommend you do the same. 😉

Believe it or not, responsive images syntax is the easy part. In Part 9, we’ll discuss the vexing challenge of picking your image breakpoints.

  1. Definitions
  2. Img Required
  3. Srcset Display Density
  4. Srcset Width Descriptors
  5. Sizes
  6. Picture Element
  7. Type
  8. Currently Viewing:CSS Responsive Images
  9. Image breakpoints
  10. Conclusion

Comments

Šime Vidas said:

“Until image-set() is widely supported, you’ll probably end up using the CSS art direction solution for resolution switching.”

What? How? Didn’t you mean, we’ll end up using resolution media queries instead?

Replies to Šime Vidas

Jason Grigsby (Article Author ) replied:

No, I realize this is a little confusing, but from a use case perspective, resolution switching covers both scenarios of display density and also the same image displayed in the page at different sizes (what we typically think of when we say responsive images).

So the resolution media query is great for 1x, 2x, etc, but won’t help you provide a 200×200 image source if the viewport is smaller than 400px wide and a 500×500 image source if the viewport is smaller than 800 pixels, etc. For that, you just need same media queries that you use to modify layout (max-width, min-width, etc.).

Replies to Jason Grigsby
Šime Vidas replied:

I see (I assumed resolution switching == display density.) However, the current -webkit-image-set() implementation is restricted to display density, so its functionality can be achieved using resolution media queries, or?

Replies to Šime Vidas
Jason Grigsby (Article Author ) replied:

The difference between image-set and resolution media queries is very similar to the difference between srcset and picture using the media attribute.

When using image-set or srcset, you’re giving the browser options and letting the browser choice the best source based on whatever heuristics it can use to make a smart decision.

When using the media attribute on picture source elements or when using the resolution media query, you’re dictating to the browser what source it MUST use.

Whenever we’re dealing with the resolution switching use case, I have a strong bias for letting the browser have choice as the browser is in a better position to know user preference.

Hiren Khambhayta said:

Jason really worth content on responsive images.
I want to know that is it any possible way to make the image with IMG tag a responsive one? Because main product photos and few fancybox gallery need image tag instead of calling from css. And when viewed on ipad or mobile device need to resize image size so that it look like gallery. Facing the issue mainly in gallery as have to call big size image for monitors and have to call the same for mobile small screens also.
Page I am talking about is Brass Battery Terminals page of my website.
Expecting helpful reply.