clip-path属性(CSS)

2020-01-31  本文已影响0人  WhiteStruggle

MDN

属性可以创建一个只有元素的部分区域可以显示的剪切区域。区域内的部分显示,区域外的隐藏。剪切区域是被引用内嵌的URL定义的路径或者外部svg的路径,或者作为一个形状例如circle().

clip-path属性代替了现在已经弃用的剪切 clip属性

取值:

裁切函数有后面几种,circle()、ellipse()、inset()和polygon()。

/* 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: 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: unset;

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
           .main{
               position: absolute;
               width: 500px;
               height: 500px;
               background-color:#999;
               clip-path: circle(40%);
               /* clip-path: ellipse(300px 400px at 0 0 ) ; */
               position: relative;
               border: 5px red solid;
           }
           .main:hover{
               background: red;
           }
           img{
               position: absolute;
               top: 0;left: 0;right: 0;bottom: 0;
               margin: auto;
           }
    </style>
</head>
<body>
    <div class="main">
        <img src="Taobao_128px_1230507_easyicon.net.ico" alt="">
    </div>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读