Get icon from single png image

不羁岁月 提交于 2019-12-07 06:50:41

问题


I have seen this so many times until now, but I never used myself. Can somebody explain how you can get specific icon picture from this single png image, for example the icons i selected with red ... using css


回答1:


That is called CSS sprites. It is used to cut down the http requests. Basically all icons are placed on a single canvas and are used as background-image property and later they are mapped using CSS background-position property, so for example

.icon1 {
   background-image: url('YOUR_URL_HERE');
   background-position: 10px 10px; /* X and Y */
   height: 30px;
   width: 30px;
}

Demo

So inshort just define a fix height/width to your element, and than map the canvas using background-position property. Hence, if you have 100 small icon images on a page, it will make 100 requests to the server, thus to increase the performance, CSS Sprites are used.




回答2:


  1. Set a fixed (in pixels) height and width on an element
  2. Set the image as the background-image
  3. Adjust background-position so the part of the image you want to be visible is in view



回答3:


Using background shorthand for the positioning of image.

div {
    background:url(http://i.stack.imgur.com/mUhg1.png) -82px -104px;
    width:27px;
    height:27px;
}

http://jsfiddle.net/T2EtY/1/



来源:https://stackoverflow.com/questions/18180761/get-icon-from-single-png-image

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