margin-left 和 margin-right 同时设为负值的新认知

之前对margin的理解,都是用来改变容器位置的,今天发现在特定情况下 ,margin也可以改变容器的大小。

当不指定容器的宽度(这里指的是具体值和百分比),即容器的宽度为width: auto时,同时给该容器的margin-left和margin-right设置一个负值,可以增大容器的宽度。(垂直方向上不是同理的)

案例代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <link rel="stylesheet" href="style.css" />
        <style>
            .wrap {
                width: 500px;
                height: 500px;
                background: #ccc;
                margin: 0 auto;             
            }           
            .test {
                height: 50px;
                border: 1px solid #0000FF;
            }
            .test:hover {
                margin-left: -20px;
                margin-right: -20px;
            }
            .icon-color {
                color: blue;
            }
        </style>
    </head>
    <body class="icon-color">
        <div class="wrap">
            <div class="test">子级</div>
            父级
        </div>
    </body>
</html>

效果图:
1222368-20181114170442534-2128731372.png

1222368-20181114170541723-1173594997.png

具体原理还不懂,学无止境,继续加油......

参考文章:https://www.jianshu.com/p/549aaa5fabaa

转载于:https://www.cnblogs.com/chaoyueqi/p/9958925.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值