Sass学习笔记

1、使用变量  $

1 $c:center  //声明变量
2 li{align-item:$c}  //使用变量

2、父级选择器  &

 1 <li class="on"><span class='adorn'>
 2  </span></li>
 3 scss:
 4 li{
 5     margin-bottom: 20px;
 6     margin-right: 20px;
 7     display: flex;
 8 &.on{
 9     border-color: #009588;
10     .adorn {
11         display: block;
12        }
13     }
14 }
15 &:hover{ background:blue;}
16 相当于  li.on{}  
3、嵌套规则

  CSS 中重复写选择器是非常恼人的。如果要写一大串指向页面中同一块的样式时,往往需要 一遍又一遍地写同一个 ID 或类名:

1 .aaa div{background:red; }
2 .aaa div h1{font-size:50px;}
3 .aaa div p{ margin-top:20px;}
4 .aaa input{ margin-left:20px;}

  像这种情况,sass 可以让你只写一遍,且使样式可读性更高。在Sass中,你可以像俄罗斯套娃那样在规则块中嵌套规则块。sass 在输出 css 时会帮你把这些嵌套规则处理好,    避免你的重复书写。

 1 .aaa{
 2     div{
 3     background:red;
 4         h1{
 5         font-size:50px;
 6         }
 7         p{
 8         margin-top:20px;
 9         }
10     }
11     input{
12     margin-left:20px;
13     }
14 }

4、混合器@mixin

1 @mixin blend_button{width:100px;height:100px}
2 button{@include blend_button;
3       background_color:yellow}
4 相当于 
5 button{width:100px;height:100px;
6       background_color:yellow}

 给混合器传参

1 @mixin ellipsis($width){
2     max-width:$width
3 }
4 input{
5 @include ellipsis(100px)
6 }

传多个参数

 1 @mixin ellipsis($normal,$hover){
 2     color:$normal;
 3     &:hover:{color:$hover}
 4 }
 5 input{
 6 @include ellipsis(blue,red)
 7 }
 8 // 不用在乎先后顺序的传参
 9 input{
10 @include ellipsis($normal:blue,$hover:red);
11 }

5、深度选择器deep:

  当组件使用了第三方组件库,需要在当前组件控制第三方组件的样式时,使用deep使深样式作用更深

1 /deep/.ivu-form

6、#{} 

  在有引号的文本字符串中使用 #{} 插值语句可以添加动态的值
1 p:before{
2     content:'I ate #{1+2} pies'
3     }

7、 @if  条件语句

 1 $type: monster;
 2 p {
 3   @if $type == ocean {
 4     color: blue;
 5   } @else if $type == matador {
 6     color: red;
 7   }  @else {
 8     color: black;
 9   }
10 }

  @for  循环语句

 1 @for $i from 1 through 3 {
 2   .item-#{$i} { width: 2em * $i; }
 3 }
 4 // 相当于 
 5 .item-1 {
 6   width: 2em; }
 7 .item-2 {
 8   width: 4em; }
 9 .item-3 {
10   width: 6em; }

  @each

1 @each $animal in puma, sea-slug, egret, salamander {
2   .#{$animal}-icon {
3     background-image: url('/images/#{$animal}.png');
4   }
5 }

 

 

posted @ 2021-03-26 17:34  聂丽芳  阅读(58)  评论(0)    收藏  举报