scss : div水平垂直居中

scss 是一个很好用的css预处理语言,有很多很好的特性。

比如 mixin。

我们可以像使用函数那样使用mixin。

比如写一个div水平垂直居中。

上代码。

@mixin absolute_center_mixin($width, $height){
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -($width / 2);
    margin-top: -($height / 2);
    width: $width;
    height: $height;
    background: #000;
}

html{
    body{
        .main{            
            .left{
                position: relative;
                .box{
                    @include absolute_center_mixin(3.106297rem, 1.405564rem);
                }
            }
        }        
    }
}

用vscode的scss live compiler插件可以很方便的编写scss,他会帮你自动编译成css。

以上。

猜你喜欢

转载自www.cnblogs.com/foxcharon/p/10558729.html