问题
I am trying to overlay a header onto an image. However the background is hidden under the image. Why is this happening?
http://jsfiddle.net/YRDFB/
<div class="header">
<img width="100%" height="200px" src="/path/to/image.jpg" />
<h1>Header Title</h1></div>
h1 {
background: rgba(67,67,67,0.8);
margin-top: -3em;
}
回答1:
We have 2 solutions here:
Using
display:inline-blockand setwidth:100%for yourh1, also use negativemargin-bottomon theimginstead:h1 { background: rgba(67,67,67,0.8); display:inline-block; width:100%; } img { margin-bottom:-4em; }Demo 1.
Using
position:relativefor the img and setz-index:-1for it:h1 { background: rgba(67,67,67,0.8); } img { margin-bottom:-4em; position:relative; z-index:-1; }Demo 2.
回答2:
Are you looking for this Updated Fiddle
h1 {
background: rgba(67, 67, 67, 0.8);
position:relative;
z-index:9;
display:block;
width:100%;
padding:0;
color:#fff;
margin-top:-43px;
}
来源:https://stackoverflow.com/questions/23285146/background-hidden-when-overlaying-header-with-negative-top-margins