search
HomeWeb Front-endHTML Tutorialcss sprites_html/css_WEB-ITnose

css sprites是什么呢?

简而言之,它就是将网页中的许多小图片,按照约定好的规则,将这些小图片绘制到一张大图片上,然后利用background中的background-position将需要的图片,扣出来。

可能你会问,这样做是为什么?

其实就是减少http请求,提高网站性能。其实因为每一张图片去服务端下载,都需要一次完整的HTTP请求,当许多图片同时下载时,那么就会发出相应数量的http请求,然而,不同的浏览器处理图片下载的数量是有限制的,一般在1-10范围中,所以,倘若你网站中的图片很多,那么不管你网速有多快,其势必影响网站性能。这就是为什么需要css sprites技术。

好了,我们来一起写个demo,举个例子看看。

倘若我要实现一个控制按钮器,如下图所示:

 

每当我鼠标移到图中的每个控制器按钮时,都会触发一个事件,让其变成红色区域,那么可见,每个按钮都是独立且有功能的,你可能会将每个区域都设置成一个张小图片。

但,那得向后台请求多少次呢!!!

优化网站性能的其中之一,就是减少http请求。

所以这里需要图片合并减少http请求,我们就用上了css sprites了。

首先,我们按照一定的规律,将需要的所有小图片合并成一张大图,制作出我们需要的图片。

下图为鼠标未移到每个按钮上时的状态:

下图为鼠标移到每个按钮时的状态:

然后,我们再将每个按钮区域的大小设置为宽31px,高29px以及背景图片定义为正常情况下的图片,及上述图一。

你可能会问,为什么是宽31高29?

因为每张小图片,即每个按钮的宽高是这么多嘛。

接下来,我们就利用background-position属性来将我们需要的部分图片,扣下来,如我需要,那么我就将background-position设置为0 0,就ok了。

最后,当鼠标移到相应按钮时,其变成浅红色状态的图案时,也是如此,利用 background加载大图,background-position扣下需要的小图片。

实现后的详细代码,见下

<!DOCTYPE html>    <head>        <title>js_test</title>        <style type="text/css">            .ptz {                width:31px;                height:29px;                margin-right:1px;                margin-bottom:6px;                cursor:pointer;                float:left;                background:url(ptz.gif) no-repeat;            }            .tmain, .mmain, .bmain {                clear:left;            }            .tl {                background-position:0 0;             }            .tm {                background-position:-34px 0;             }            .tr {                background-position:-68px 0;             }            .add {                background-position:-102px 0;             }            .search {                background-position:-136px 0;             }            .subtract {                background-position:-170px 0;            }            .ml {                background-position:0 -32px;            }            .mm {                background-position:-34px -32px;                }            .mr {                background-position:-68px -32px;             }            .focus {                background-position:-136px -32px;             }            .bl {                background-position:0 -64px;            }            .bm {                background-position:-34px -64px;            }            .br {                background-position:-68px -64px;            }            .iris {                background-position:-136px -64px;            }            .slide {                width:192px;                height:18px;                clear:left;                background-position:0 -96px;            }            .slideBtt {                width:14px;                height:16px;                margin-left:22px;                background-position:0 -121px;            }            .indicate {                clear:left;                width:189px;                height:29px;                border:1px solid #C1C0C0;                 border-radius:3px;            }            .outterBtt {                float:left;                border-right:1px solid #C1C0C0;                width:37px;                height:100%;            }            .lastOut {                border:0;            }            .btt1 {                width:14px;                height:14px;                margin:8px 0 0 12px;                background-position:-28px -127px;            }            .btt2 {                width:20px;                height:20px;                background-position:-57px -127px;                margin:7px 0 0 11px;            }            .btt3 {                width:12px;                height:16px;                background-position:-90px -128px;                margin:8px 0 0 10px;            }            .btt4 {                width:20px;                height:18px;                background-position:-118px -128px;                margin:9px 0 0 9px;            }            .btt5 {                width:15px;                height:15px;                background-position:-152px -128px;                margin:8px 0 0 12px;            }            .tmain div:hover,             .mmain div:hover,             .bmain div:hover,             .outterBtt div:hover {                background-image:url(ptzon.gif);              }                    </style>    </head>    <body>                <div class="tmain">            <div class="ptz tl"></div>             <div class="ptz tm"></div>              <div class="ptz tr"></div>              <div class="ptz add"></div>             <div class="ptz search"></div>            <div class="ptz subtract"></div>        </div>                <div class="mmain">            <div class="ptz ml"></div>            <div class="ptz mm"></div>              <div class="ptz mr"></div>              <div class="ptz add"></div>             <div class="ptz focus"></div>            <div class="ptz subtract"></div>        </div>                <div class="bmain">            <div class="ptz bl"></div>            <div class="ptz bm"></div>              <div class="ptz br"></div>              <div class="ptz add"></div>             <div class="ptz iris"></div>            <div class="ptz subtract"></div>        </div>                <div class="ptz slide">            <div class="ptz slideBtt"></div>        </div>                <div class="indicate">            <div class="outterBtt">                <div class="ptz btt1"></div>            </div>            <div class="outterBtt">                <div class="ptz btt2"></div>            </div>            <div class="outterBtt">                <div class="ptz btt3"></div>            </div>            <div class="outterBtt">                <div class="ptz btt4"></div>            </div>            <div class="outterBtt lastOut">                <div class="ptz btt5"></div>            </div>        </div>    </body></html>

 

Statement
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
What is the difference between an HTML tag and an HTML attribute?What is the difference between an HTML tag and an HTML attribute?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The Future of HTML: Evolution and TrendsThe Future of HTML: Evolution and TrendsMay 13, 2025 am 12:01 AM

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor