CSS高级技巧:图片替换

2019-03-15 00:22:17王旭

Lindsay将文字设置为1px, 并将文字颜色设置成替换用图片背景的颜色.
HTML

<h3 id="header">
Revised Image Replacement
</h3>

CSS代码大致如下:

#header {
background: url(sample-opaque.gif) no-repeat;
width: 329px;
height: 25px;
font-size: 1px;
color: #xxxxxx;
}

缺点:
只能用在纯色背景的替换用图片的情况下, 而且在关闭图像 开启CSS的情况下同样无法获取信息. Shea 加强版
Shea为容器加上了title属性, title中包含将被隐藏的文字, 通过这种方法来达到一种鼠标激活工具提示的一种效果来让用户获取信息.
HTML

<h3 id="header" title="Revised Image Replacement">
<span></span>Revised Image Replacement
</h3>

CSS

#header {width: 329px;height: 25px;position: relative;}
#header span {background: url(sample-opaque.gif) no-repeat;position: absolute;width: 100%;height: 100%;}

缺点:
多余的空白无语义span标签...
以上几乎是现存所有的图像替换方法, 各有优缺点, 按照自己的情况选用吧.
下一篇CSS教程 文章:CSS高级技巧:滑动门