How to position css sprite map

青灯夜游
Release: 2023-01-07 11:42:13
Original
7767 people have browsed it

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.

How to position css sprite map

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

How to position css sprite map

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>
Copy after login

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}
Copy after login

Rendering:

How to position css sprite map

##css sprites key code and explanation

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}
Copy after login
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.

Then set specific values ​​for icon background positioning for different span classes respectively

  • ##ul.Sprites li span.a1{ background-position: -62px -32px }

    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

  • ul.Sprites li span.a2{ background-position: -86px -32px}

    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

  • ul.Sprites li span.a3{ background-position: -110px -32px}

    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

  • ##ul.Sprites li span.a4{ background-position: -133px -32px}
  • Set the background image as the corresponding box object "Drag" 133px to the left after the background and "drag" 32px upward to start displaying this background icon

  • ul.Sprites li span.a5{ background-position: -158px -32px}
  • Set the background image as the background of the corresponding box object and then "drag" 158px to the left and "drag" 32px upward to start displaying this background icon

    Key:
The background background-position has two values. The first one represents the distance value to the left (can be positive or negative), and the second value represents the distance value to the top (can be positive or negative)

Background background-position has two values ​​that can be positive or negative. When it is a positive number, it means that the background image is used as the background image of the object box. When the background image is moved to the left and the distance is considered, the background image starts to be displayed; when it is a negative number, it means The background image is used as the background image of the box object. Drag the background image as far as it goes beyond the left side of the box object, and drag it as far as it goes beyond the top of the box object to start displaying the background image.

(Learning video sharing:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template