如何使用Flexbox将可分解的div包裹在图像周围
我试图在宽度或高度变化时试图围绕图像缠绕。
问题是,当宽度变化时,div不会紧密包裹孩子,在这种情况下,孩子是一个图像:
我确实确定在调整DIV大小时将行和列之间的挠性方向求解,并且可以使用诸如调整大小的观察者之类的东西来切换flex方向,但希望有CSS解决方案?
这是一个有问题的代码笔: https://codepen.io/quinnaz/quinnaz/pen/quinnaz/pen/pen/rnjdjjy
<div class="container direction-row">
<div class="border">
<img src="https://dummyimage.com/400x600/d4b9d4/7477a3.png" class="img-element" />
</div>
</div>
.container {
resize: both;
overflow: auto;
background-color: beige;
border: solid;
display: flex;
}
.direction-row {
flex-direction: row;
}
.direction-col {
flex-direction: column;
}
.img-element {
max-width: 100%;
max-height: 100%;
display: block;
}
.border {
border-width: 50px;
border-color: blue;
border-style: solid;
}
Im trying to wrap a div around a image both when the width or height changes.
The issue is that when the width changes the div does not tightly wrap against the child in this case the child is a image:
Wrap div around a image current result
I did determine that setting the flex-direction between row and column solves it when the div gets resized and could use something like a resize observer to toggle the flex direction but hope there is a css solution to this?
Here is a code pen with the issue: https://codepen.io/quinnaz/pen/rNJdjJy
<div class="container direction-row">
<div class="border">
<img src="https://dummyimage.com/400x600/d4b9d4/7477a3.png" class="img-element" />
</div>
</div>
.container {
resize: both;
overflow: auto;
background-color: beige;
border: solid;
display: flex;
}
.direction-row {
flex-direction: row;
}
.direction-col {
flex-direction: column;
}
.img-element {
max-width: 100%;
max-height: 100%;
display: block;
}
.border {
border-width: 50px;
border-color: blue;
border-style: solid;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
object-fit
属性,并将其提供值cover
。我还将分别更改Max Width
和最大高处
分别为width
和height
。替换的内容(在这种情况下为图像)的大小以维持其长宽比,同时填充元素的整个内容框。如果对象的纵横比与其框的纵横比不匹配,则将剪辑对象以适合。
codepen link https://codepen.io/thechewy/thechewy/thechewy/pen/pen/pen/pen/pen/pen/perpen/perxevo
** 编辑 **
如果您希望它完全适合
.container
div,则需要制作.border
div用width填充父
.container
不过,问题不清楚。如果不是上面的片段,则应该解决问题。我个人只会将边框添加到图像中,然后删除额外的DIV和CSS。
You need to use the
object-fit
property and give it the valuecover
. I would also changemax-width
andmax-height
towidth
andheight
respectively.The replaced content (in this case an image) is sized to maintain its aspect ratio while filling the element's entire content box. If the object's aspect ratio does not match the aspect ratio of its box, then the object will be clipped to fit.
codepen link https://codepen.io/thechewy/pen/ZErxevo
** EDIT **
If you want it to fully fit the
.container
div you'll then need to make the.border
div fill the parent.container
withwidth: 100%;
height: 100%;
set on.border
, this isn't clear in the question though. If not the above snippet should do the trick.Personally I would just add the border to the image and remove the extra div and CSS.