【发布时间】:2014-10-27 21:25:24
【问题描述】:
我希望我的一个特定的div 有自己的特定背景,宽度为 100%,所以它覆盖了整个div,为什么不显示背景图像。
See here
【问题讨论】:
标签: html image background
我希望我的一个特定的div 有自己的特定背景,宽度为 100%,所以它覆盖了整个div,为什么不显示背景图像。
See here
【问题讨论】:
标签: html image background
您需要添加 height 和 width 属性才能使用背景图像。
#counter1 {
background-image: url(http://i.imgur.com/7NMEPIo.jpg);
width:600px;height:340px;
}
here is a fiddle with a working version
您应该重新考虑使用其他字体颜色 ;-)
【讨论】:
带有id=counter1 的元素没有内容*(float 将元素移出文档流),div 的高度为 0,这就是您看不到背景图像的原因。
#counter1 {overflow: hidden}
http://jsfiddle.net/huspk2j1/9/
*)
检查这个小提琴http://jsfiddle.net/huspk2j1/11/,我已将border: 1px solid blue 添加到#counter1 以显示它是空的。
【讨论】:
基本上是因为浮动计数器不会导致其父级正确计算高度。您可以使用 overflow: hidden 属性解决此问题。
#counter1 {
background-image:url(http://i.imgur.com/7NMEPIo.jpg);
overflow: hidden;
}
【讨论】:
为了解决这个问题,您只需在容器末尾添加一个带有 clear: both 的元素。
在此处查看更新的代码:http://jsfiddle.net/aksruLLh。
【讨论】:
overflow 方法更干净。
添加
#counter1:after {
clear: both;
content: "";
display: table;
}
到你的CSS。
【讨论】: