banner
banner
banner
NEWS LETTER

CSS-画多边形

Scroll down

画多边形不需要拼接多个元素,clip-path 可以一步裁出图形。它支持 inset() 裁矩形、circle() 裁圆、ellipse() 裁椭圆、polygon() 裁多边形、path 裁任意 SVG 形状。这篇给出取值总览、一个可直接运行的完整示例,以及每种函数在原文示例中的参数写法。

背景:clip-path 是什么

clip-path 直接裁出图形,无需拼接元素实现图形。它只影响绘制结果,不改变元素在文档流中占的位置,所以被裁掉的部分依然占据原来的布局空间。

第一步:了解 clip-path 的取值

值 说明
inset() 裁剪出一个矩形
circle() 裁剪出一个圆形
ellipse() 裁剪出一个椭圆
polygon() 裁剪出一个多边形
path 裁剪出一个任意形状
(使用 SVG 填充规则和一个 SVG 路径定义)

第二步:跑一个完整示例

下面这份 HTML 把 inset、circle、ellipse、polygon、path 五种取值放在同一页里,可以直接保存为 .html 打开对照效果:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
<title>多边形案例</title>
<style>
.line {
display: flex;
align-items: center;
background-color: rgb(164, 192, 245);
}
.leaf {
height: 152px;
width: 200px;
clip-path: inset(10px round 152px 5px);
line-height: 152px;
background-color: cadetblue;
text-align: center;
}
.shanbei {
height: 152px;
width: 200px;
clip-path: inset(10px round 20px 10px 150px);
line-height: 152px;
background-color: cadetblue;
text-align: center;
}
.roundjuxing {
height: 152px;
width: 200px;
clip-path: inset(10px 20px 10px 30px round 20px);
line-height: 152px;
background-color: cadetblue;
text-align: center;
}
.circle1 {
width: 200px;
height: 200px;
background-color: #ffb700;
margin-right: 40px;
text-align: center;
clip-path: circle(6rem at right center);
}
.circle2 {
width: 200px;
height: 200px;
background-color: #ffb700;
text-align: center;
margin-right: 40px;
clip-path: circle(10% at 2rem 90%);
}
.circle3 {
width: 200px;
height: 100px;
background-color: #ffb700;
margin-right: 40px;
text-align: center;
clip-path: circle(closest-side at 50% 50%);
}
.circle4 {
width: 200px;
height: 100px;
background-color: #ffb700;
text-align: center;
margin-right: 40px;
clip-path: circle(farthest-side at 50% 50%);
}
.ellipse1 {
width: 300px;
height: 200px;
text-align: center;
background-color: steelblue;
clip-path: ellipse(4rem 50% at right center);
}
.ellipse2 {
width: 300px;
height: 200px;
text-align: center;
background-color: steelblue;
clip-path: ellipse(closest-side closest-side at 50% 68%);
}
.ellipse3 {
width: 300px;
height: 200px;
text-align: center;
background-color: steelblue;
clip-path: ellipse(farthest-side farthest-side at 50% 68%);
}
.polygon1 {
width: 300px;
height: 200px;
text-align: center;
background-color: rgb(243, 117, 0);
clip-path: polygon(
0% 0%,
100% 0%,
100% 75%,
75% 75%,
75% 100%,
50% 75%,
0% 75%
);
}
.path1 {
width: 300px;
height: 300px;
text-align: center;
background-color: rgb(243, 117, 0);
clip-path: path(
'M 150 0 L 184 112 L 300 112 L 207 175 L 241 288 L 150 225 L 59 288 L 93 175 L 0 112 L 116 112 Z'
);
}
</style>
</head>
<body>
<div class="line">
<span>inset</span>
<div class="leaf">叶子</div>
<div class="shanbei">扇贝</div>
<div class="roundjuxing">圆角矩形</div>
</div>
<div class="line">
<span>circle</span>
<div class="circle1">方向值偏移</div>
<div class="circle2">数值偏移</div>
<div class="circle3">closest-side</div>
<div class="circle4">farthest-side</div>
</div>
<div class="line">
<span>ellipse</span>
<div class="ellipse1">方向值偏移</div>
<div class="ellipse2">closest-side</div>
<div class="ellipse3">farthest-side</div>
</div>
<div class="line">
<span>polygon</span>
<div class="polygon1">对话框</div>
</div>
<div class="line">
<span>path</span>
<div class="path1">五角星</div>
</div>
</body>
</html>

第三步:inset 裁矩形与圆角

inset() 的参数按“上、右、下、左”的顺序省略,和 margin 的简写规则一致。

一个参数表示整体内缩:

1
clip-path: inset(30px);

两个参数表示上下、左右:

1
clip-path: inset(30px 50px);

三个参数表示上、左右、下:

1
clip-path: inset(100px 30px 50px);

四个参数表示上、右、下、左:

1
clip-path: inset(20px 89px 50px 40px);

round 用来把角变成弧线,注意 『round 不能在第一位,round 后面必须跟值,使用 round 不能输入负值』。

叶子:整体内缩 10px,round 后跟两个值,分别对应左上右下角和右上左下角:

1
clip-path: inset(10px round 152px 5px);

扇贝:round 后跟三个值,依次对应左上角、右上左下角、右下角:

1
clip-path: inset(10px round 20px 10px 150px);

圆角矩形:前面四个值裁出矩形,round 后的值表示四个角的弧线大小:

1
clip-path: inset(10px 20px 10px 30px round 20px);

第四步:circle 裁圆

  • 参数一:可以是数值,也可以是 closest-side 和 farthest-side
  • 参数二:at 关键字,控制圆心位置,默认在中间,有 x 轴、y 轴,后面可以是数值也可以是方向值

closest-side 与 farthest-side 的区别:

  • 使用 closest-side 时,裁剪出的圆会尽量小,以确保它不会超出矩形的边界。
  • 使用 farthest-side 时,裁剪出的圆会尽量大,以确保它能够覆盖到矩形的边界。

第五步:ellipse 裁椭圆

  • 参数一:左右半径
  • 参数二:上下半径
  • 其它参数:at 关键字控制圆心位置,默认居中
  • closest-side 与 farthest-side 的区别跟 circle 一样

第六步:polygon 裁多边形

以逗号分隔,格式为 x1 y1 一组,最少三组,每一组都以原物体的左上角为原点,按照顺序依次连接。

可以借助 https://bennettfeely.com/clippy/ 拖拽出想要的图形,然后按顺序把 x、y 写进去。

第七步:path 裁任意形状

path() 接收一段 SVG 路径,可以通过 GPT 生成一段 SVG 五角星的路径。

踩坑与注意点

  • round 只能出现在 inset() 的偏移量之后,写在第一位会整条失效,且不接受负值。
  • inset() 的四个偏移量顺序是“上右下左”,与 border-radius 的四角顺序不同,容易写反。
  • clip-path 不改变布局,裁掉的部分仍然占据原来的空间。
  • path() 使用固定坐标,元素尺寸变化时图形不会跟着缩放,需要按实际尺寸调路径。
  • circle() 的 closest-side 在宽高不等的元素上会取较短的一边,所以示例里的圆可能比预期小。
其他文章
cover
CSS-滤镜篇
  • 24/10/30
  • 15:29
  • CSS&HTML
目录导航 置顶
  1. 1. 背景:clip-path 是什么
  2. 2. 第一步:了解 clip-path 的取值
  3. 3. 第二步:跑一个完整示例
  4. 4. 第三步:inset 裁矩形与圆角
  5. 5. 第四步:circle 裁圆
  6. 6. 第五步:ellipse 裁椭圆
  7. 7. 第六步:polygon 裁多边形
  8. 8. 第七步:path 裁任意形状
  9. 9. 踩坑与注意点
请输入关键词进行搜索