Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a recommended amount of spacing between CSS sprites?

Usually if I create CSS sprites, I line them all up next to each other with no spacing at all. For example if all the images are 10x10 pixels I would put them at the coordinates 0,10; 0,20; 10,10; 10,20.

But this seems to cause problems in certain circumstances. For example during page zooming and on mobile you can see the edge of the next sprite along.

Why does this problem occur, is it to do with rounding errors? Is simply adding spacing around the sprites the best way to avoid the problem? If so, is there a minimum or recommended amount of spacing we should have between the icons in the sprite image?

like image 356
DisgruntledGoat Avatar asked Oct 17 '12 16:10

DisgruntledGoat


People also ask

What are the benefits of CSS sprites?

An image sprite is a collection of images put into a single image. A web page with many images can take a long time to load and generates multiple server requests. Using image sprites will reduce the number of server requests and save bandwidth.

How do CSS sprites work?

CSS Sprites are a collection of images that are combined into a single file that an HTML document can access. These images are then called into use within the HTML code to be displayed on the website.

How you would implement CSS sprites on a page or site?

To create a sprite, we make use of CSS properties and set the combined image as the background of an element then we position it using the background-position property. Let us understand this with two different examples: Example 1: Let us use the GeeksforGeeks logo to understand sprites in this example.

What is combine images using CSS sprites?

Combining images using CSS sprites reduces the number of round-trip requests made, which can yield performance gains for your page. This method mostly refers to small images like icons, logos, and other graphical elements that can be combined into a single image and then positioned by CSS on the page.


1 Answers

Sprite Bleeding may occur on zoom due to rounding, especially in IE (the old versions simply round to the nearest integer, e.G. a calculated value of 20.343px would be rendered as 20px).
Since the rounding error is never bigger than 1px, with a padding of 1px on all sides you can already fix that problem.

Modern browsers use a method called sub pixel rendering to mitigate this problem.

like image 74
Christoph Avatar answered Oct 18 '22 09:10

Christoph