【发布时间】:2019-08-11 11:25:00
【问题描述】:
我想让这个 div 容器内的图像在拉伸时不影响其容器大小,如果可能的话,我希望它在不设置容器高度或通过为位置指定负顶部或底部的情况下向上和底部移动。
.container {
display: flex;
background: skyblue;
height: 300px;
margin-top:50px;
}
img {
position:relative;
width: 500px;
top:-30px;
}
.column1 {
flex: 1;
}
.column2 {
flex: 1;
}
<div class="container">
<div class="column1">Hello world</div>
<div class="column2">
<img src="https://food.fnr.sndimg.com/content/dam/images/food/fullset/2013/6/28/0/FNK_Apple-Pie_s4x3.jpg.rend.hgtvcom.826.620.suffix/1382545039107.jpeg" alt="">
</div>
</div>
所以基本上我想要的是图像不影响容器大小,当拉伸 div 的图像大小以保持不变时,它可以出去但没有像这样为 div 设置高度。
编辑:我想让图像不影响它的 div 大小(我可以用高度来做)但我也希望它不仅从底部覆盖,而且从 div 的顶部覆盖(就像现在一样,但是不使用 css 中的负顶部位置)
【问题讨论】:
-
但容器
div将拉伸以适应img,除非您为其指定height。 -
对了,还有没有其他方法可以使用 flexbox?为了使它更具响应性,我怎样才能使它从上覆盖 div,而不仅仅是底部而不使用一些负位置?
-
那么,您希望容器
div的最终高度仅为.column1文本中的height,而img位于底部? -
让我编辑这个 sn-p 来向你展示我真正想要的东西,但不是用那种方法(因为它是错误的)
-
是的,那太好了。