I have following CSS sprite. When I try to display the first icon, it works fine, but when I set zoom to 150%, I see also small part of the second icon. This problem is on Google Chrome and Internet Explorer, but not on Mozilla Firefox:
div {
width: 29px;
height: 29px;
background-image: url(http://i.imgur.com/O2Cp0nb.png);
}
<div></div>
That's how it looks for me on 150% zoom:
Update: Chris suggested that I need to put some space between icons. So my question is: why? And why it works fine on Mozilla Firefox even without that space?
The problem seems to be image interpolation or image size rounding errors.
Maybe try this:
It's werid but works in IE 11.
Different browsers may use different algorithms for rounding numbers and rendering images in certain cases. You could read some articles about it, for example — this and this.
So, when your
zoom
is150%
and you have29x29
px image, your browser is need to render43.5x43.5
px image. How each version of each browser will round it? We don't know, maybe43x43
px, maybe44x44
px. There is article about sprites and zoom.I create new code snippet with two pairs of images. The first pair uses your image sprite and the second — my. I increased the Facebook image size from
29x29
px to30x30
px. Try to zoom it. You can see they have problems on differentzoom
ratio (the first — on150%
, the second — on110%
—125%
).JSFiddle
So, my advice is to add
1px
between images to be independent from different browsers rounding algorithms and browsers bugs:JSFiddle
If you need to support old IE browsers, look into this article.
Now you could use some useful things on
gulp
/grunt
to automatically generate sprites where you can set gap/margin between images. See this.Since you have a size defined for the container, use
background-size: cover;
should correct the zooming issue for you. The will image"cover"
the entire width or height of the container.It looks like you're using css pixels to crop a piece of a larger image. This is what i see looking at your image url.
How about using a url with the facebook icon alone set to 29x29 like this facebook icon example?
You can also crop the facebook icon you have and set it to 29x29 with an image editor. I did it for you using Adobe Photoshop
Hope this helps