SASS & LESS

1、less

less 是css的一个拓展语言。 也可以叫做CSS的预处理语言。

浏览器不直接支持less(不能直接解析less), 需要把less文档编译成css文档才能使用。

利用less书写页面外观,可以很大程度的提高css的编写速度(提供工作效率)

demo.less

变量语法

嵌套规则

混合语法

....

demo.less

1.1 变量语法
// 注释1

/* 
*
* 注释2
*
* 
*/


/* 
*
* 1) 声明变量
*
* 
*/

@color: #ff5500;
@w: 1000px;
@h: 80px;
@bgColor: #f3e4ca;
@pad: 0 20px;
@borderColor: #ff5500;


.container {
    width: @w;
    margin: 0 auto;
}

/* 
*
* 2) 嵌套规则
*
* 
*/

.index-header {
    height: @h;
    line-height: @h;
    background-color: @bgColor;
    padding: @pad;
    .logo {
        color: @color;
        font-size: 40px;
        font-weight: bold;
    }
}
1.2 嵌套规则
// 现在的做法
.index-nav {
    padding: @pad;
    ul {
        width: 100%;
        height: 80px;
        display: flex;
        align-items: center;
        border-bottom: 2px solid @color;
        li {
            margin: 0 15px;
            a {
                color: @color;
                text-decoration: none;
            }
        }
    }
}
1.3 混合语法
/* 
*
* 3) 混合
*  可以重复使用的代码块
* 
*/

@width: 50px;
@height: 50px;

.size() {
    width: @width;
    height: @height;
}

.base() {
    content: "";
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    border-radius: 50%;
}

.addColor(@c: red) {
    background-color: @c;
}

.icon-heart {
    display: inline-block;
    vertical-align: middle;
    position: relative;
    // 在less 文档 可以直接使用加减乘除 + - * / 
    margin: (@width / 2) (@width / 2);
    transform: rotate(45deg) scale(1);
    .size();
    .addColor(red);

    // 伪类元素
    &::before {
        .base();
        .size();
        .addColor(red);
        left: -(@width / 2);

    }

    &::after {
        .base();
        .size();
        .addColor(red);
        top: -(@width / 2);
    }
}
1.4 函数、循环、拓展语法
/* 
*
* 4) 针对属性值进行操作的函数
*  向上取整  向下取整  
* 
*/
.box {
    width: (1000px / 3);
    width: floor((1000px / 3));
    width: ceil((1000px / 3));
}

ol {
    list-style: none;
    padding: 0;
    margin: 0;
}

// 5) 循环 1 2 3 4 5 6
.loop(@i) when (@i < 7) {
    // less文档中字符串
    .demo-@{i} {
        margin-top: 5px;
        width: 50px + (@i * 50);
        height: 10px;
        background-color: rgba(255,0,0, (@i / 10));
    }
    .loop(@i + 1);
}
// 使用.loop()
.loop(1);
// 字符串拼接
// @i: 1;
// .demo-@{i} {
//     width: 100px;
// }
// 6) 拓展语法 (代码重复使用)
.square {
    width: 100px;
    height: 100px;
    background-color: deepskyblue;
    margin-top: 10px;
}

.circle:extend(.square) {
    border-radius: 20px;
}
2、sass

sass 是css的一个拓展语言。 也可以叫做css的预处理语言。

浏览器不直接支持sass, 需要把scss文档编译成css文档才能使用。

利用scss书写页面外观,可以很大程度的提高css的编写速度

sass 有两个版本

demo.sass 旧版本(语法严格)

demo.scss 新版本

demo.scss

// 注释1
/*注释2*/


/*
*
*
*1) 声明变量
*
*
*/
$color: red;
.box {
    color: $color;
}

/*
*
*
*2) 嵌套规则
*
*
*/
.nav {
    width: 1000px;
    ul {
        list-style: none;
        li {
            border-bottom: 1px solid #ccc;
            a {
                text-decoration: none;
                height: 40px;
                line-height: 40px;
            }
        }
    }
}

/*
*
*
* 3) 混合语法 
*  可重复使用的代码块
*
*/
@mixin size(){
    width: 1000px;
    height: 100px;
}
@mixin addColor($c:red){
    background-color: $c;
}

.header {
    @include size();
    @include addColor();
}
.footer {
    @include size();
    @include addColor(green);
}
.nav {
    @include size();
    @include addColor(blue);
}

// 4) 运算 + - * / 
.image-box {
    width: (256px / 2);
}

// 5) 函数
.text-box {
    width: floor(1000.99999px);
}

// 6) 循环
// 写法1:
@for $i from 1 to 5 {
    // 字符串拼接
    .demo-#{$i}{
        width: 100px + (10px * $i);
    }
}

// 写法2:
@each $key in header nav  footer {
    .#{$key}-demo{
        width: 100px;
    }
}


// 7) 拓展语法
.square {
    width: 100px;
    height: 100px;
    background-color: pink;
}
.circle {
    @extend .square;
    border-radius: 50%;
}

综上所述:

在实际Web开发环境中,为了提高工作效率,可以使用less或sass去编写项目的样式。 其中语法,使用的多的是变量、嵌套规则、混合语法、运算、拓展。

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨桃贝贝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值