css——实现正六边形的两种方法

注意:宽度可以随便定义,高度是要根据宽度计算出来的。
公式:height = width*cos(30°)*2
在这里插入图片描述
以下两种方法都可以实现。
【第一种】使用div+border,通过伪元素来实现

<style>
  .block{
            width: 100px;
            height: 173.2px;
            background-color: skyblue;
            position:relative;
            margin: 0 auto;
        }
        .block::after{
            position: absolute;
            content: '';
            left: 0;
            top: 0;
            width: 0;
            height: 0;
            border: 50px solid transparent;
            border-top-width: 86.6px;
            border-bottom-width: 86.6px;
            border-right-color: red;
            transform: translateX(-100%);
        }
        .block::before{
            position: absolute;
            content: '';
            left: 0;
            top: 0;
            width: 0;
            height: 0;
            border: 50px solid transparent;
            border-top-width: 86.6px;
            border-bottom-width: 86.6px;
            border-left-color: red;
            transform: translateX(100%);
        }
</style>

<body>
    <div class="block"></div>
</body>

效果图如下:
在这里插入图片描述
【第二种】
通过css3中的旋转来实现。

<style>
.block2{
            width: 100px;
            height: 173.2px;
            background-color: skyblue;
            position:relative;
            margin: 100px;
        }
        .block2::after{
            position: absolute;
            content: '';
            width: 100px;
            height: 173.2px;
            background-color: pink;
            /* z-index: 1; */
            transform: rotate(60deg);
        }
        .block2::before{
            position: absolute;
            content: '';
            width: 100px;
            height: 173.2px;
            background-color: rgb(123, 203, 147);
            /* z-index: 1; */
            transform: rotate(-60deg);
        }
</style>

<body>
    <div class="block2"></div>
</body>

效果图如下:
在这里插入图片描述
希望这篇文章对你有所帮助哦~

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值