图片重叠两个 div(垂直)
Image overlap two div's (vertically)
我有一个正在创建的网站,我想将图像垂直叠加在两个单独的 div 上。我真的在寻找解决这个问题的最佳方法。
这是我当前的代码:
HTML
<div class="top-div">Some text</div>
<div class="bottom-div">Some text</div>
<img src="thisimage.jpg" alt="overlap top and bottom div" height="280" width="100" />
顶部和底部 div 的最小高度均为 280 像素。
您正在寻找这样的东西吗?这里不需要position
,直接用负数margin
就可以了。但唯一的问题是您需要稍微更改标记。
.top-div,
.bottom-div {min-height: 280px; background: #99f;}
.top-div {background: #f99;}
img {display: block; margin: -140px auto;}
<div class="top-div">Some text</div>
<img src="//placehold.it/100x280" alt="overlap top and bottom div" height="280" width="100" />
<div class="bottom-div">Some text</div>
预览
另一种方法是将您的 div 和图像放入单独的容器中,然后使用相对位置和绝对位置。
Fiddle:
https://jsfiddle.net/ewpgovvs/
代码:
div {
border: 2px solid red;
}
.wrapper {
position:relative;
}
img {
position:absolute;
width:100%;
height:100%;
top:0;
left:0;
}
<div class="wrapper">
<div class="top-div">Some text text text text text text text text text text text text text text text t text text text text text text text text text text text textext text text text text text text text text text text text</div>
<div class="bottom-div">Some text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>
<img src="http://pngimg.com/upload/cat_PNG1631.png" alt="overlap top and bottom div" />
</div>
我有一个正在创建的网站,我想将图像垂直叠加在两个单独的 div 上。我真的在寻找解决这个问题的最佳方法。
这是我当前的代码:
HTML
<div class="top-div">Some text</div>
<div class="bottom-div">Some text</div>
<img src="thisimage.jpg" alt="overlap top and bottom div" height="280" width="100" />
顶部和底部 div 的最小高度均为 280 像素。
您正在寻找这样的东西吗?这里不需要position
,直接用负数margin
就可以了。但唯一的问题是您需要稍微更改标记。
.top-div,
.bottom-div {min-height: 280px; background: #99f;}
.top-div {background: #f99;}
img {display: block; margin: -140px auto;}
<div class="top-div">Some text</div>
<img src="//placehold.it/100x280" alt="overlap top and bottom div" height="280" width="100" />
<div class="bottom-div">Some text</div>
预览
另一种方法是将您的 div 和图像放入单独的容器中,然后使用相对位置和绝对位置。
Fiddle: https://jsfiddle.net/ewpgovvs/
代码:
div {
border: 2px solid red;
}
.wrapper {
position:relative;
}
img {
position:absolute;
width:100%;
height:100%;
top:0;
left:0;
}
<div class="wrapper">
<div class="top-div">Some text text text text text text text text text text text text text text text t text text text text text text text text text text text textext text text text text text text text text text text text</div>
<div class="bottom-div">Some text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text</div>
<img src="http://pngimg.com/upload/cat_PNG1631.png" alt="overlap top and bottom div" />
</div>