使用CSS将div的底部弯曲到内部 [英] Curve bottom side of the div to the inside with CSS

查看:393
本文介绍了使用CSS将div的底部弯曲到内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用CSS弯曲此矩形div/background的底边,所以结果是这样的:

I would like to curve the bottom side of this rectangle div/background with CSS, so the result is something like this:

有人可能会实现一个想法吗?

Does someone have an idea perhaps how it could be achieved?

.curved {
  margin: 0 auto;
  height: 400px;
  background: lightblue;
  border-radius:0 0 200px 200px;
}

<div class="container">
  <div class="curved"></div>
</div>

推荐答案

只需使用border-radius并依靠一些溢出即可.您还可以考虑使用伪元素来避免额外的标记:

Simply use border-radius and rely on some overflow. You can also consider pseudo element to avoid extra markup:

.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;
}

<div class="container">
</div>

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

You can also use radial-gradient if you want a transparent shape:

body {
  background: pink;
}

.container {
  margin: 0 auto;
  width: 500px;
  height: 200px;
  background: radial-gradient(110% 50% at bottom, transparent 50%, lightblue 51%);
}

<div class="container">
</div>

这是使用clip-path

.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%);
}

<div class="container">
</div>

您还可以考虑使用SVG:

You can also consider SVG:

.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;
}

<div class="container">
</div>

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

Here is an example if you want also to add border around your shape:

.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;
}

<div class="container">
</div>

如果您希望将图像或渐变作为具有透明度的背景,请使用 mask-image :

If you want to have an image or gradient as background with the transparency, use mask-image:

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);
}

<div class="container">
</div>

这篇关于使用CSS将div的底部弯曲到内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆