了解sass

举报
江一铭的博客 发表于 2021/11/23 22:39:39 2021/11/23
3.4k+ 0 0
【摘要】 一、变量 所有变量以$开头 $font_size: 12px; .container{ font-size: $font_size; } 1234 如果变量嵌套在字符串中,需要写在#{}中 ...

一、变量

所有变量以$开头

$font_size: 12px;
.container{
    font-size: $font_size;
}

  
 

如果变量嵌套在字符串中,需要写在#{}中

$side : left;
.rounded {
    border-#{$side}: 1px solid #000;
}
  
 

二、嵌套

层级嵌套

.container{
    display: none;
    .header{
        width: 100%;
    }
}

  
 

属性嵌套,注意,border后需要加上冒号:


```javascript
.container {
    border: {
        width: 1px;
    }
}

  
 

可以通过&引用父元素,常用在各种伪类

.link{
    &:hover{ 
        color: green;
    }  
}

  
 

三、mixin
简单理解,是可以重用的代码块,通过@include 命令

// mixin
@mixin focus_style {
    outline: none;
}
div {
    @include focus_style; 
}

  
 

编译后生成

div {
  outline: none; }

  
 

还可指定参数、缺省值

// 参数、缺省值
@mixin the_height($h: 200px) {
        height: $h;
}
.box_default {
        @include the_height;
}
.box_not_default{
        @include the_height(100px);
}

  
 

编译后生成

.box_default {
  height: 200px; }

.box_not_default {
  height: 100px; }
  
 

四、继承

通过@extend,一个选择器可以继承另一个选择器的样式。例子如下

// 继承
.class1{
        float: left;
}
.class2{
        @extend .class1;
        width: 200px;
}

  
 

编译后生成

.class1, .class2 {
  float: left; }

.class2 {
  width: 200px; }
  
 

五、运算

直接上例子

.container{
        position: relative;
        height: (200px/2);
        width: 100px + 200px;
        left: 50px * 2;
        top: 50px - 10px;
}

  
 

编译后生成

.container {
  position: relative;
  height: 100px;
  width: 300px;
  left: 100px;
  top: 40px; }

  
 

插入文件
用@import 来插入外部文件

@import "outer.scss";

  
 

也可插入普通css文件

@import "outer.css";

  
 

自定义函数
通过@function 来自定义函数

@function higher($h){
        @return $h * 2;
}
.container{
        height: higher(100px);
}

  
 

编译后输出

.container {
  height: 200px; }
  
 

注释
两种风格的注释

// 单行注释,编译后消失

  
 
/* 标准的CSS注释,会保留到编译后的代码中 */

  
 

如果重要的注释,压缩编译后还想保留,可在 /* 后面加上 !

/*!
重要注释,压缩编译也不会消失
*/

  
 

文章来源: jiangwenxin.blog.csdn.net,作者:前端江太公,版权归原作者所有,如需转载,请联系作者。

原文链接:jiangwenxin.blog.csdn.net/article/details/108086649

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。