clip-path:MDN:clip-path
语法:
/* Keyword values */
clip-path: none;
/* <clip-source> values */
clip-path: url(resources.svg#c1);
/* <geometry-box> values */
clip-path: margin-box;
clip-path: border-box;
clip-path: padding-box;
clip-path: content-box;
clip-path: fill-box;
clip-path: stroke-box;
clip-path: view-box;
/* <basic-shape> values */
clip-path: inset(100px 50px);
clip-path: circle(50px at 0 100px);
clip-path: ellipse(50px 60px at 0 10% 20%);
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
clip-path: path('M0.5,1 C0.5,1,0,0.7,0,0.3 A0.25,0.25,1,1,1,0.5,0.3 A0.25,0.25,1,1,1,1,0.3 C1,0.7,0.5,1,0.5,1 Z');
/* Box and shape values combined */
clip-path: padding-box circle(50px at 0 100px);
/* Global values */
clip-path: inherit;
clip-path: initial;
clip-path: revert;
clip-path: revert-layer;
clip-path: unset;
其中:
polygon()函数:用于定义一个多边形,也可以用来剪裁图形。它的参数是一组坐标对(<shape-arg> <shape-arg>),每一个坐标对代表多边形的一个顶点坐标。浏览器会将最后一个顶点和第一个顶点连接得到一个封闭的多边形。坐标对使用逗号来进行分隔,可以使用绝对单位或百分比单位值。
举例:实现如图所示不规则图形效果,并在不规则区域添加边框显示
实现前:
实现方法:
1.绘制不规则区域,并添加边框以及背景颜色:
<div class="box">
...
</div>
.box{
clip-path: polygon(29% 0%, 31% 15px, 67% 15px, 69% 0%, 100% 0%, 100% 100%, 0% 100%, 0% 0%);
width: 100%;
height: 300px;
border: 1px solid #054689;
background: rgba(6, 52, 105, 0.24);
border-radius: 10px;
position: relative;
}
实现效果:
2.给不规则区域添加边框
<div class="box">
<div class="box-left"></div>
<div class="box-mid"></div>
<div class="box-right"></div>
</div>
.box{
clip-path: polygon(29% 0%, 31% 15px, 67% 15px, 69% 0%, 100% 0%, 100% 100%, 0% 100%, 0% 0%);
width: 100%;
height: 300px;
border: 1px solid #054689;
background: rgba(6, 52, 105, 0.24);
border-radius: 10px;
position: relative;
}
.box-left {
position: absolute;
background: #1a8bff;
width: 1px;
height: 17px;
transform: rotate(146deg);
left: 29.7%;
z-index: 111;
top: -2px;
}
.box-right {
position: absolute;
background: #1a8bff;
width: 1px;
height: 17px;
transform: rotate(-146deg);
right: 31.7%;
z-index: 111;
top: -2px;
}
.box-mid {
position: absolute;
background: #1a8bff;
width: 36.5%;
height: 1px;
right: 32.7%;
z-index: 111;
top: 14px;
}
实现效果: