CSS 剪辑路径在带有子元素/伪元素的 Safari 中不起作用

发布于 2025-01-16 21:59:19 字数 985 浏览 5 评论 0原文

我有一个非常基本的 SVG 剪辑路径:

<svg width="0" height="0">
  <defs>
    <clipPath id="line">
      <path d="..." />
    </clipPath>
  </defs>
</svg>

我想使用它与 CSS:

div {
  clip-path: url(#line);

  &:before {
    ...
  }
}

它在 Google Chrome 上运行良好,但在 Safari 中根本不显示。
还尝试添加 -webkit- 前缀。
这是一个现场演示:https://jsfiddle.net/1dtpLg8y/
如果您删除 :before 元素,它实际上会起作用: https://jsfiddle.net /fp5mouLn/
这是一个仅适用于 SVG 的版本: https://jsfiddle.net/3gt67cLh/1/ 但会喜欢用 CSS 来做这件事。

预期结果:
输入图片此处描述

在 Safari 中不显示任何内容。

I got a pretty basic SVG clip-path:

<svg width="0" height="0">
  <defs>
    <clipPath id="line">
      <path d="..." />
    </clipPath>
  </defs>
</svg>

Which i wanna use w/ CSS:

div {
  clip-path: url(#line);

  &:before {
    ...
  }
}

It's working fine with Google Chrome, but not showing at all in Safari.
Also tried adding the -webkit- prefix.
Here's a live demo: https://jsfiddle.net/1dtpLg8y/
Its actually working if you get rid of the :before element: https://jsfiddle.net/fp5mouLn/
Here's a working SVG only version: https://jsfiddle.net/3gt67cLh/1/ but would love to do this with CSS.

Expected outcome:
enter image description here

Not showing anything in Safari.

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

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

发布评论

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

评论(2

三生一梦 2025-01-23 21:59:19

overflow:hidden 添加到行 div 似乎可以解决此问题。

(在 iOs safari 上测试)

.line {
  -webkit-clip-path: url(#line);
  clip-path: url(#line);
  background-color: #ccc;
  width: 196px;
  height: 6px;
  position: relative;
  overflow: hidden;
}
.line:before {
  content: "";
  width: 80px;
  height: 80px;
  background: radial-gradient(red, transparent 35%);
  position: absolute;
  left: -30px;
  top: -36px;
}

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
}
<div class="line"></div>

<svg width="0" height="0">
  <defs>
    <clipPath id="line">
      <path
        fillRule="evenodd"
        clipRule="evenodd"
        d="M0 5C0 4.65482 0.279822 4.375 0.625 4.375H5C8 4 9.11049 0.375 10 0.375C11 0.375 12 4 15 4.375H195.375C195.72 4.375 196 4.65482 196 5V5C196 5.34518 195.72 5.625 195.375 5.625H15C12 5.5 10 1.8 10 1.8C10 1.8 8 5.5 5 5.625H0.625C0.279822 5.625 0 5.34518 0 5V5Z"
        fill="black"
      />
    </clipPath>
  </defs>
</svg>

Adding overflow:hidden to your line div seems to solve this issue.

(tested on iOs safari)

.line {
  -webkit-clip-path: url(#line);
  clip-path: url(#line);
  background-color: #ccc;
  width: 196px;
  height: 6px;
  position: relative;
  overflow: hidden;
}
.line:before {
  content: "";
  width: 80px;
  height: 80px;
  background: radial-gradient(red, transparent 35%);
  position: absolute;
  left: -30px;
  top: -36px;
}

body {
  display: grid;
  place-items: center;
  min-height: 100vh;
}
<div class="line"></div>

<svg width="0" height="0">
  <defs>
    <clipPath id="line">
      <path
        fillRule="evenodd"
        clipRule="evenodd"
        d="M0 5C0 4.65482 0.279822 4.375 0.625 4.375H5C8 4 9.11049 0.375 10 0.375C11 0.375 12 4 15 4.375H195.375C195.72 4.375 196 4.65482 196 5V5C196 5.34518 195.72 5.625 195.375 5.625H15C12 5.5 10 1.8 10 1.8C10 1.8 8 5.5 5 5.625H0.625C0.279822 5.625 0 5.34518 0 5V5Z"
        fill="black"
      />
    </clipPath>
  </defs>
</svg>

∞琼窗梦回ˉ 2025-01-23 21:59:19

如果herrstrietzel的解决方案不起作用,请尝试“filter:hue-rotate(0deg);”

我遇到了类似的问题,添加“overflow:hidden”不起作用,但添加“filter:hue-rotate(0deg)”却起作用。

If herrstrietzel's solution does not work, try "filter: hue-rotate(0deg);"

I had a similar problem, and adding "overflow:hidden" did not work, but adding "filter: hue-rotate(0deg)" did.

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