Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - using one background image with multiple images on it

I've observed that often the websites use only one background image which contains multiple images on it. For example, instead of using separately icons, all of the icons are put on one image and then the different parts of image are used in different section.

  • Is there any advantage to this?
  • How can this be used?

For example, for the following Stack Overflow sprite, how would I display just one of the images?

Stack Overflow Sprite Example

like image 779
Roman Avatar asked Mar 04 '11 14:03

Roman


People also ask

How do I put multiple images as my background in CSS?

CSS allows you to add multiple background images for an element, through the background-image property. The different background images are separated by commas, and the images are stacked on top of each other, where the first image is closest to the viewer.

Can an image have multiple background layers?

You can apply multiple backgrounds to elements. These are layered atop one another with the first background you provide on top and the last background listed in the back. Only the last background can include a background color.

Does CSS3 allows you to use several background images for an element select one Otrue Ofalse?

CSS3 allows you to use several background images for an element.

What will happen if we use CSS to set both a background image and a background color for the same element?

What will happen if we use CSS to set both a background image and a background color for the same element? The background image will show with a border around it in the background color. The background image and the background color will be blended and both will show.


2 Answers

The technique is called CSS Sprites. Basically you use CSS's background-position property and fixed height or width for your element.

If your elemnts are fixed width and fixed height at the same time you can freely create a more compact image. See this site for more complex examples.

like image 99
vbence Avatar answered Sep 20 '22 17:09

vbence


You are talking about CSS sprites, in which the background position changes on hover. Learn more here:

http://css-tricks.com/css-sprites/

like image 35
Dave Kiss Avatar answered Sep 22 '22 17:09

Dave Kiss