用CSS曲线底部的div到内部

时间:2018-01-04 08:36:16

标签: html css css-shapes

我想用CSS修改这个矩形div / background的底部,所以结果是这样的:

enter image description here 是否有人知道如何实现它?



.curved {
  margin: 0 auto;
  width: 800px;
  height: 500px;
  background: lightblue;
}

<div class="container">
  <div class="curved"></div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:10)

只需使用边框半径,并将元素设为溢出。您还可以依赖伪元素来避免额外的标记:

&#13;
&#13;
.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  background: lightblue;
  position: relative;
  overflow: hidden;
}

.container:after {
  content: "";
  position: absolute;
  height: 80px;
  left: -10%;
  right: -10%;
  border-radius: 50%;
  bottom: -25px;
  background: #fff;
}
&#13;
<div class="container">
</div>
&#13;
&#13;
&#13;

如果您想要透明的形状,也可以使用radial-gradient

&#13;
&#13;
body {
  background: pink;
}

.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  background: radial-gradient(110% 50% at bottom, transparent 50%, lightblue 51%);
}
&#13;
<div class="container">
</div>
&#13;
&#13;
&#13;

以下是使用clip-path的另一种方法(只需关注browser support):

&#13;
&#13;
.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  background-color: lightblue;
  position: relative;
  overflow: hidden;
}

.container:after {
  content: "";
  position: absolute;
  bottom: 0;
  right: -5%;
  left: -5%;
  height: 120px;
  background: #fff;
  -webkit-clip-path: ellipse(50% 60% at 50% 100%);
  clip-path: ellipse(50% 60% at 50% 100%);
}
&#13;
<div class="container">
</div>
&#13;
&#13;
&#13;

您也可以考虑使用SVG:

&#13;
&#13;
.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  background: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64' width='64' height='48' fill='lightblue'><path d='M0 0 L0 16 C16 6 48 6 64 16 L64 0 Z' /></svg>") top center/auto 700px no-repeat;
}
&#13;
<div class="container">
</div>
&#13;
&#13;
&#13;

如果您还想在形状周围添加边框,以下是一个示例:

&#13;
&#13;
.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  border: 2px solid #000;
  border-bottom: 0;
  background: lightblue;
  position: relative;
  overflow: hidden;
}

.container:after {
  content: "";
  position: absolute;
  height: 80px;
  left: -10%;
  right: -10%;
  border-radius: 50%;
  bottom: -62px;
  background: #fff;
  z-index: 2;
}

.container:before {
  content: "";
  position: absolute;
  height: 82px;
  left: -10%;
  right: -10%;
  border-radius: 50%;
  bottom: -62px;
  background: #000;
  z-index: 1;
}
&#13;
<div class="container">
</div>
&#13;
&#13;
&#13;

如果考虑mask-image

,您还可以将图像或渐变作为背景与透明度

&#13;
&#13;
body {
  background: pink;
}

.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  -webkit-mask-image: radial-gradient(110% 50% at bottom, transparent 50%, #fff 51%);;
  mask-image: radial-gradient(110% 50% at bottom, transparent 50%, #fff 51%);;
  background: linear-gradient(45deg,red,yellow,blue);
}
&#13;
<div class="container">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

检查一下。我创建了这个:在伪元素之后。如果背景是纯色的话会很有帮助。

&#13;
&#13;
.curved {
  margin: 0 auto;
  width: 300px;
  height: 300px;
  background: lightblue;
  position: relative;
}
.curved:after{
  background: white;
  position: absolute;
  content: "";
  left:0;
  right:0;
  bottom: -25px;
  height: 50px;
  border-radius: 50% 50% 0 0;
}
&#13;
<div class="container">
  <div class="curved"></div>
</div>
&#13;
&#13;
&#13;