Application of Sprite Pictures
Jul 11, 2016 am 08:43 AMOn the weekend, I studied the implementation of sprite graphics. Let’s first learn about sprite pictures. Sprite pictures mean css sprites, and they are also called css sprites. That is, merge the small icon and background image into one image, and then use the background positioning of CSS to display the part of the image that needs to be displayed.
1. Benefits and advantages:
Effectively reduce the number of http requests on the website and speed up the display of images.
2. Conditions:
- Static pictures, pictures do not change according to changes in user information.
- Small pictures with relatively small capacity (2~3k).
- The image loading capacity is relatively large.
Note: It is not recommended to use Sprite images for large pictures. If the picture is so big, it will be invincible after disassembly, and the loading will be slow, which is not worth the loss! ! !
3. Principle : Use css3's background-position to control a layer Display the size of the area and slide the background image through a window .
To put it simply, use this attribute to set the starting position where the background image needs to be displayed, and control the display range of the background image through labels .
4. Let’s first understand the background-position attribute,
Based on the picture:
- Take the upper left corner as (0,0) coordinates
- x and y are both negative values
In summary, the value of background-position is the starting coordinate of the background image display, in the form of background-position: 0, 0;
5. Jigsaw puzzle: You can use PS or many online sprite image generation tools on the Internet to generate sprite images.
I used an online puzzle tool to manually drag and drop the comments, which is a bit disgusting~~haha
In fact, the best puzzle is to control the margins of each icon and the amount of white space around the icon. If the coordinates of the background-position of the css are written regularly, it will be much better (however, this I dragged it manually, and the position is very irregular, so the coordinate values ??below are all taken after debugging)
After talking about this, we can get to the main topic, html and css code. The following code is to imitate a menu~~~
6. Code:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="content"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 2</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">ul </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="content"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 3</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-1"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>女裝/男裝/內(nèi)衣<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 7</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-2"</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;"> 9</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>鞋靴/箱包/配件<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">10</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">11</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-3"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">12</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">13</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>童裝玩具/孕產(chǎn)/用品<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">14</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">15</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-4"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">16</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">17</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>家電/數(shù)碼/手機(jī)<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">18</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">19</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-5"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">20</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">21</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>美妝/洗護(hù)/保健品<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">22</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">23</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-6"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">24</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">25</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>珠寶/眼鏡/手表<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">26</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">27</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-7"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">28</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">29</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>運(yùn)動(dòng)/戶外/樂(lè)器<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">30</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">31</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-8"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">32</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">33</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>游戲/動(dòng)漫/影視<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">34</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">35</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-9"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">36</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">37</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>美食/生鮮/零食<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">38</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">39</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-10"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">40</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">41</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>鮮花/寵物/農(nóng)資<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">42</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">43</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">li </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="cat-11"</span><span style="color: #0000ff;">></span> <span style="color: #008080;">44</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></</span><span style="color: #800000;">i</span><span style="color: #0000ff;">></span> <span style="color: #008080;">45</span> <span style="color: #0000ff;"><</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>房產(chǎn)/裝修/建材<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span> <span style="color: #008080;">46</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">li</span><span style="color: #0000ff;">></span> <span style="color: #008080;">47</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">ul</span><span style="color: #0000ff;">></span> <span style="color: #008080;">48</span> <span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 1</span> <span style="color: #800000;"> <style type="text/css" > </span><span style="color: #008080;"> 2</span> <span style="color: #800000;"> #content</span>{ <span style="color: #008080;"> 3</span> <span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 180px</span>; <span style="color: #008080;"> 4</span> <span style="color: #ff0000;"> background</span>:<span style="color: #0000ff;"> #f8f8f8</span>; <span style="color: #008080;"> 5</span> <span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> 1px solid #bbb</span>; <span style="color: #008080;"> 6</span> } <span style="color: #008080;"> 7</span> <span style="color: #800000;"> h3</span>{ <span style="color: #008080;"> 8</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 0</span>; <span style="color: #008080;"> 9</span> <span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 0</span>; <span style="color: #008080;">10</span> } <span style="color: #008080;">11</span> <span style="color: #800000;"> ul</span>{ <span style="color: #008080;">12</span> <span style="color: #ff0000;"> list-style</span>:<span style="color: #0000ff;"> none</span>; <span style="color: #008080;">13</span> <span style="color: #ff0000;"> padding</span>:<span style="color: #0000ff;"> 0</span>; <span style="color: #008080;">14</span> } <span style="color: #008080;">15</span> <span style="color: #800000;"> li h3</span>{ <span style="color: #008080;">16</span> <span style="color: #ff0000;"> font-size</span>:<span style="color: #0000ff;"> 14px</span>; <span style="color: #008080;">17</span> <span style="color: #ff0000;"> font-weight</span>:<span style="color: #0000ff;"> 400</span>; <span style="color: #008080;">18</span> } <span style="color: #008080;">19</span> <span style="color: #800000;"> li</span>{ <span style="color: #008080;">20</span> <span style="color: #ff0000;"> margin</span>:<span style="color: #0000ff;"> 3px 10px 0 0</span>; <span style="color: #008080;">21</span> <span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> block</span>; <span style="color: #008080;">22</span> <span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 31px</span>; <span style="color: #008080;">23</span> <span style="color: #ff0000;"> line-height</span>:<span style="color: #0000ff;"> 31px</span>; <span style="color: #008080;">24</span> <span style="color: #ff0000;"> overflow</span>:<span style="color: #0000ff;"> hidden</span>; <span style="color: #008080;">25</span> <span style="color: #ff0000;"> border-bottom</span>:<span style="color: #0000ff;"> 1px solid #dedede</span>; <span style="color: #008080;">26</span> <span style="color: #008080;">27</span> } <span style="color: #008080;">28</span> <span style="color: #800000;"> li i</span>{ <span style="color: #008080;">29</span> <span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> url(sprite.png)</span>; <span style="color: #008080;">30</span> <span style="color: #ff0000;">display</span>:<span style="color: #0000ff;"> inline</span>; <span style="color: #008080;">31</span> <span style="color: #ff0000;">width</span>:<span style="color: #0000ff;"> 40px</span>; <span style="color: #008080;">32</span> <span style="color: #ff0000;">height</span>:<span style="color: #0000ff;"> 28px</span>; <span style="color: #008080;">33</span> <span style="color: #ff0000;">float</span>:<span style="color: #0000ff;"> left</span>; <span style="color: #008080;">34</span> } <span style="color: #008080;">35</span> <span style="color: #800000;"> .cat-1 i</span>{ <span style="color: #008080;">36</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -7px -5px</span>; <span style="color: #008080;">37</span> } <span style="color: #008080;">38</span> <span style="color: #800000;"> .cat-2 i</span>{ <span style="color: #008080;">39</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -2px -35px</span>; <span style="color: #008080;">40</span> } <span style="color: #008080;">41</span> <span style="color: #800000;"> .cat-3 i</span>{ <span style="color: #008080;">42</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -7px -65px</span>; <span style="color: #008080;">43</span> } <span style="color: #008080;">44</span> <span style="color: #800000;"> .cat-4 i</span>{ <span style="color: #008080;">45</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -7px -105px</span>; <span style="color: #008080;">46</span> } <span style="color: #008080;">47</span> <span style="color: #800000;"> .cat-5 i</span>{ <span style="color: #008080;">48</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -7px -129px</span>; <span style="color: #008080;">49</span> } <span style="color: #008080;">50</span> <span style="color: #800000;"> .cat-6 i</span>{ <span style="color: #008080;">51</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -7px -151px</span>; <span style="color: #008080;">52</span> } <span style="color: #008080;">53</span> <span style="color: #800000;"> .cat-7 i</span>{ <span style="color: #008080;">54</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;">-60px -4px</span>; <span style="color: #008080;">55</span> } <span style="color: #008080;">56</span> <span style="color: #800000;"> .cat-8 i</span>{ <span style="color: #008080;">57</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;">-56px -33px</span>; <span style="color: #008080;">58</span> } <span style="color: #008080;">59</span> <span style="color: #800000;"> .cat-9 i</span>{ <span style="color: #008080;">60</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -56px -66px</span>; <span style="color: #008080;">61</span> } <span style="color: #008080;">62</span> <span style="color: #800000;"> .cat-10 i</span>{ <span style="color: #008080;">63</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;">-60px -103px</span>; <span style="color: #008080;">64</span> } <span style="color: #008080;">65</span> <span style="color: #800000;"> .cat-11 i</span>{ <span style="color: #008080;">66</span> <span style="color: #ff0000;"> background-position</span>:<span style="color: #0000ff;"> -51px -128px</span>; <span style="color: #008080;">67</span> } <span style="color: #008080;">68</span> <span style="color: #008080;">69</span> <span style="color: #800000;"> </style></span>
7. The rendering is as follows:
That’s it for the application of sprites~~Please point out if there are any mistakes~~Hahaha

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

ARIA's role attribute is used to define the role of web elements and improve accessibility. 1. Role attribute helps assistive technology to understand the functions of elements, such as buttons, navigation, etc. 2. Use role attributes to assign specific roles to non-semantic HTML elements. 3. The role attribute should be consistent with the element behavior and be verified by the accessibility tool test.

How to create a website layout? 1. Use HTML tags to define the content structure, such as, ,. 2. Control styles and positions through CSS, using box model, float or Flexbox layout. 3. Optimize performance, reduce HTTP requests, use cache and optimize images, and ensure responsive design.

Improve the readability and maintainability of HTML code can be achieved through the following steps: 1. Use semantic tags, such as, etc. to make the code structure clear and improve SEO effect; 2. Keep the code formatted and use consistent indentation and spaces; 3. Add appropriate comments to explain the code intention; 4. Avoid excessive nesting and simplify the structure; 5. Use external style sheets and scripts to keep the HTML concise.

The key to keep up with HTML standards and best practices is to do it intentionally rather than follow it blindly. First, follow the summary or update logs of official sources such as WHATWG and W3C, understand new tags (such as) and attributes, and use them as references to solve difficult problems; second, subscribe to trusted web development newsletters and blogs, spend 10-15 minutes a week to browse updates, focus on actual use cases rather than just collecting articles; second, use developer tools and linters such as HTMLHint to optimize the code structure through instant feedback; finally, interact with the developer community, share experiences and learn other people's practical skills, so as to continuously improve HTML skills.

The reason for using tags is to improve the semantic structure and accessibility of web pages, make it easier for screen readers and search engines to understand page content, and allow users to quickly jump to core content. Here are the key points: 1. Each page should contain only one element; 2. It should not include content that is repeated across pages (such as sidebars or footers); 3. It can be used in conjunction with ARIA properties to enhance accessibility. Usually located after and before, it is used to wrap unique page content, such as articles, forms or product details, and should be avoided in, or in; to improve accessibility, aria-labeledby or aria-label can be used to clearly identify parts.

To create a basic HTML document, you first need to understand its basic structure and write code in a standard format. 1. Use the declaration document type at the beginning; 2. Use the tag to wrap the entire content; 3. Include and two main parts in it, which are used to store metadata such as titles, style sheet links, etc., and include user-visible content such as titles, paragraphs, pictures and links; 4. Save the file in .html format and open the viewing effect in the browser; 5. Then you can gradually add more elements to enrich the page content. Follow these steps to quickly build a basic web page.

To create an HTML checkbox, use the type attribute to set the element of the checkbox. 1. The basic structure includes id, name and label tags to ensure that clicking text can switch options; 2. Multiple related check boxes should use the same name but different values, and wrap them with fieldset to improve accessibility; 3. Hide native controls when customizing styles and use CSS to design alternative elements while maintaining the complete functions; 4. Ensure availability, pair labels, support keyboard navigation, and avoid relying on only visual prompts. The above steps can help developers correctly implement checkbox components that have both functional and aesthetics.

HTMLtagsareessentialforstructuringwebpages.Theydefinecontentandlayoutusinganglebrackets,ofteninpairslikeand,withsomebeingself-closinglike.HTMLtagsarecrucialforcreatingstructured,accessible,andSEO-friendlywebpages.
