Home > Article > Web Front-end > How to position css sprite map
The sprite map uses the combination of background-image, background-repeat, and background-position to position the background. The background-position attribute can use numbers to accurately locate the background image in the layout box object position.
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
css sprite map actually integrates multiple images into one image, and then uses CSS background positioning technology to lay out the background of the web page.
The benefits of this are also obvious, because if there are many pictures, it will increase http requests, which will undoubtedly reduce the performance of the website, especially for websites with a lot of pictures. If you can use css sprites to reduce the number of pictures, What will result is an increase in speed.
Sprite image generation background:
1. Each image on the web page needs to send a request to the server before it can be displayed to the user.
2. When there are too many images on the web page, the server will frequently accept and send requests, greatly reducing the page loading speed. In order to effectively reduce the number of times the server accepts and sends requests and improve the loading speed of the page, CSS Sprites technology appeared
Definition of sprite map:
1. Many pictures are merged into one picture, and the icons are displayed in each box through background positioning.
2. The background positioning method is mainly by controlling the values of the x and y axes.
Use the combination of "background-image", "background-repeat" and "background-position" to position the background. Background-position can use numbers to accurately locate the background image in the layout box object position.
Tips for using sprite charts:
1. General situation: Just use a box, and the background image inside is a sprite chart. Pay attention to the x-axis and y-axis when using it.
2. Special situation: There is a small icon in the box. At this time, the small icon is wrapped with a label (i label or span). After setting the positioning of the small icon (automatically converted into an inline block), define the width and height. This width and height is the width and height of the small picture in the sprite chart, and then pay attention to the values of the x-axis and y-axis.
Example:
Sprite
HTML code
<ul class="Sprites"> <li><span class="a1"></span><a href="#">WORD文章标题</a></li> <li><span class="a2"></span><a href="#">PPT内容标题</a></li> <li><span class="a3"></span><a href="#">Excel内容标题</a></li> <li><span class="a4"></span><a href="#">PDF内容标题</a></li> <li><span class="a5"></span><a href="#">文本文档标题</a></li> </ul>
css code
ul.Sprites{ margin:0 auto; border:1px solid #F00; width:300px; padding:10px;} ul.Sprites li{ height:24px; font-size:14px;line-height:24px; text-align:left; overflow:hidden} ul.Sprites li span{ float:left; width:17px;padding-top:5px;height:17px; overflow:hidden;background-image: url(ico.png);background-repeat:no-repeat;} ul.Sprites li a{ padding-left:5px} ul.Sprites li span.a1{ background-position: -62px -32px} ul.Sprites li span.a2{ background-position: -86px -32px} ul.Sprites li span.a3{ background-position: -110px -32px} ul.Sprites li span.a4{ background-position: -133px -32px} ul.Sprites li span.a5{ background-position: -158px -32px}
Rendering:
##css sprites key code and explanationul.Sprites li span.a1{ background-position: -62px -32px} ul.Sprites li span.a2{ background-position: -86px -32px} ul.Sprites li span.a3{ background-position: -110px -32px} ul.Sprites li span.a4{ background-position: -133px -32px} ul.Sprites li span.a5{ background-position: -158px -32px}First introduce the background to ul.Sprites li span
ul.Sprites li span{ background-image: url(ico.png);background-repeat:no-repeat;} Set the css background image for span.
Set the background image as the background of the corresponding box object and "drag" 62px to the left and "drag" 32px upward to start displaying this background icon
Set the background image as the background of the corresponding box object and then "drag" 86px to the left and "drag" 32px upward to start displaying this background icon
Set the background image as the background of the corresponding box object and "drag" 110px to the left and "drag upward" Move "32px to start displaying this background icon
css video tutorial
)The above is the detailed content of How to position css sprite map. For more information, please follow other related articles on the PHP Chinese website!