原来你是这样的CSS(绘制图形)

234 阅读5分钟

image.png

矩形与圆角按钮

正常而言,我们遇到的按钮就这两种 -- 矩形和圆角:

image.png

它们非常的简单,宽高和圆角和背景色。

<div class='btn rect'>rect</div>
<div class='btn circle'>circle</div>
.btn {
    margin8px auto;
    flex-shrink0;
    width160px;
    height64px;
}
.rect {
    background#f6ed8d;
}

.circle {
    border-radius64px;
    background#7de3c8;
}

梯形与平行四边形

使用 transform: skewX() 即可,注意上述说的,利用元素的伪元素实现平行四边形,做到不影响内部的文字。

<div class='btn parallelogram'>Parallelogram</div>
.parallelogram {
    position: relative;
    width160px;
    height64px;

    &::before {
        content"";
        position: absolute;
        top0;
        left0;
        bottom0;
        right0;
        background#03f463;
        transformskewX(15deg);
    }
}

image.png 如果不想使用伪元素,除了 transform: skewX(),平行四边形使用渐变也是可以实现的。

大概就是这样:

{
    backgroundlinear-gradient(45deg, transparent 22%#04e6fb 22%#9006fb 78%, transparent 0);
}

梯形比平行四边形稍微复杂一点,它多借助了 perspective,其实是利用了一定的 3D 变换。

使用  perspective 和 transform: rotateX() 即可,当然,它们可以合在一起写:

<div class='btn trapezoid'>Trapezoid</div>
.parallelogram {
    position: relative;
    width160px;
    height64px;

    &::after {
          content:"";
          position: absolute;
          top0right0bottom0left0;
          transformperspective(40pxrotateX(10deg);
          transform-origin: bottom;
          background#ff9800;
    }
}

image.png

切角 -- 纯色背景与渐变色背景

接下来是切角图形,最常见的方法主要是借助渐变 linear-gradient 实现,来看这样一个图形

<div class="notching">notching</div>
.notching {
    background: 
        linear-gradient(135deg, transparent 10px#ff1493 0) top left,
        linear-gradient(-135deg, transparent 10px#ff1493 0) top right,
        linear-gradient(-45deg, transparent 10px#ff1493 0) bottom right,
        linear-gradient(45deg, transparent 10px#ff1493 0) bottom left;
    background-size50% 50%;
    background-repeat: no-repeat;
}

image.png

利用 clip-path 实现渐变背景的切角图形

当然,这个技巧有个问题,当要求底色是渐变色的时候,这个方法就比较笨拙了。

好在,我们还有另外一种方式,借助 clip-path 切出一个切角图形,这样,背景色可以是任意定制的颜色,无论是渐变还是纯色都不在话下:

<div class="clip-notching">notching</div>
.clip-notching {
    backgroundlinear-gradient(
        45deg,
        #f9d9e7,
        #ff1493
    );
    clip-pathpolygon(
        15px 0,
        calc(100% - 15px0,
        100% 15px,
        100% calc(100% - 15px),
        calc(100% - 15px100%,
        15px 100%,
        0 calc(100% - 15px),
        0 15px
    );
}

简单的实现一个渐变背景,接着核心就是,在渐变矩形图形的基础上,利用 clip-path: polygon() 切出我们想要的形状(一个 8 边形):

image.png

箭头按钮

接下来是箭头按钮,仔细观察上面的切角按钮,当两边的角被切掉的足够多的时候,就变成了一个箭头的形状。

我们可以利用两重渐变,实现一个单箭头按钮:

<div class="arrow">arrow</div>
&.arrow {
    backgroundlinear-gradient(
                -135deg,
                transparent 22px,
                #04e6fb 22px,
                #65ff9a 100%
            )
            top right,
        linear-gradient(
                -45deg,
                transparent 22px,
                #04e6fb 22px,
                #65ff9a 100%
            )
            bottom right;
    background-size100% 50%;
    background-repeat: no-repeat;
}

一个箭头就出来了:

image.png 那如果是这样一个箭头造型呢?

image.png

{
    backgroundlinear-gradient(45deg#04e6fb#65ff9a);
    clip-pathpolygon(
        0 0,
        30px 50%,
        0 100%,
        calc(100% - 30px100%,
        100% 50%,
        calc(100% - 30px0
    );
}

内切圆角

image.png 所以,只需控制下 background-size,在 4 个角实现 4 个这样的图形即可:

<div class="inset-circle">inset-circle</div>
&.inset-circle {
    background-size70% 70%;
    background-imageradial-gradient(
            circle at 100% 100%,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        ),
        radial-gradient(
            circle at 0 0,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        ),
        radial-gradient(
            circle at 100% 0,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        ),
        radial-gradient(
            circle at 0 100%,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        );
    background-repeat: no-repeat;
    background-position: right bottom, left top, right top, left bottom;
}

借助 mask 实现渐变的内切圆角按钮

如果背景色要求渐变怎么办呢?

假设我们有一张矩形背景图案,我们只需要使用 mask 实现一层遮罩,利用 mask 的特性,把 4 个角给遮住即可。

mask 的代码和上述的圆角切角代码非常类似,简单改造下即可得到渐变的内切圆角按钮:

<div class="mask-inset-circle">inset-circle</div>
.mask-inset-circle {
    backgroundlinear-gradient(45deg#2179f5#e91e63);
    maskradial-gradient(
            circle at 100% 100%,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        ),
        radial-gradient(
            circle at 0 0,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        ),
        radial-gradient(
            circle at 100% 0,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        ),
        radial-gradient(
            circle at 0 100%,
            transparent 0,
            transparent 12px,
            #2179f5 13px
        );
    mask-repeat: no-repeat;
    mask-position: right bottom, left top, right top, left bottom;
    mask-size70% 70%;
}

这样,我们就得到了这样一个图形:

image.png

圆角不规则矩形

image.png

<div class="skew">Skew</div>
.skew {
    position: relative;
    width120px;

    &::after {
        content"";
        position: absolute;
        top0;
        left0;
        right0;
        bottom0;
        border-radius10px;
        background: orange;
        transformskewX(15deg);
    }
    &::before {
        content"";
        position: absolute;
        top0;
        right: -13px;
        width100px;
        height64px;
        border-radius10px;
        background: orange;
    }
}

外圆角按钮

image.png

<div class="outside-circle">outside-circle</div>
.outside-circle {
    position: relative;
    background#e91e63;
    border-radius10px 10px 0 0;

    &::before {
        content"";
        position: absolute;
        width20px;
        height20px;
        left: -20px;
        bottom0;
        background#000;
        background:radial-gradient(circle at 0 0, transparent 20px#e91e63 21px);
    }
    &::after {
        content"";
        position: absolute;
        width20px;
        height20px;
        right: -20px;
        bottom0;
        background#000;
        background:radial-gradient(circle at 100% 0, transparent 20px#e91e63 21px);
    }
}

合抱之木生于毫末,九尺之台起于累土。对于技术的提升,我们要有足够的耐心,去一点一点的积累,等到一定程度,必然会有质变。毕竟事需缓图,欲速不达也。