背景缺一部分的实现方式

最近接到一个需求,要实现背景缺两个半圆,而且缺的半圆是透明的,要显示出下面的图片。
思考一下,可以用css3的渐变来实现。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<html>
<head>
<style>
#demo .container{
background-color: yellow;
width: 500px;
height: 500px;
}
#demo .container-nav{
background-color: red;
width: 300px;
height: 300px;
position: relative;
background: -webkit-radial-gradient(0px 150px ,circle,transparent 0%,transparent 50px,#ff0000 50px)
}
</style>
</head>
<body id="demo">
<div class="container">
<div class="container-nav"></div>
</div>
</body>
</html>

background

利用渐变,从透明到红色的径向渐变可以模拟出缺一个半圆的效果。

如果要两边都实现这个效果可以设两个渐变,但是要注意:第一个渐变右边都是红色的,第二个渐变左边都是红色的,两个重合在一起就全变成红色了,所以再加几个颜色控制点,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!doctype html>
<html>
<head>
<style>
#demo .container{
background-color: yellow;
width: 500px;
height: 500px;
}
#demo .container-nav{
background-color: red;
width: 300px;
height: 300px;
position: relative;
background: -webkit-radial-gradient(300px 150px ,circle,transparent 0%,transparent 50px,#ff0000 50px,#ff0000 200px,transparent 250px),-webkit-radial-gradient(0px 150px ,circle,transparent 0%,transparent 50px,#ff0000 50px,#ff0000 200px,transparent 250px);
}
</style>
</head>
<body id="demo">
<div class="container">
<div class="container-nav"></div>
</div>
</body>
</html>

效果如下:

background