【发布时间】:2012-10-24 15:39:43
【问题描述】:
不确定这是否可行以及如何实现,但我尝试在我的代码中使用元素的 z-index,但没有成功。
我有一个 div 元素,它在 css 中设置了背景图像。在 div 内部,我还有其他元素,例如 div 和 img。我试图让包含背景图像的主 div 保持在顶部,因为我希望该背景图像显示在其他元素的顶部(此背景图像有一些圆角,我想在包含的图像顶部显示在这个 div 中)。
示例 HTML:
<div id="mainWrapperDivWithBGImage">
<div id="anotherDiv">
<!-- show this img behind the background image
of the #mainWrapperDivWithBGImage div -->
<img src="myLargeImage.jpg" />
</div>
</div>
示例 CSS:
/* How can I make the bg image of this div show on top of other elements contained
Is this even possible? */
#mainWrapperDivWithBGImage {
background: url("myImageWithRoundedCorners.jpg") no-repeat scroll 0 0 transparent;
height: 248px;
margin: 0;
overflow: hidden;
padding: 3px 0 0 3px;
width: 996px;
}
【问题讨论】:
-
如果主 div 的背景图片显示在顶部,它会隐藏它下面的内部元素,子元素将无用。你确定吗?
-
说得好!我想这行不通,至少按照我的方式嵌套......
-
@Cyber 听起来背景图像没有占据背景 div 的整个填充。只是一小部分。
标签: html css z-index background-image