CSS 内联图像的精灵

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CSS 内联图像的精灵相关的知识,希望对你有一定的参考价值。

// Generically

<img src="images/arrow-sprite.png" alt="arrow" class="clip pos-1" />

.clip               { position: absolute; top: 0; left: 0; }
		
.pos-1              { clip:rect(0 48px 48px 0); }
.pos-2              { clip:rect(0 96px 48px 48px); left: -48px; }
.pos-3              { clip:rect(48px 48px 96px 0); top: -48px; }
.pos-4              { clip:rect(48px 96px 96px 48px); top: -48px; left: -48px; }


// Within Flow
// Use the same CSS as above, only wrap the image in a div with relative positioning and a matching height and width to the area you want to show.

<div class="clipwrapper">
   <img src="images/arrow-sprite.png" alt="arrow" class="clip pos-1" />
</div>

.clipwrapper        { position: relative; height: 48px; width: 48px; }


/*
Points to remember

* The element needs to be absolutely positioned.
* To use it on an element and keep document flow, using a wrapping div with a relative position.
* The clipped away areas are transparent but still occupy space.
* For sprites other than the top/left sprite, you'll need to nudge the position back the same top/left distance you clipped away. 
*/

以上是关于CSS 内联图像的精灵的主要内容,如果未能解决你的问题,请参考以下文章

CSS精灵图像+背景重复其中的一部分

CSS:使用带有 css 伪类的图像精灵 :before 和 :after

使用 CSS 精灵作为图像类型的输入

CSS3 - 在精灵图像的“背景位置”之间淡入淡出

CSS 剪辑 - 精灵图像

用作背景图像时如何缩放 CSS 精灵?