如何使用CSS创建波浪形状

6
我正在尝试将一个li元素显示为波浪形。我不想使用任何背景图片,但是border-radius属性不支持负值。希望你能帮助我。 is this possible in CSS

你还尝试过什么?能否贴出一些示例代码? - evolutionxbox
2
我怀疑你可以用CSS创建随机光滑曲线,我认为没有SVG是不可能的。 - Andrey
请查看此线程,并尝试其中提到的选项。 - Harry
好的,谢谢。SVG 似乎最适合。 - CREA Nico Wehmöller
2个回答

5
我能做的最接近是仅使用CSS来实现这个效果。

.one {
  position: absolute;
  top: 22px;
  left: 19px;
  width: 230px;
  height: 180px;
  background: #0F1E3C;
  border-radius: 100%;
  clip: rect(70px, auto, auto, 45px);
  transform:rotate(90deg);
}

.one:before {
  content: '';
  position: absolute;
  top: -15px;
  left: -62px;
  width: 200px;
  height: 200px;
  background: white;
  border-radius: 100%;
}

.two {
  position: absolute;
  top: 156px;
  left: 59px;
  width: 230px;
  height: 180px;
  background: #0F1E3C;
  border-radius: 100%;
  clip: rect(70px, auto, auto, 45px);
  transform:rotate(-90deg);
}

.two:before {
  content: '';
  position: absolute;
  top: -15px;
  left: -62px;
  width: 200px;
  height: 200px;
  background: white;
  border-radius: 100%;
}
<div class="one"></div>
<div class="two"></div>


1
不错的尝试,伙计(虽然我仍然建议使用SVG) :) - Harry
嘿,我刚试了一下,这可能对那些被限制使用SVG的人有用。 - Suresh Karia
1
没问题,绝对没有。我喜欢CSS Shapes :) - Harry
1
我也喜欢创建形状和使用伪元素,看到你的CodePen个人资料很酷,你真棒!@Harry - Suresh Karia

3

尽管SVG在这里是一个更好的选择,但您可以使用类似边框的技巧来创建此形状的基础。

以下是此类形状的基本示例,但我必须说有很大的改进空间:

div {
  position:relative;
  height:100px;
  width:100px;
  background:lightgray;
  overflow:hidden;
}

div:before {
  content:"";
  position:absolute;
  top:calc(-100% - 25px);
  left:00%;
  height:200%;
  width:200%;
  border-radius:50%;
  background:cornflowerblue;
  border-bottom:50px solid blue;
}
div:nth-child(2) {
  transform:rotate(180deg);
  margin-left:40px;
  margin-top:-25px;
}
<div></div>
<div></div>

一个快速的SVG版本:

<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="407pt" height="126pt" viewBox="0 0 407 126">
  <g transform="translate(0,126) scale(0.1,-0.1)" fill="#000000" stroke="none">
    <path d="M43 1223 c-4 -21 -8 -119 -7 -218 0 -169 2 -185 28 -265 153 -466
545 -728 1030 -689 276 23 694 112 1116 239 175 53 375 99 501 116 149 19 363
15 453 -10 134 -37 273 -132 351 -241 26 -36 42 -51 46 -42 4 7 13 58 20 115
29 239 -44 492 -201 700 -99 132 -238 236 -405 303 l-76 30 -417 -3 -417 -4
-190 -37 c-104 -21 -275 -58 -380 -82 -316 -73 -466 -96 -678 -102 -124 -4
-218 -2 -280 7 -175 23 -341 91 -437 177 l-49 44 -8 -38z" />
  </g>
</svg>


2
投票支持SVG,特别是支持使用SVG的JB :D - Harry

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接