Html Z-index 无法按预期使用图片和背景 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8136655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Z-index not working as expected working with picture and background-div
提问by OptimusCrime
I have a image, and I'd like to have an effect on my page where a background-div with gray color is displayed 5px on the left side and bottom of the picture. But for some reason, I can't get the background-div to align behind the picture. It always lays ontop. How can I fix this?
我有一个图像,我想在我的页面上有一个效果,其中一个带有灰色的背景 div 在图片的左侧和底部显示 5px。但由于某种原因,我无法让背景 div 在图片后面对齐。它总是躺在上面。我怎样才能解决这个问题?
Jsfiddle: http://jsfiddle.net/3Z7m4/2/
jsfiddle:http: //jsfiddle.net/3Z7m4/2/
回答by Jarno Argillander
Add position: relative;
into the img
CSS.
添加position: relative;
到img
CSS。
See: http://www.w3schools.com/cssref/pr_pos_z-index.asp
见:http: //www.w3schools.com/cssref/pr_pos_z-index.asp
Note: z-index only works on positioned elements (position:absolute, position:relative, or position:fixed).
注意:z-index 仅适用于定位元素(位置:绝对、位置:相对或位置:固定)。
回答by anglimasS
just try,
你试一试,
.container .gray {
width: 420px;
height: 315px;
position: absolute;
margin-left: 5px;
margin-top: -315px;
z-index: -10;
background-color: gray;
}