clip-path可截取不规则图形
<div
class="panel-container"
style="width: 300px;height: 300px;margin: 50px auto;">
</div>
通过加背景色,先画外圈,再画内圈,实现只留有边框长度的样式
<style lang="scss" scoped>
$--cornerGap: 16px; /* 角落空隙长度,(宽高的长度 不是斜边的长度) */
$--border: 1px; /* 边框 */
.panel-container {
position: relative;
margin: 16px;
padding: 16px;
}
.panel-container::before{
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #3972b0;
clip-path: polygon(
0 0,
calc(100% - $--cornerGap) 0,
100% $--cornerGap,
100% calc(100% - $--cornerGap),
calc(100% - $--cornerGap) 100%,
$--cornerGap 100%,
0 calc(100% - $--cornerGap),
0 0,
$--border 0,
$--border calc(100% - $--cornerGap),
$--cornerGap calc(100% - $--border),
calc(100% - $--cornerGap) calc(100% - $--border),
calc(100% - $--border) calc(100% - $--cornerGap),
calc(100% - $--border) $--cornerGap,
calc(100% - $--cornerGap) $--border,
$--border $--border,
);
}
</style>
更加复杂的边框
$--cornerGap: 16px; /* 角落空隙长度(宽高的长度,不是斜边的长度) */
$--border: 1px; /* 边框 */
$--concaveLength: 88px; /* 凹陷长度,外侧长度,且不包含gap */
$--concaveBottomLength: 148px; /* 底部凹陷长度,外侧长度,且不包含gap */
$--slopeWidth: 8px; /* 凹陷斜角长度(宽高的长度,不是斜边的长度) */
$--gap: 2px; /* 凹陷内容和边框线的距离 */
.panel-container {
position: relative;
margin: 16px;
padding: 16px;
}
.panel-container::before{
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #3972b0;
clip-path: polygon(
0 0,
calc(100% - $--cornerGap) 0,
100% $--cornerGap,
100% calc(50% - $--concaveLength/2 - $--gap),
calc(100% - $--slopeWidth) calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
calc(100% - $--slopeWidth) calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
100% calc(50% + $--concaveLength/2 + $--gap),
100% calc(100% - $--cornerGap),
calc(100% - $--cornerGap) 100%,
calc(50% + $--concaveBottomLength/2 + $--gap) 100%,
calc(50% + $--concaveBottomLength/2 + $--gap - $--slopeWidth) calc(100% - $--slopeWidth),
calc(50% - $--concaveBottomLength/2 - $--gap + $--slopeWidth) calc(100% - $--slopeWidth),
calc(50% - $--concaveBottomLength/2 - $--gap) 100%,
$--cornerGap 100%,
0 calc(100% - $--cornerGap),
0 calc(50% + $--concaveLength/2 + $--gap),
$--slopeWidth calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
$--slopeWidth calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
0 calc(50% - $--concaveLength/2 - $--gap),
0 0,
$--border 0,
$--border calc(50% - $--concaveLength/2 - $--gap),
calc($--slopeWidth + $--border) calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
calc($--slopeWidth + $--border) calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
$--border calc(50% + $--concaveLength/2 + $--gap),
$--border calc(100% - $--cornerGap),
$--cornerGap calc(100% - $--border),
calc(50% - $--concaveBottomLength/2 - $--gap) calc(100% - $--border),
calc(50% - $--concaveBottomLength/2 - $--gap + $--slopeWidth) calc(100% - $--slopeWidth - $--border),
calc(50% + $--concaveBottomLength/2 + $--gap - $--slopeWidth) calc(100% - $--slopeWidth - $--border),
calc(50% + $--concaveBottomLength/2 + $--gap) calc(100% - $--border),
calc(100% - $--cornerGap) calc(100% - $--border),
calc(100% - $--border) calc(100% - $--cornerGap),
calc(100% - $--border) calc(50% + $--concaveLength/2 + $--gap),
calc(100% - $--slopeWidth - $--border) calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
calc(100% - $--slopeWidth - $--border) calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
calc(100% - $--border) calc(50% - $--concaveLength/2 - $--gap),
calc(100% - $--border) $--cornerGap,
calc(100% - $--cornerGap) $--border,
$--border $--border,
);
}
每条边框上加装饰 background-image可同时添加多个图片
<style lang="scss">
$--cornerGap: 16px; /* 角落空隙长度(宽高的长度,不是斜边的长度) */
$--border: 1px; /* 边框 */
$--concaveLength: 88px; /* 凹陷长度,外侧长度,且不包含gap */
$--concaveBottomLength: 148px; /* 底部凹陷长度,外侧长度,且不包含gap */
$--slopeWidth: 6px; /* 凹陷斜角长度(宽高的长度,不是斜边的长度) */
$--gap: 4px; /* 凹陷内容和边框线的距离 */
.panel-container {
position: relative;
margin: 16px;
padding: 16px;
}
.panel-container::before {
position: absolute;
top: 0;
left: 0;
content: "";
width: 100%;
height: 100%;
background-image:
url("../assets/images/adornborder-left.png"),
url("../assets/images/adornborder-bottom.png"),
url("../assets/images/adornborder-right.png");
background-repeat: no-repeat, no-repeat, no-repeat;
background-position: left center, bottom center, right center;
}
.panel-container::after{
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #3972b0;
clip-path: polygon(
0 0,
calc(100% - $--cornerGap) 0,
100% $--cornerGap,
100% calc(50% - $--concaveLength/2 - $--gap),
calc(100% - $--slopeWidth) calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
calc(100% - $--slopeWidth) calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
100% calc(50% + $--concaveLength/2 + $--gap),
100% calc(100% - $--cornerGap),
calc(100% - $--cornerGap) 100%,
calc(50% + $--concaveBottomLength/2 + $--gap) 100%,
calc(50% + $--concaveBottomLength/2 + $--gap - $--slopeWidth) calc(100% - $--slopeWidth),
calc(50% - $--concaveBottomLength/2 - $--gap + $--slopeWidth) calc(100% - $--slopeWidth),
calc(50% - $--concaveBottomLength/2 - $--gap) 100%,
$--cornerGap 100%,
0 calc(100% - $--cornerGap),
0 calc(50% + $--concaveLength/2 + $--gap),
$--slopeWidth calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
$--slopeWidth calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
0 calc(50% - $--concaveLength/2 - $--gap),
0 0,
$--border 0,
$--border calc(50% - $--concaveLength/2 - $--gap),
calc($--slopeWidth + $--border) calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
calc($--slopeWidth + $--border) calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
$--border calc(50% + $--concaveLength/2 + $--gap),
$--border calc(100% - $--cornerGap),
$--cornerGap calc(100% - $--border),
calc(50% - $--concaveBottomLength/2 - $--gap) calc(100% - $--border),
calc(50% - $--concaveBottomLength/2 - $--gap + $--slopeWidth) calc(100% - $--slopeWidth - $--border),
calc(50% + $--concaveBottomLength/2 + $--gap - $--slopeWidth) calc(100% - $--slopeWidth - $--border),
calc(50% + $--concaveBottomLength/2 + $--gap) calc(100% - $--border),
calc(100% - $--cornerGap) calc(100% - $--border),
calc(100% - $--border) calc(100% - $--cornerGap),
calc(100% - $--border) calc(50% + $--concaveLength/2 + $--gap),
calc(100% - $--slopeWidth - $--border) calc(50% + $--concaveLength/2 + $--gap - $--slopeWidth),
calc(100% - $--slopeWidth - $--border) calc(50% - $--concaveLength/2 - $--gap + $--slopeWidth),
calc(100% - $--border) calc(50% - $--concaveLength/2 - $--gap),
calc(100% - $--border) $--cornerGap,
calc(100% - $--cornerGap) $--border,
$--border $--border,
);
}
</style>