sass的常用的一些语法介绍

本文详细介绍了Sass中嵌套规则、属性嵌套、变量使用、混合指令(@mixin)与扩展(@extend)的实用技巧,以及它们如何简化CSS编写并提升代码复用。特别关注了@extend在保持简洁性方面的优势。
摘要由CSDN通过智能技术生成

sass一些常用的规则

1、嵌套规则

.box{
    p{
        font-size:12px
    }
}

2、父选择器 &

  div {
    font-weight: bold;
    text-decoration: none;
    &:hover { text-decoration: underline; }
    a {
        font-weight: bold;
        &:hover { color: red; }
    }
  }

//编译成
div { font-weight: bold; text-decoration: none; }

div:hover { text-decoration: underline; }

div a { font-weight: bold; }

div a:hover { color: red; }

3、 属性嵌套

.funky {
  font: {
    family: fantasy;
    size: 30em;
    weight: bold;
  }
}

//编译为
.funky { 
    font-family: fantasy; 
    font-size: 30em; 
    font-weight: bold; }

4、变量 $

$width: 10px
#main {
   width: $width;
}

//编译为
#main { width: 10px; }

5、混合指令@mixin

//不带参数
@mixin button {
    width:30px;
    height:15px;
    border-radius: 4px;
    font-size:14px;
    line-height:15px;
    text-align: center; 
  }
.btn_success {
    background:green;
    @include button;
  }
.btn_err {
    background:red;
    @include button;
  }
.btn_warning {
    background:yellow;
    @include button;
  }
.btn_default {
    background:#999;
    @include button;
  }

//编译为
.btn_success { background: green; width: 30px; height: 15px; border-radius: 4px; font-size: 14px; line-height: 15px; text-align: center; }

.btn_err { background: red; width: 30px; height: 15px; border-radius: 4px; font-size: 14px; line-height: 15px; text-align: center; }

.btn_warning { background: yellow; width: 30px; height: 15px; border-radius: 4px; font-size: 14px; line-height: 15px; text-align: center; }

.btn_default { background: #999; width: 30px; height: 15px; border-radius: 4px; font-size: 14px; line-height: 15px; text-align: center; }

//@mixin编译出来的CSS实在是糟糕,因为编译出来的CSS样式,没有把相同的样式合并在一起

//带参数
@mixin absolute($t: 0, $l: 0, $w: auto, $h: auto) {
    top: $t;
    left: $l;
    width: $w;
    height: $h;
    position: absolute;
  }
  
.box{
    @include absolute(10px, 20px, 100px, 100px)
}

//编译为
.box { top: 10px; left: 20px; width: 100px; height: 100px; position: absolute; }

@extend

.button {
    width:30px;
    height:15px;
    border-radius: 4px;
    font-size:14px;
    line-height:15px;
    text-align: center; 
  }
.btn_success {
    background:green;
    @extend .button;
  }
.btn_err {
    background:red;
    @extend .button;
  }
.btn_warning {
    background:yellow;
    @extend .button;
  }
.btn_default {
    background:#999;
    @extend .button;
  }

  //编译后
  .button, .btn_success, .btn_err, .btn_warning, .btn_default { width: 30px; height: 15px; border-radius: 4px; font-size: 14px; line-height: 15px; text-align: center; }

.btn_success { background: green; }

.btn_err { background: red; }

.btn_warning { background: yellow; }

.btn_default { background: #999; }


//1、相比@mixin编译,@extend编译后更简洁,@extend把相同样式都合并到了一起。
//2、如果我们在HTML中从来不使用.button,其存在的唯一目的就是为了扩展吗?这似乎比我们需要的基本样式稍大些,因为我们将永远不会使用.button对应的样式

%foo占位符

%button {
    width:30px;
    height:15px;
    border-radius: 4px;
    font-size:14px;
    line-height:15px;
    text-align: center; 
  }
.btn_success {
    background:green;
    @extend %button;
  }
.btn_err {
    background:red;
    @extend %button;
  }
.btn_warning {
    background:yellow;
    @extend %button;
  }
.btn_default {
    background:#999;
    @extend %button;
  }

//编译后
  .btn_success, .btn_err, .btn_warning, .btn_default { width: 30px; height: 15px; border-radius: 4px; font-size: 14px; line-height: 15px; text-align: center; }

.btn_success { background: green; }

.btn_err { background: red; }

.btn_warning { background: yellow; }

.btn_default { background: #999; }

@media和#foo占位符

使用@extend调用定义好的选择器占位符%foo有所限制,他不能在不同的@media中运行。

@media screen {
  .text3 {
    @extend %small-text;
  }
}

%small-text {
  font: {
    family: arial;
    size: 10px;
    weight: bold;
  }

  color: #f00;
}

//编译时报错
Compilation Error
Error: You may not @extend an outer selector from within @media.
       You may only @extend selectors within the same directive

修改为:

@media screen {
  %small-text {
    font: {
      family: arial;
      size: 10px;
      weight: bold;
    }

    color: #f00;
  }
}

.text3 {
  @extend %small-text;
}


//编译为
@media screen { .text3{ font-family: arial; font-size: 10px; font-weight: bold; color: #f00; } }

@extend和@include都具有强大的功能,尽管细节上有一些差别,这就要问你自己,编译出来的CSS样式,接近重用的样式对你是不是很重要。在某些情况下,@extend可以大大的减化你的CSS输出,并且显著的提高你的CSS性能。

资料:
https://www.sass.hk/docs/#t7-3
https://www.w3cplus.com/preprocessor/understanding-placeholder-selectors.html

SCSSSass)是一种 CSS 预处理器,它扩展了 CSS 语言,提供了许多有用的功能,例如变量、嵌套、函数、继承等,使得样式表更加简洁、易于维护。SCSS 可以通过编译器将 SCSS 代码编译成普通的 CSS 代码,然后在浏览器中使用。 SCSS常用语法有以下几种: 1. 变量 使用 $ 符号定义变量,例如: ``` $primary-color: #3498db; a { color: $primary-color; } ``` 2. 嵌套 使用嵌套可以使样式表更加清晰易读,例如: ``` nav { ul { margin: 0; padding: 0; list-style: none; li { display: inline-block; a { text-decoration: none; } } } } ``` 3. 混合 使用混合可以将一组属性集合起来,方便复用,例如: ``` @mixin button($bg-color) { background-color: $bg-color; color: #fff; padding: 10px 20px; border: none; border-radius: 5px; } .btn-primary { @include button(#3498db); } .btn-success { @include button(#2ecc71); } ``` 4. 继承 使用继承可以让一个选择器继承另一个选择器的样式,例如: ``` .btn { padding: 10px 20px; border: none; border-radius: 5px; } .btn-primary { @extend .btn; background-color: #3498db; color: #fff; } .btn-success { @extend .btn; background-color: #2ecc71; color: #fff; } ``` 5. 条件语句 使用条件语句可以根据不同的条件设置不同的样式,例如: ``` $primary-color: #3498db; .btn { padding: 10px 20px; border: none; border-radius: 5px; @if $primary-color == #3498db { background-color: $primary-color; color: #fff; } @else { background-color: #fff; color: $primary-color; } } ``` 总之,SCSS 是一种非常实用的 CSS 预处理器,可以提高开发效率,让样式表更加易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值