【发布时间】:2011-06-10 14:50:56
【问题描述】:
我在几个span 元素上设置了background-image,但它们没有出现,我想是因为我的height 和width 设置被忽略了。
HTML 源代码:
<div class="textwidget">
<a href="#" title="Start here"><span id="starthere" class="sidebar-poster"></span></a>
<a href="#" title="Primary documents"><span id="#primarydocs" class="sidebar-poster"></span></a>
<a href="#" title="Donate"><span id="donate" class="sidebar-poster"></span></a>
</div>
CSS:
span.sidebar-poster {
margin-bottom: 10px;
background-repeat: no-repeat;
width: 160px;
}
span#starthere {
background-image: url(/betatesting/wp-content/themes/dynamik/css/images/brunelwantsyou180.jpg);
height: 285px;
}
span#starthere:hover {
background-image: url(/betatesting/wp-content/themes/dynamik/css/images/brunelwantsyou_hover.jpg);
}
span#primarydocs {
background-image: url(/betatesting/wp-content/themes/dynamik/css/images/brunelwantsyou180.jpg);
height: 285px;
}
span#primarydocs:hover {
background-image: url(/betatesting/wp-content/themes/dynamik/css/images/brunelwantsyou_hover.jpg);
}
span#donate {
background-image: url(/betatesting/wp-content/themes/dynamik/css/images/donatebutton.jpg);
height: 285px;
}
span#donate:hover {
background-image: url(/betatesting/wp-content/themes/dynamik/css/images/donateposter_hover.jpg);
}
实际上没有任何背景图像可见。
在 Chrome 开发者工具中,在 Computed Style 下,这两个 span 确实有背景图片。如果我复制并粘贴此图像的 URL,我会看到该图像。然而实际上并没有渲染。
[更新 - 这部分已解决,谢谢] 在 Chrome 开发人员工具中,在匹配规则下,实际上只有 #starthere 和 #donate 跨度在使用 background-image 属性。 #primarydocs 跨度不是。为什么不呢?
【问题讨论】:
-
所以 span 元素的高度为 0px,宽度为 0px,也许就是这样。为什么?为什么他们没有我在 #id 中给他们的高度和 sidebar-poster 类中的宽度?
-
span id="#primarydocs"应该是span id="primarydocs"。 -
是的。谢谢,不知道我怎么没发现!现在只需弄清楚宽度即可。
-
更广泛的问题:如果没有在 SO 上发帖,我怎么能发现这一点?对于 CSS 错误,是否有等效的 Firebug? Firebug 给出了一个神秘的错误“属性选择器中的意外标记!”,但我什至不确定这与这个错误有关。
-
@AP257:firebug 列出了与元素匹配的所有规则,因此,如果您没有看到您希望出现的规则,您通常拼错了 class/id 或在选择器规则。 (至少,我就是这样做的。)