gpt4 book ai didi

css - 与背景图片的链接

转载 作者:行者123 更新时间:2023-11-28 11:47:37 24 4
gpt4 key购买 nike

我有大约 10 个按钮,每个按钮都有图像文本和悬停状态。我想要做的是使用 background-position、width 和 height 只显示背景图像 Sprite 的一部分,并使用 hover background-position 来显示悬停样式。我还将在该元素上使用图像替换类,以使其保持可访问性和可索引性。

所以(测量是随机的):

[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 在悬停时交换图像。

两者在可访问性和机器人方面有什么区别吗?

谢谢。

最佳答案

你所做的是正确的。不要忘记向该链接添加 display: block;。您可以做的其他事情是将实际链接文本放在一个跨度中,然后将该跨度绝对定位在屏幕之外。像这样:

<a href="" title="Test link"><span>Home page</span></a>

在你的 CSS 文件中:

a > span {position: absolute; top: -10000px;}

关于css - 与背景图片的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9874355/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com