less && scss

less

变量(Variables)
@width: 10px;
#header { width: @width;  }


混合(Mixins)
.bordered { border-top: dotted 1px black; border-bottom: solid 2px black; }
#menu a { color: #111; .bordered(); }


嵌套(Nesting)
.clearfix {
  display: block;
  zoom: 1;
  &:after {
    content: " ";
    display: block;
    font-size: 0;
    height: 0;
    clear: both;
    visibility: hidden;
  }
}


@规则嵌套和冒泡
.component {
  width: 300px;
  @media (min-width: 768px) {
    width: 600px;
    @media  (min-resolution: 192dpi) {
      background-image: url(/img/retina2x.png);
    }
  }
  @media (min-width: 1280px) {
    width: 800px;
  }
}
编译为:
.component {
  width: 300px;
}
@media (min-width: 768px) {
  .component {
    width: 600px;
  }
}
@media (min-width: 768px) and (min-resolution: 192dpi) {
  .component {
    background-image: url(/img/retina2x.png);
  }
}
@media (min-width: 1280px) {
  .component {
    width: 800px;
  }
}


运算(Operations)
@base: 5%;
@filler: @base * 2; // 结果是 10%
@var: 50vh/2;
width: calc(50% + (@var - 20px));  // 结果是 calc(50% + (25vh - 20px))

@min768: ~"(min-width: 768px)";
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}
编译为:
@media (min-width: 768px) {
  .element {
    font-size: 1.2rem;
  }
}
注意,从 Less 3.5 开始,可以简写为:

@min768: (min-width: 768px);
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}
在 Less 3.5+ 版本中,许多以前需要“引号转义”的情况就不再需要了。


函数(Functions)
@base: #f04615;
@width: 0.5;

.class {
  width: percentage(@width); // returns `50%`
  color: saturate(@base, 5%);
  background-color: spin(lighten(@base, 25%), 8);
}


映射(Maps)
#colors() {
  primary: blue;
  secondary: green;
}
.button {
  color: #colors[primary];
  border: 1px solid #colors[secondary];
}
输出符合预期:
.button {
  color: blue;
  border: 1px solid green;
}


作用域(Scope)
@var: red;
#page {
  @var: white;
  #header {
    color: @var; // white
  }
}


导入(Importing)
@import "library"; // library.less
@import "typo.css";

 

 

 

scss记录(sass)
 

变量($)  : 
  $primary-color: #333;                  

  body {  color: $primary-color;  }



嵌套 :

 nav {
       ul {
          list-style: none;
        }

         li { display: inline-block; }

a {
    text-decoration: none;

         &:hover{

               cursor:pointer;

                       }
                }
          }

引用父级选择器"&":

a {
  text-decoration: none;
  &:hover { text-decoration: underline; }
}



引入:

// _reset.scss
html, body, ul {
  margin:  0;
  padding: 0;
}

// base.scss

//可以省略后缀名
@import 'reset';   
body {
  background-color: #efefef;
}



混合 :

@mixin border-radius($radius) {
          border-radius: $radius;
}

.box {
  @include border-radius(10px);
}



继承:
%message-common {
  color: #333;
}

.message {
  @extend %message-common;
}

.success {
  @extend %message-common;
  border-color: green;
}



操作符:

article[role="main"] {
  float: left;
  width: 600px / 960px * 100%;
}





嵌套属性:



/*===== SCSS =====*/
.demo {
  // 命令空间后带有冒号:
  font: {
    family: fantasy;
    size: 30em;
  }
}

/*===== CSS =====*/
.demo {
  font-family: fantasy;
  font-size: 30em; }



注释: /* */    //





 插值语句 #{} (Interpolation: #{})   
$name: foo;
$attr: border;
p.#{$name} {
  #{$attr}-color: blue;
}

编译为

p.foo {
  border-color: blue; }




函数指令 (Function Directives)
Sass 支持自定义函数,并能在任何属性值或 Sass script 中使用:

$grid-width: 40px;
$gutter-width: 10px;

@function grid-width($n) {
  @return $n * $grid-width + ($n - 1) * $gutter-width;
}

#sidebar { width: grid-width(5); }


编译为

#sidebar {
  width: 240px; }

 

发布了40 篇原创文章 · 获赞 22 · 访问量 8292
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章