简体   繁体   English

如何在HTML中使用背景图片代替IMG标签?

[英]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,并使用

spritegen 精灵

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.

相关问题 使用 HTML<img> 标记为背景图像而不是 CSS background-image 属性? - Use the HTML <img> tag as a background image instead of the CSS background-image property? 如何使用 Javascript 而不是使用背景属性<img>标签? - How can I use the background property using Javascript instead of using <img> tag? html<img> 标签而不是 css 背景图像 - html <img> tag instead of css background-image 如何在html img标签中使用Java字符串图像对象-有可能吗? - How can I use a java string image object in a html img tag - Is it possible? 我们如何使用<picture>标签而不是“背景图像”?</picture> - How can we use <picture> tag instead of “background-image”? 如何模糊图像的一部分 <img> 使用CSS标记,我没有使用背景图片的选项 - How to blur part of an image <img> tag using css, I don't have an option to use background image 如何使用img代替背景图片来编辑内容 - How to use an img instead of background image for editable content 在html image tag中获取图像的路径。 <img> 标签? - Getting Path of image in html image tag.so how can i get URL of image from <img> tag? 错误:不要使用<img> . 请改用“下一个/图像”中的图像。 - Next.js 使用 html 标签<img> (带有样式组件) - Error: Do not use <img>. Use Image from 'next/image' instead. - Next.js using html tag <img /> ( with styled components ) 如何使用html文件作为图像文件的来源 <img src=“”> 标签 - How to use a html file as a source of image file in <img src=“”> tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM