CSS - using one background image with multiple ima

2019-02-04 18:29发布

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

7条回答
Deceive 欺骗
2楼-- · 2019-02-04 18:39

Change the css property background-position.

查看更多
做自己的国王
3楼-- · 2019-02-04 18:42

yes , using sprites is good for website performs because every single component on website send different http request .So, when we use sprites images the http request become less & website performance increase.That rule is also apply on css also less css files less http request. you can yourself with the help of safari web inspector.

for more better performance download "yslow"

查看更多
Explosion°爆炸
4楼-- · 2019-02-04 18:42

And with CSS sprites is also possible to make e.g. menu button hover effect without waiting until second image loads. see

查看更多
甜甜的少女心
5楼-- · 2019-02-04 18:48

It has been common for a while to put two images on one sprite sheet, but the tendency has been moving towards combining ALL of your background images on the same sprite sheet to load just one file for all of them. There's a rather good tutorial here.

查看更多
祖国的老花朵
6楼-- · 2019-02-04 18:49

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.

查看更多
爷的心禁止访问
7楼-- · 2019-02-04 18:51

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

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

查看更多
登录 后发表回答