【发布时间】:2012-03-26 14:46:52
【问题描述】:
我有大约 10 个按钮,每个按钮都有图像文本和悬停状态。 我想要做的是使用背景位置、宽度和高度来只显示背景图像精灵的一部分,并使用悬停背景位置来显示悬停样式。 我还将在元素上使用图像替换类,以便它保持可访问性和可索引性。
所以(测量是随机的):
[CSS]
.menu{background-image:url(path/to/sprite.png);}
.button-1{width:200px;height:30px;background-position:0 0;}
.button-1:hover{background-position:0 -30px;}
.button-2{width:250px;height:30px;background-position:100px 0;}
.button-2:hover{background-position:100px -30px;}
/* Image Replacement Class (H5BP, @necolas && BEM) */
.ir{border:0;font:0/0 a;text-shadow:none;color:transparent;background-color:transparent;}
[HTML]
<a href="someLink.html" class="menu button-1 ir">Button 1</a>
<a href="someOtherLink.html" class="menu button-2 ir">Button 2</a>
我想知道的是,这是否是一种好方法,或者应该以不同的方式进行,例如:
<a href="someLink.html"><img src="image.png" width="200" height="30" alt="Button 1"/></a>
然后用 JavaScript 交换悬停时的图像。
两者在可访问性和机器人方面有什么区别吗?
谢谢。
【问题讨论】:
-
我觉得不错。我唯一要做的就是重新审视设计,看看您是否可以仅仅为了可维护性而将文本保留为 HTML。
-
嘿。文本保持为 HTML 是什么意思?
-
我的意思是尽量不要用图像替换文本,即使它们是使用 CSS 应用的背景图像。我不知道您更换图像的原因 - 我只是不惜一切代价避免它。维护是一种痛苦。
标签: css image-replacement