如何覆盖CSS中img标签的全局样式
我有一个模板,它设置全局 img 标签属性,如下所示:
#content img {
background: none repeat scroll 0 0 #FFFFFF;
border: 1px solid #CFCFCF;
margin-bottom: 2px;
padding: 2px;
}
我的页面上有一个列表,其中一个项目需要有一个小的透明图像。上面的样式导致图像获得我不想要的背景和边框。列表 html 是:
<div id="land_items">
<ul>
<li class="trace_item">
<a href="/imglink"><img src="img/popup.png"></a>
</li>
</ul>
</div>
我尝试使用下面的代码覆盖 img 标签,但是 Firebug 继续用“删除线”显示这些规则,指示上面的全局 img 样式优先。我听说这可能是因为 id css 样式覆盖了类样式。我该如何实现这个目标?
li.trace_item img {
background-color: transparent;
border: none;
padding: 0;
margin: 0;
}
I have a template which sets global img tag properties as such:
#content img {
background: none repeat scroll 0 0 #FFFFFF;
border: 1px solid #CFCFCF;
margin-bottom: 2px;
padding: 2px;
}
I have a list on my page and one of the items needs to have a small transparent image. The styling above is causing the image to get a background and borders that I do not want. The list html is:
<div id="land_items">
<ul>
<li class="trace_item">
<a href="/imglink"><img src="img/popup.png"></a>
</li>
</ul>
</div>
I have tried to override the img tag with the code below, but Firebug continues to show these rules with a "strikethrough" indicating the global img styling above is taking precedence. I hear this could be because id css styles override class styles. How do I accomplish this?
li.trace_item img {
background-color: transparent;
border: none;
padding: 0;
margin: 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是因为 CSS 在应用样式时使用 特异性 (它级联)
它应该适合你如果您将其更改为
以下是《星球大战》术语中解释的特殊性:http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
That's because CSS uses specificity when applying the styles (it cascades)
It should work for you if you change it to
Here's specificity explained in Star Wars terms : http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
只需将 #content 添加到您的覆盖 css 选择器中:
请在此处查看演示: http://jsfiddle.net/alp82 /A5rHY/6/
Just prepend #content to your overriding css selector:
See a demo here: http://jsfiddle.net/alp82/A5rHY/6/
你有不同的选择
您将 id 内容转换为类内容,或者将trace_item 类转换为 id 或
您将 !important 添加到您的trace_item 类中或在 .trace_item 前面添加 #content 也可以
you have different options
you convert the id content to a class content or you convert the trace_item class to an id or
you add the !important to your trace_item class or prepend #content to .trace_item also works