CSS在其他包含的元素之上显示div背景图像

Int*_*els 17 html css z-index background-image

不确定这是否可能以及如何,但我尝试在我的代码中使用元素的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>
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

Jak*_*ris 19

我会把绝对定位,z-index: 100;跨度(或跨度)与background: url("myImageWithRoundedCorners.jpg");在其上设置里面#mainWrapperDivWithBGImage.

  • @user如果你这样做,一定要把包装器div设置为`position:relative; ` (2认同)