How can I create an inset curved background that works with a background gradient and overlapping transparent shapes?
I have been trying to create a section of a webpage that looks like the image below with just CSS. I can not figure out how to create the curves the top and bottom of the background and keep intact the transparent overlays. The section above and below (partially shown) also use transparent backgrounds. So, you see the different shades the circles have.
Solution 1:
I think the best approximation you can have with only CSS is the use of 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>
Or you consider the use of mask
.box {
margin:50px;
height:300px;
background:linear-gradient(45deg,red,blue);
-webkit-mask:
radial-gradient(60% 30% at top ,transparent 98%, #fff) top /100% 50% no-repeat,
radial-gradient(60% 30% at bottom ,transparent 98%, #fff) bottom/100% 50% no-repeat;
mask:
radial-gradient(60% 30% at top ,transparent 98%, #fff) top /100% 50% no-repeat,
radial-gradient(60% 30% at bottom ,transparent 98%, #fff) bottom/100% 50% no-repeat;
}
body {
background:pink;
}
<div class="box">
</div>
Solution 2:
Here is how I would attempt it. It will probably take a bit of finessing to make it work for your site, but hopefully you can see the technique I used.
Basically you are using divs as the background to content and translating them to overlay. Then putting any content in a container that you can change the z-index on to bring above everything else. The curve itself is just a border-radius of 50% and skewed wider than tall. Then the content's parent div has the gradient color background.
I do like the simplicity of Temani's solution though.
/* 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>