html - 如何将背景图片放在div后面
问题描述
我试图将我的背景放在包含 jumbotron 的 div 后面,但 div 一直保持在背景图像下方,而不是出现在它上面。我该如何解决?
PS:出于某些原因,我不想将背景图像放在我的 CSS 文件中,所以我希望图像 src 只在我的 HTML 中。非常感谢!
这是我的代码:
<div class="container-fluid" >
<img src='U_Thant_PIC_3.jpg'
width='1400px' height='800px'/>
<div class="jumbotron jumbotron-fluid">
<center>
<h2 class="a">Cats are cool</h2>
</center>
</div>
</div>
解决方案
为了实现这一点,您需要告诉浏览器将img
元素定位在您的 child 后面div
。为此,您可以使用该position
属性,img
具有较低的z-index
.
z-index
只要您position
在元素上有属性,这确实有效:
div.container-fluid{position:relative;}
div.container-fluid img{position:absolute;top:0;left:0;z-index:1}
div.container-fluid div.jumbotron{position:relative;z-index:5;color:white;}
<div class="container-fluid" >
<img src='https://www.w3schools.com/css/img_lights.jpg'
width='1400px' height='800px'/>
<div class="jumbotron jumbotron-fluid">
<center>
<h2 class="a">Cats are cool</h2>
</center>
</div>
</div>
推荐阅读
- javascript - 部署具有 CDN 要求的 JS Widget 的正确方法?
- unit-testing - jest 测试中 jest 与 jasmine 语法的性能优势?
- excel - 如何为自定义误差线指定正值和负值
- tensorflow - LSTM 模型上的高 MAE
- powerquery - PowerQuery 上的高级筛选器
- r - 更简洁地定义行序列的索引
- kubernetes - Kubelet - 工厂“crio”无法处理容器
- python - 在pyqt5中使用多个窗口
- amazon-web-services - 更新 Amazon DynamoDB 中的项目字段而不覆盖其他字段
- java - 如何在 Jenkins 管道脚本中为字符串参数添加双引号