CSS3 Boxshadow

本文介绍如何使用CSS的box-shadow属性及before、after伪元素来绘制各种形状,并探讨实现多重边框的方法。文章还对比了不同技术方案的特点。
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>无标题文档</title>
    <style>
    .icon-img {
        display: inline-block;
        position: relative;
        box-sizing: border-box;
        width: 90px;
        height: 80px;
        border: 5px solid;
        border-radius: 10px;
        color: #f00;
        overflow: hidden;
    }
    .icon-img:before,.icon-img:after {
        content: '';
        pointer-events: none;
        position: absolute;
    }
    .icon-img:before {
        width: 10px;
        height: 10px;
        top: 18px;
        right: 20px;
        box-shadow: inset 0 0 0 1em;
        border-radius: 50%;
    }
    .icon-img:after {
        width: 60px;
        height: 50px;
        left: 0;
        bottom: -27px;
        box-shadow: inset 0 0 0 50px,30px -20px 0 0;
        transform: rotate(45deg);
    }
    </style>
    </head>
    <body>
    <div class="icon-img"></div>
    </body>
    </html>

效果如下:
这里写图片描述

利用boxshadow 以及before、after伪元素可实现形状的绘制。
实例:

    <style>
    .contact{width:100%; height:147px; background:#000;position:relative;}
    .contact:before,.contact:after{content: '';pointer-events: none;position: absolute;}
    .contact:before{width:20px; height:20px;background:rgba(204,204,204,.4);-ms-transform: rotate(45deg);-moz-transform: rotate(45deg);-webkit-transform: rotate(45deg);transform: rotate(45deg);right:-10px; top:-10px;}
    .contact:after{width:20px; height:20px;color:#fad699;left: -15px;top: -60px;box-shadow: inset 0 0 0 10px rgba(243,153,0,.2),5px 35px 0 3px ;-ms-transform: rotate(45deg);-moz-transform: rotate(45deg);-webkit-transform: rotate(45deg);transform: rotate(45deg);z-index:-2;}
    </style>
    <body>
      <div class="contact"></contact>
    </body>

效果如下:
这里写图片描述

技巧3:

语法:box-shadow: [x偏移] [y偏移] [阴影模糊宽度] [阴影宽度] [颜色],并且还能添加多个阴影,使用逗号隔开。

这里写图片描述

当然你还可以继续增加,四重边框,五重边框……都不再是问题啦。另外,还能加圆角,阴影会贴紧内层div。

使用这种方法,有一个缺点就是,不支持虚线边框。

解法三: 使用outline(只能支持两重边框)
这里写图片描述
使用这种方法的缺点就是,只能支持两层的边框,而且还不能根据容器的border-radius自动贴合。

来源: https://gold.xitu.io/post/583e97c961ff4b006b662f5e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值