如何使用Flexbox将可分解的div包裹在图像周围

发布于 2025-02-03 00:25:34 字数 1010 浏览 0 评论 0原文

我试图在宽度或高度变化时试图围绕图像缠绕。

问题是,当宽度变化时,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2025-02-10 00:25:34

您需要使用object-fit属性,并将其提供值cover。我还将分别更改Max Width最大高处分别为widthheight

替换的内容(在这种情况下为图像)的大小以维持其长宽比,同时填充元素的整个内容框。如果对象的纵横比与其框的纵横比不匹配,则将剪辑对象以适合。

codepen link https://codepen.io/thechewy/thechewy/thechewy/pen/pen/pen/pen/pen/pen/perpen/perxevo

.img-element {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

** 编辑 **

如果您希望它完全适合.container div,则需要制作.border div用width填充父.container不过,问题不清楚。如果不是上面的片段,则应该解决问题。

我个人只会将边框添加到图像中,然后删除额外的DIV和CSS。

You need to use the object-fit property and give it the value cover. I would also change max-width and max-height to width and height 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

.img-element {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

** EDIT **

If you want it to fully fit the .container div you'll then need to make the .border div fill the parent .container with width: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文