使用CSS Border-Radius(圆角),如果图像被缩放(最大宽度/高度),我们如何防止圆角消失?

发布于 2025-01-26 19:18:50 字数 1637 浏览 1 评论 0 原文

考虑这种情况:

  1. 图像使用flex框在水平和垂直方面居中,
  2. 图像具有配置的CSS Border-radius(圆角),
  3. 图像在< a> a> 标记
  4. 图像中配置了图像。使用CSS占用水平或垂直空间的50%,并在需要时按比例缩小
<body style="margin:0;overflow:hidden;width:100%;height:100%;position:fixed;text-align:center;background-color:green">
  <div style="flex-direction:column;justify-content:center;align-items:center;display:flex;height:100vh;width:100%">
    <a href="https://google.com/" style="max-width:50%;max-height:50%">
    <img src="https://i.imgur.com/CHHOCY5.png" style="border-radius:20%;object-fit:scale-down;max-height:100%;max-width:100%;width:200px;height:200px">
    </a>
  </div>
</body>

示例:

正常操作(可用空间):

如果可用的高度和宽度都以大约相等的比例降低,则圆角为保留为图像收缩:

”如果可用的高度和宽度都大致相等地降低,则保留圆角”

但是,如果水平空间受到约束,但垂直空间却没有,反之亦然,则丢失了圆角:

垂直空间约束:

“垂直约束”

水平空间约束:

Border-Radius是否以像素表达或百分比行为的百分比,都会发生问题。

无论是在Firefox和Chrome中都验证了

Consider this scenario:

  1. An image is centered both horizontally and vertically using a flex box
  2. The image has a CSS border-radius configured (rounded corners)
  3. The image is inside an <a> tag
  4. The image is configured using CSS to take up no more than 50% of the horizontal or vertical space, and to shrink down proportionally if needed
<body style="margin:0;overflow:hidden;width:100%;height:100%;position:fixed;text-align:center;background-color:green">
  <div style="flex-direction:column;justify-content:center;align-items:center;display:flex;height:100vh;width:100%">
    <a href="https://google.com/" style="max-width:50%;max-height:50%">
    <img src="https://i.imgur.com/CHHOCY5.png" style="border-radius:20%;object-fit:scale-down;max-height:100%;max-width:100%;width:200px;height:200px">
    </a>
  </div>
</body>

Example: https://jsfiddle.net/c8h0L7o1/

Normal operation (ample space available):

Normal operation (ample space available):

If available height and width are both reduced in roughly equal proportion, the rounded corners are retained as the image shrinks:

If available height and width are both reduced in roughly equal proportion, the rounded corners are retained

However if horizontal space is constrained but vertical space is not, or vice versa, the rounded corners are lost:

Vertical space constraint:

vertical constraint

Horizontal space constraint:

horizontal constraint

How can the rounded corners be retained in these scenarios?

The problem happens regardless if the border-radius is expressed in pixels or percent

Behavior has been verified in both Firefox and Chrome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

殊姿 2025-02-02 19:18:50

只需在锚标签中添加边框半径

<a style="border-radius:20%" > img </a>

just add border radius in anchor tag too

<a style="border-radius:20%" > img </a>
旧城烟雨 2025-02-02 19:18:50

删除宽度:200px;身高:200px; 来自IMG标签

Delete width:200px; height: 200px; from img tag

神也荒唐 2025-02-02 19:18:50

为IMG添加类:

Then try to edit by CSS code

add class for img:

Then try to edit by CSS code

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