首页前端开发CSScss怎么制作五边形

css怎么制作五边形

时间2023-11-10 11:59:03发布访客分类CSS浏览766
导读:要制作五边形,我们可以利用css中的伪元素:after和:before来实现。首先,我们需要将一个正方形进行旋转,使其成为五边形的形状。 .pentagon { width: 100px; height:...

要制作五边形,我们可以利用css中的伪元素:after和:before来实现。首先,我们需要将一个正方形进行旋转,使其成为五边形的形状。

    .pentagon {
            width: 100px;
            height: 100px;
            position: relative;
            background: blue;
            transform: rotate(36deg);
    }

接着,我们可以利用:before和:after伪元素来完成五边形的制作。分别对应五边形的左上角和右上角。

    .pentagon:before,    .pentagon:after {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 0;
            height: 0;
            border-right: 50px solid blue;
            border-top: 100px solid transparent;
    }
    .pentagon:before {
            transform: rotate(-72deg);
    }
    .pentagon:after {
            transform: rotate(72deg);
    }

这里我们采用了三角形的做法,利用border来实现。因为五边形每个角度为72度,而三角形每个角度为60度,所以我们需要将各个边的角度分配到三角形的三个角上,最终重合成为五边形的形状。

最后,我们需要将五边形的中心点调整到正方形的中心点。我们可以通过margin来完成这一操作。

    .pentagon:before,    .pentagon:after {
            margin-left: -25px;
    }
    .pentagon:before {
            margin-top: -25px;
    }
    .pentagon:after {
            margin-top: -75px;
    }
    

这样,我们就完成了五边形的制作。代码和效果如下:

    div class="pentagon">
    /div>
.pentagon { width: 100px; height: 100px; position: relative; background: blue; transform: rotate(36deg); } .pentagon:before, .pentagon:after { content: ""; position: absolute; top: 0; left: 0; width: 0; height: 0; border-right: 50px solid blue; border-top: 100px solid transparent; } .pentagon:before { transform: rotate(-72deg); margin-top: -25px; margin-left: -25px; } .pentagon:after { transform: rotate(72deg); margin-top: -75px; margin-left: -25px; }

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: css怎么制作五边形
本文地址: https://pptw.com/jishu/533047.html
css 分页滚动到边界的时候模糊小时 css怎么制作一个圆角按钮

游客 回复需填写必要信息