[英]How can I use a background image instead of the IMG tag in html?
I have the following HTML with many of these kinds of links: 我有以下带有许多此类链接的HTML:
<a class="button" id="menu">
<img width="16" height="16" src="/Images/control-double.png">
</a>
I created a sprite for these with all the images combined using 我为这些图像创建了一个Sprite,并使用
But when I look at this it seems like it's just for background as it gives an example: 但是当我看这个的时候,它似乎只是作为背景,因为它给出了一个例子:
.sprite-control-double{ background-position: 0 -396px; width: 16px; height: 16px; }
#container li {
background: url(csg-4febd28fe3aa3.png) no-repeat top left;
}
How can I use a sprite with positioning for an image instead of a single image? 如何使用带有图片定位而不是单个图片定位的精灵?
If you use the "sprite" technique, then you don't need IMG
elements within your anchors: 如果使用“ sprite”技术,则锚点中不需要
IMG
元素:
a.button {
background-color: transparent;
background-image: url(/Images/control-double.png);
background-position: 0 -396px;
background-repeat: no-repeat;
height: 16px;
width: 16px;
}
I added the background-color
style because in some browsers the rendering engine will not show a background image if there is no background color. 我添加了
background-color
样式,因为在某些浏览器中,如果没有背景颜色,渲染引擎将不会显示背景图像。
Yes you can. 是的你可以。 That's how sprites work, they're applied as the background for an element with its overflow set to hidden.
这就是精灵的工作方式,它们被用作元素的背景,且其溢出设置为隐藏。
You should remove the <img>
tag entirely and rely on the background of the <a>
tag. 您应该完全删除
<img>
标记,并依赖<a>
标记的背景。 The purpose of the sprite is to replace the need for non-semantic <img>
tags. Sprite的目的是取代对非语义
<img>
标签的需求。
从spritegens帮助页面链接的文章中对此进行了很好的介绍: http : //www.alistapart.com/articles/sprites
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.