搞定clip-path属性[专栏2]

本文详细介绍了CSS3的clip-path属性,用于定义元素的可视区域。通过circle、ellipse、inset等专有名词可以轻松创建简单图形,如圆形、椭圆和圆角矩形。同时,文章提供了多个实例,包括五角星、菱形、X形状和三角形等复杂图形的绘制方法,展示了clip-path的灵活性和实用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、clip-path作用

属性使用裁剪方式创建元素的可显示区域。区域内的部分显示,区域外的隐藏。兼容性页蛮高的。

-webkit-clip-path解决兼容性问题

clip-path可以通过专有名词属性值很容易的画出一些简单的图形,例如圆(circle),椭圆(ellipse),圆角矩形(inset):

绘制圆形设置悬停后,按钮变大的效果案例

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <style>

    .div1 {

        width: 320px;

        height: 320px;

        border: 1px solid red;

        background: url(./yuanquan.png) no-repeat;

        /* background: url(./flower.jpg) no-repeat; */

        background-size: cover;

        /* 裁剪出圆形区域 */

         /* clip-path: circle(40px at 100px 100px); */

        clip-path: circle(40px at center);

        /* 控制变化时间 */

        transition: clip-path .8s;

    }

    .div1:hover{

         /* clip-path: circle(80px at 100px 100px); */

        clip-path: circle(80px at center);

    }

    </style>

</head>

<body>

    <div class="div1"></div>

</body>

</html>

注意1使用clip-path要从同一个方向绘制,如果顺时针绘制就一律顺时针,逆时针就一律逆时针,因为polygon是一个连续的线段,若线段彼此有交集,面积区域就会有相减的状况发生(当然如果这是你要的效果就无妨了)。

注意2如果绘制时采用「比例」的方式绘制,长宽就必须要先行设定,不然有可能绘制出来的长宽和我们想像的就会有落差,使用「像素」绘制就没有这种问题。

二、案例

div.item_cont .item:nth-child(7){

   background: #00CCCE;

   clip-path:circle(50% at 50% 50%);

}

椭圆ellipse,

div.item_cont .item:nth-child(8){

   clip-path:ellipse(25% 50% at 50% 50%);

   background: #C4C4C4;

}

圆角矩形

div.item_cont .item:nth-child(9){

   clip-path:inset(20% 0 round 0 20%);

   background: #9A99FF;

}

以下使用polygon绘制

五角星1

div{

   width: 200px;

   height: 200px;

   background: url(../jsbase/瀑布流/picture/9.jpg) no-repeat center / cover;

   margin: 10px;

   clip-path: polygon(100% 39%,66% 32%,50% 3%,32% 32%,0 39%,24% 64%,19% 97%,50% 84%,81% 97%,76% 64%);

}

五角星2正五角星

div.item_cont .item:nth-child(6){

   clip-path:polygon(50% 0%, 63% 38%, 100% 38%, 69% 59%, 82% 100%, 50% 75%, 18% 100%, 31% 59%, 0 38%, 37% 38%);

   background: url(../jsbase/瀑布流/picture/8.jpg) no-repeat center / cover;

}

菱形

div.item_cont .item:nth-child(4){

   clip-path: polygon(100% 50%, 50% 0,0 50%,50% 100%);

   background: url(../jsbase/瀑布流/picture/6.jpg) no-repeat center / cover;

}

X形状

div.item_cont .item:nth-child(5){

   clip-path:polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);

   background: url(../jsbase/瀑布流/picture/1.jpg) no-repeat center / cover;

   animation-duration: 2s;

}        

三角形

div.item_cont .item:nth-child(1){

   clip-path: polygon(100% 100%,50% 0,0 100%);

   background: url(../yuan.jpg) no-repeat center / cover;

   animation-duration: 1s;

   clip-path: rec;

}

div.item_cont .item:nth-child(2){

   clip-path: polygon(100% 0,0 100%,100% 100%);

   background: url(../yuan.jpg) no-repeat center / cover;

   animation-duration: 4s;

}

提示框

div.item_cont .item:nth-child(10){

   clip-path:polygon(0% 0%, 100% 0%, 100% 75%, 75% 75%, 75% 100%, 50% 75%, 0% 75%);

   background: url(../jsbase/瀑布流/picture/1.jpg) no-repeat center / cover;

   animation-duration: 1.2s;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大怪冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值