Sprites aren’t limited to background-image, as with the object-fit and object-position properties we can nudge an inline image around its content-box to act just like a sprite. For example, let’s say we want the image below to be added to our HTML page like a regular ol’ image:

<img src='sprite.png' alt='Icons'>
Then we’ll crop the image so that only the first icon is visible on screen:
img {
object-fit: none;
object-position: 0 0;
width: 100px;
height: 100px;
}

Here, the content-box of the <img>
should be 100px wide and 100px tall, but because the image extends beyond those boundaries, it’s automatically cropped for us with object-fit: none
. We might then want to use a class to nudge the image and reveal another part of it altogether:
.book {
object-position: 0 -234px;
}

These sprites can be in any regular image format but it’s also possible to use the same technique with SVG. Here’s an example that currently works in the latest stable version of Chrome:
See the Pen Sprites with object-position by Robin Rendle (@robinrendle) on CodePen.
Image Slider
Using a dab of JavaScript, we can actually use this same concept to create an image slider out of a single <img>
tag. When the image is clicked, just change some classes which change the object-position.
See the Pen SVG sprite with object-position by Robin Rendle (@robinrendle) on CodePen.
Support
Keep this in mind for the future, since unfortunately the browser support for object-fit
isn’t particularly great at the moment. The current desktop versions of Safari and Firefox don’t support it and neither does iOS. So make sure to double check the almanac entry for object-fit
before using this trick anytime soon.
Great article! Will be trying this out soon…
I don’t get it. What is the point reinventing the wheel using something with such a poor browser support ?
http://caniuse.com/#feat=object-fit
Thanks anyway.
The browser support was clearly noted in the article. I think it’s super super interesting. A bonafide CSS trick, the namesake of this site.
<img>
are content,background-image
is design, so theoretically useful for images which have no supporting text, where you could use the alt text.for instance.
Not to mention the single-request super-simple image slider with a single tag. That’s highly appealing to me.
I like these posts about cutting edge tips that might be usable in the future, it keeps the web exciting. Without them you just repeat the same old stuff.
Nice tip, but as AlienSKP says, there’s little browser support.
I’m also worried about semantics, the idea of having inline images is that you may remove all styling to a page and still see it’s content correctly.
Also, what would be the advantage of using img over css sprites using background images?
The “it would look wrong without CSS” is interesting. Not something I generally worry about, but if this was being used within content that was going out to RSS or something, that could get weird.
I like the idea that a JavaScript-enhanced image slider using this technique would fall back to a big combined image everything though, that’s pretty cool.
The same result could be achieved using good old
clip
property. Alas, element must be absolute positioned for that.It’s a good sort of CSS practice. One day all browser’s will support it.
Every time an article points out a new cool browser feature an army of commenters comes to note that there is little support for it. Doh. Obviously at one point EVERY CSS feature had little support.
Did it ever occur to you that maybe browser makers read CSS tricks? If there is a lot of people who want a particular feature it will surely make it’s way to browsers faster.
Thanks for the article, I’m happy to know about this trick.
This is exactly what I thought too. It’s important to try new things and be ready for when it is supported, otherwise you are always playing catch up.
I agree. Just think where CSS would be now, if noone had ever experimented with it. Back in the day of the first CSS, the browser support was minimal. Thinking we’d all stuck to using table based layouts then, makes me smile.
Thanks for the article and explanation, it looks like object-fit will be very useful in the future.
As an aside (and you probably all know this) but you can do the ‘sprite trick’ for older browsers (back to ie6 if you use an anchor for hover effects).
http://codepen.io/paulobrien/full/azVEvM/
I have always thought something like this was a bit an obvious feature, so I am glad to hear it is on the horizon. And it looks nice and simple to implement as well.
I guess from an accessibility POV “Icons” in alt text is not terribly meaningful. You might as well use blank alt text, unless your javascript is updating the alt text whenever a different icon is visible. Then we’d be looking at adding ARIA roles etc too.
thank you for sharing this article
its so useful
http://www.novinmarketing.com/index.aspx?Content=Web-Design
Gonna try this out like asap. http://www.zulusproweddings.com
Well, effect can be retrieve by background-position so why to use this complicated one.
Because this is referring to content, not design elements.
What is the point of using img sprite rather than css sprite as img sprite will take much load time. Also we can’t use img sprite for dynamic galleries.
Oh yes, we are advancing. Till browser support you could use a wrapper around the image with overflow:hidden and move the image with margin-top: negative value. Cheers.