为精灵使用背景图像时,您会使您的网站无法访问。使用高对比度模式的低视力用户看不到背景图像。以下来自雅虎的无障碍博客,是无障碍精灵的一个很好的例子。
的HTML:
<div role="toolbar" class="toolbar">
<button type="button" class="prnt">Print</button>
<button type="button" class="find">Find</button>
<button type="button" class="save">Save</button>
<button type="button" class="sets">Settings</button>
<button type="button" class="info">Info</button>
</div>
CSS:
.toolbar {
border: 1px solid #666;
background: #ddd;
padding: 3px;
font-size: 0;
/* Eliminates white space below and between buttons */
}
.toolbar button {
border: 1px solid #333;
background: #bbb;
padding: 0;
margin: 0 3px 0 0;
width: 36px;
height: 36px;
overflow: hidden;
vertical-align: top; /* Needed for Firefox to ensure buttons are properly aligned inside the toolbar. */
}
/* Remove the extra padding and border given to buttons by
default in Firefox to ensure correct alignment of the img. */
.toolbar button::-moz-focus-inner {
border: 0;
padding: 0;
}
/* The above rule removes the focus outline in Firefox. This rule restores it. */
.toolbar button:focus {
outline: dotted 1px #000;
}
/* Hide the text label by inserting an image before it. */
.toolbar button:before {
display: inline-block;
content: url('http://findicons.com/files/icons/2340/preview/toolbar_icon_set_full.png');
}
.toolbar button {
*background: url('http://findicons.com/files/icons/2340/preview/toolbar_icon_set_full.png') no-repeat;
*text-indent: 36px;
}
.toolbar .prnt {
*background-position: -38px -74px;
}
.toolbar .prnt:before {
margin: -73px 0 0 -37px;
}
.toolbar .find {
*background-position: -182px -146px;
}
.toolbar .find:before {
margin: -145px 0 0 -181px;
}
.toolbar .save {
*background-position: -146px -74px;
}
.toolbar .save:before {
margin: -73px 0 0 -145px;
}
.toolbar .sets {
*background-position: -74px -110px;
}
.toolbar .sets:before {
margin: -109px 0 0 -73px;
}
.toolbar .info {
*background-position: -146px -146px;
}
.toolbar .info:before {
margin: -145px 0 0 -145px;
}
对于悬停效果,您可以使用类和伪选择器:hover
将边距调整为适当的值。