Single image file to store all the little images on a page

允我心安 提交于 2019-11-30 02:24:20

Basically you use your single image as the background image, but move it off (to the left and up) by the offset of the image you want to display. E.g. to display the hammer icon:

.hammer
{
  background: transparent url(myIcons.jpg) -50px -50px no-repeat;
}

But as far as I know, you have to make sure that the element that's using the background image has the correct size (e.g. 32x32 px).

A search for CSS Sprites will give you more information.

It's called css sprites.

I's basically an old trick used in games programming where you load a single bitmap containing all the "states" of some item you need to draw, the advantage is that this way the image get's preloaded and there's no delay when you need to actually use it, in the case of css, it's normally implemented by using the image as background to the element, and applying different offsets and bounds on :hover, :active and "normal" classes.

There's more info in the stackoverflow Blog

Here's a nice generator: http://www.csssprites.com/

You know the answer ... ask google in this case look at the source of the google search results page, with a tool like firebug and you will find


.w10 
 background-position:-152px 0;
}
.w10, .w11, .w20, .w21, .w24, .wci, .wpb 
 background:transparent url(/images/nav_logo4.png) no-repeat scroll 0 0;
 border:0 none;
 cursor:pointer;
 height:16px;
 margin-left:8px;
 vertical-align:bottom;
 width:16px;
}

So all w10, w11, w20 etc share the same image (nav_logo4.png) all have fixed hight and width. and all specify (different) backgroup-position's.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!