如何用css画胡子-单标签实现胡子效果

我们最终要实现的效果如下:


你们相信吗?这么一个相对复杂的图形只需要一个div就能够实现。下面就跟我来学习如何实现。


1. 首先写出代码结构,只需要如下一行

<div class="mustache"></div>


2. 通过分析可以发现,这个胡子是左右对称的,因此只需要实现一边的胡子就可以了。

首先画胡子中间的圆

为.mustache添加样式

.mustache{
            width: 100px;
            height: 100px;
            background: black;
            border-radius: 50%;
        }

3. 然后通过伪元素来为胡子添加胡须

        .mustache:before, .mustache:after{
            content: '';
            display: block;
            position: absolute;
            border-bottom: 1px solid;
            transform-origin: right bottom;
            border-radius: 0 0 0 100%;
        }
        .mustache:before{
            width: 106px;
            height: 100px;
            background: white;
            z-index: 2;
            left: 125px;
            top: -8px;
            transform: rotate(-20deg);
        }
        .mustache:after{
            width: 200px;
            height: 100px;
            left: 30px;
            top: -7px;
            background: black;
            transform: rotate(-20deg);
        }


4. 最后通过css3的倒影来画另一边的胡子,也就是box-reflect,这个样式接收两个参数:第一个是倒影的方向,

第二个是倒影的位移。


完成了,是不是很神奇。

最终的代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>纯css画胡子</title>
    <style>
    *{
    margin: 0; padding: 0;
    }
        .mustache{
            width: 100px;
            height: 100px;
            background: black;
            border-radius: 50%;
            transform: translate(220px);
            -webkit-box-reflect: left -20px;
            box-reflect: left -20px
        }
        .mustache:before, .mustache:after{
            content: '';
            display: block;
            position: absolute;
            border-bottom: 1px solid;
            transform-origin: right bottom;
            border-radius: 0 0 0 100%;
        }
        .mustache:before{
            width: 106px;
            height: 100px;
            background: white;
            z-index: 2;
            left: 125px;
            top: -8px;
            transform: rotate(-20deg);
        }
        .mustache:after{
            width: 200px;
            height: 100px;
            left: 30px;
            top: -7px;
            background: black;
            transform: rotate(-20deg);
        }
    </style>
</head>
<body>
    <div class="mustache"></div>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值