如何创建可与背景渐变和重叠的透明形状配合使用的嵌入式弯曲背景?

时间:2018-07-14 16:53:34

标签: javascript jquery css css3 frontend

我一直在尝试创建网页的一部分,如下图所示,仅包含CSS。我无法弄清楚如何在背景的顶部和底部创建曲线并保持完整的透明覆盖层。上面和下面的部分(部分显示)也使用透明背景。因此,您会看到圆具有不同的阴影。

enter image description here

2 个答案:

答案 0 :(得分:2)

我认为仅使用CSS最好的近似方法就是使用radial-gradient

.box {
  margin:50px;
  height:300px;
  background:
   radial-gradient(ellipse at top ,transparent 19.5%,#3adecf 20%, #3ae7be) top/400% 50% no-repeat,
   radial-gradient(ellipse at bottom ,transparent 19.5%, #3ae3c5 20%,#3ae7be) bottom/400% 50% no-repeat;

}

body {
 background:pink;
}
<div class="box">

</div>

答案 1 :(得分:1)

这就是我要尝试的方式。要使它在您的网站上运行可能需要一些技巧,但希望您可以看到我使用的技术。

基本上,您是将div作为内容的背景,并将其转换为叠加层。然后将任何内容放入可以更改z-index的容器中,以使其高于其他所有内容。曲线本身仅是50%的边界半径,并且倾斜的宽度大于高度。然后,内容的父div具有渐变颜色背景。

我确实喜欢Temani解决方案的简单性。

/* top ellipse */
.top {
  border-radius: 50%;
  width: 150%;
  height: 200px;
  transform: translate(-15%, 20%);
  background: white;
}

/* content's parent container */
.mid {
  padding: 4rem;
  background: linear-gradient(-20deg, #3adecf, #3ae3c5);
}

.content {
  position: relative;
  z-index: 20;
  color: white;
  font-size: 30px;
}

/* bottom ellipse */
.bottom {
  border-radius: 50%;
  width: 200%;
  height: 200px;
  transform: translate(-25%, -20%);
  background: white;
}

.all {
  overflow-y: hidden;
  position: relative;
}

.t-circle {
  background: #00ccdd;
  position: absolute;
  top: 5%;
  right: -200px;
  z-index: 10;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  opacity: 0.2;
  pointer-events: none;
}
<div class="all">
  <div class="top">Top</div>
  <div class="mid">
    <div class="content">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sapien mauris, efficitur in nisi vel, gravida mollis urna. Praesent ac sem vitae neque pretium ultricies. Vestibulum id mattis neque. Nullam ultricies neque eget metus volutpat, et tempus magna commodo. Phasellus accumsan lacus nibh, at commodo elit pellentesque at. Morbi iaculis bibendum massa, sit amet accumsan felis ullamcorper a. Praesent luctus odio vel tortor finibus feugiat. Sed luctus finibus nisl, in pellentesque orci efficitur ut. Curabitur suscipit elementum aliquam. Sed vel convallis urna.
      <br><br>
        Phasellus porttitor blandit ornare. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam elementum rhoncus diam ac rhoncus. Nunc sed lorem porttitor, placerat sem vitae, bibendum nunc. Ut dolor mi, condimentum vitae leo in, suscipit maximus risus. Morbi consequat dui eros, sit amet dapibus urna porta tempor. Fusce mollis a velit nec auctor. Donec semper elementum feugiat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Pellentesque vel turpis pellentesque, ultricies metus blandit, interdum nibh. Duis malesuada dolor lacus, quis tempor erat elementum in. Pellentesque at consequat nisl. Vestibulum vel urna nec ipsum interdum pellentesque id nec magna. Mauris eu lectus posuere, aliquet justo id, pharetra nisl. 
      </div>
    </div>
  <div class="bottom">bottom</div>
  <div class="t-circle"></div>
</div>