【问题标题】:convert svg to css shape将 svg 转换为 css 形状
【发布时间】:2020-12-22 07:00:18
【问题描述】:

我正在尝试在 css 中执行此“圆形蛋”,但它不适用于我。

我有这个圆圈作为 SVG,我想在我的网站中使用它并在其中放置一个图像, 使用 Pattern 和 Image 将图像放置在 svg 中对我不起作用,因为我无法控制背景大小并重复。

 <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="115" height="120"><defs><path id="a" d="M93.174 120C124.931 120 148 93.748 148 61.364S127.605 0 95.849 0C64.092 0 33 28.98 33 61.364S61.418 120 93.174 120z"/></defs><use fill="#00C1B1" fill-rule="evenodd" transform="translate(-33)" xlink:href="#a"/></svg>

所以我决定使用 css 制作这张图片

请注意左上角 Circle

.circle{
  margin-top:50px;
  margin-left:50px;
  background-color:#00C1B1;
  border-radius: 70% 50% 60% 60% / 70% 70% 55% 60%;
  height: 120px;
  width:115px;
}
<div class="circle">
</div>

我尝试过使用边界半径,但它似乎不适用于我。

谢谢

【问题讨论】:

  • 分享您的SVG,我们可以将它用于您的图像并且没有图案

标签: css svg border


【解决方案1】:

使用 SVG 作为遮罩,您可以轻松考虑背景。只需确保正确设置 SVG 的 viewBox:

.box {
  -webkit-mask:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="30 0 120 120" ><path d="M93.174 120C124.931 120 148 93.748 148 61.364S127.605 0 95.849 0C64.092 0 33 28.98 33 61.364S61.418 120 93.174 120z" fill="black"/></svg>') center/contain no-repeat;
          mask:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="30 0 120 120" ><path d="M93.174 120C124.931 120 148 93.748 148 61.364S127.605 0 95.849 0C64.092 0 33 28.98 33 61.364S61.418 120 93.174 120z" fill="black"/></svg>') center/contain no-repeat;
   width:200px;
   display:inline-block;
   background:red;
}
/* to maintain the ratio */
.box::before {
   content:"";
   display:inline-block;
   padding-top:100%;
}


svg {
  border:1px solid;
}
<div class="box"></div>

<div class="box" style="background:url(https://picsum.photos/id/1000/800/800.jpg) center/cover;width:150px;"></div>

<div class="box" style="background:url(https://picsum.photos/id/1074/800/800.jpg) center/cover;width:100px"></div>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" viewBox="30 0 120 120" ><path d="M93.174 120C124.931 120 148 93.748 148 61.364S127.605 0 95.849 0C64.092 0 33 28.98 33 61.364S61.418 120 93.174 120z" fill="black"/></svg>

【讨论】:

  • @AhmadKhalaf 我会说,我不明白为什么会这样。如果您遇到任何问题,您仍然可以回到这里
  • 我已经更新了问题,遗憾的是在应用到 React 之后,形状渲染但里面的图像没有
  • @AhmadKhalaf 请不要用我的回答更新您的问题。您的问题需要保持原样,否则我的回答将变得无关紧要
  • @AhmadKhalaf 您应该提出一个新问题或分享问题之外的代码,以便我查看。 SO 不是聊天系统,我们应该有问题和答案
  • @AhmadKhalaf 您的屏幕截图也显示代码运行良好,因为您可以看到红色背景。图片可能无法加载,请尝试其他图片
猜你喜欢
  • 2016-01-14
  • 2015-10-13
  • 2022-11-26
  • 2011-04-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 2019-10-27
相关资源
最近更新 更多