我希望将标题叠加到图像上.我已设法做到这一点,但图像正在从父母扩展div.
我有包含div设置inline-block,因为我希望它'自动大小',而不是占用width: 100%.如果查看当前输出,您将看到图像可能位于黑色边框内.
如果您遇到跨浏览器问题,它只需要在Chrome中运行.
提前致谢!
CSS:
#body_content {
border: solid 1px blue;
display: inline-block;
padding: 5px;
}
#body_header {
border: solid 1px red;
font-size: 25px;
padding: 5px;
}
#body_image {
position: absolute;
}
#body_image_caption {
color: white;
line-height: 30px;
margin-left: 10px;
}
#body_image_container {
background: white;
border: solid 1px black;
margin-top: 3px;
padding: 10px;
}
#body_image_overlay {
background-color: black;
bottom: 5px;
display: block;
height: 30px;
opacity: 0.85;
position: absolute;
width: 100%;
}?
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="body_content">
<div id="body_header">
Heading
</div>
<div id="body_image_container">
<div id="body_image">
<img src="http://i.imgur.com/s6G8n.jpg" width="200" height="200" />
<div id="body_image_overlay">
<div id="body_image_caption">
Some Text
</div>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
cim*_*non 20
该#body_image元素正在逃避,#body_image_container因为它position被设置为absolute.绝对定位的元素将从文档的流中移除,导致父元素崩溃,就像子元素不存在一样.如果将其更改为relative,则它将包含在黑匣子中:
#body_image{
position: relative;
}
Run Code Online (Sandbox Code Playgroud)