嵌套
font-size: 12px;
color: #333333;
.top {
border: 1px solid red;
&-left {
width: 200px;
}
}
.a {
color: #333333;
font: {
size: 12px;
weight: bold;
};
}
.default%foo {
background: red;
width: 100px;
}
.btn-success {
@extend %foo;
border: 1px solid green;
}
变量
sass 变量的定义规则
变量以美元 ($) 开头,后面跟变量名;
变量名是 “不” 以数字开头的,可包含字母、数字、下划线、横线(连接符);
写法彤 css,即变量名和值之间用冒号 (:) 分割;
变量一定要先定义,后使用;
变量的作用域:
1、局部变量:只能在当前作用域内进行使用;
2、全局变量:
变量值的类型:
sass 支持 6 种主要的数据类型
1、数字:1、2、13、10px
2、字符串:有引号字符串与无引号字符串,“foo” ‘bar’ baz
3、颜色:blue、#000000、rgba(155,0,0,0.5)
4、布尔型:true、false
5、空置:null
6、数组(list):用空格或逗号进行分割,1.5em 1em 0.2em, 1,2,3
7、maps:相当于 JavaScript 的 object (key1:value,key2:value2)
$color: 'red';
$color: 'green' !default;
$layer-index: 10;
$border-width: 3px;
$font-base-family: 'Open Sans', Helvetica, Sans-Serif;
$top-bg-co1or: rgba(255, 147, 29, 0.6);
$block-base-padding: 6px 10px 6px 10px;
$blank-mode: false;
$var: null;
$color-map: (color1:#fa0080, color2:#fbe208, color3:#95d7eb);
$fonts: (serif:"Helvetica Neue", monospace:"Consolas");
.containers {
$font-size: 16px;
font-size: $font-size;
@if $blank-mode {
background-color: #000;
} @else {
background-color: #fff;
}
content: type-of($var);
content: length($var);
color: map-get($color-map, color2);
background: $color;
.footer {
font-size: $font-size;
}
}
.wrap {
font: 18px bold map-get($fonts, "sans");
}
.text {
color: $font-base-color;
}
混入:mixin
1、mixin 是可以重复使用的一组 css 声明。
2、mixin 有助于减少重复代码,只需声明一次,就可在文件中引用。
3、混合指令可以包含所有的 css 规则,绝大部分 Sass 规则,甚至通过参数功能引入变量,输出多样化的样式。
4、使用参数时建议加上 默认值。
@mixin warn {
border: 1px solid blue;
}
@mixin warring-text {
background: red;
.warn-text {
background: green;
}
}
@mixin block-padding($top:0,$right:0,$bottom:0,$left:0) {
padding: {
top: $top;
right: $right;
bottom: $bottom;
left: $left;
};
}
@mixin linear-gradient($direction,$gradients...) {
background-color: nth($gradients, 2);
background-image: linear-gradient($direction, $gradients);
}
.container {
@include warn;
@include warring-text;
@include block-padding($top: 10px, $bottom: 20px, $right: null);
@include linear-gradient(to right, red, green, blue)
}
继承:extend
%important {
font-weight: bold;
font-size: 14px;
}
%alert {
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
font-size: 12px;
@extend %important;
}
.alert-info {
@extend %alert;
background-color: #e6f7ff;
color: #1890ff;
border-color: #91d5ff;
}
sass 运算符:Operations
$theme: 6;
.container {
@if ($theme == 1) {
background: red;
} @else {
background: blue;
}
}
.container2 {
@if ($theme >= 5) {
background: red;
} @else {
background: blue;
}
}
$width: 100;
$height: 100;
$last: false;
$index: 10;
div {
@if ($width > 50 and $height < 300) {
font-size: 16px;
} @else {
font-size: 14px;
}
@if (not ($index > 5)) {
border-color: red;
} @else {
border-color: blue;
}
}
.num {
width: 50+20;
width: 50+20%;
width: 50%+20%;
width: 50+20px;
width: 50px+20px;
width: 10px+10pt;
width: round($number:50) / 2;
width: 10 / 5;
width: (math.div(10, 5));
}
插值表达式
$font-size: 12px;
$line-height: 30px;
p {
font: 12px/30px "Helvetica Neue";
font: #{$font-size}/#{$line-height} "Helvetica Neue";
}
$class-name: dander;
$attr: color;
a.#{$class-name} {
border-#{$attr}: #4a90e1;
}
常见函数的基本使用
.color {
$color: #52c41a;
.p0 {
background: $color;
}
.p1 {
background: lighten($color, 30%);
background: darken($color, 30%);
background: opacify(rgb($color, 0.1), 0.5);
}
}
.string {
&:after {
content: quote(这是里面的内容);
}
background-color: unquote($string: '#F00');
z-index: str-length('sass学习');
}
.math {
z-index: abs($number: -25);
z-index: ceil(5.8);
z-index: max(5, 1, 6, 8, 3);
opacity: random();
}
.list {
z-index: length(12px);
z-index: length(12px 5px 8px);
z-index: index(a b c d, c);
padding: append(10px 20px, 30px);
color: nth($list: red blue green, $n: 2);
background: join(blue red, green yellow);
}
.map {
$font-size: ("small":12px, 'normal':18px, 'large':24px);
$padding: (top:10px, right:20px, bottom:10px, left:30px);
font-size: map-get($font-size, 'normal');
@if map-has-key($padding, 'right') {
padding-right: map-get($padding, 'right');
}
&:after {
content: map-keys($font-size) + " " + map-values($padding)+"";
}
}
sass 流程控制指令:@if @for @each @while
$theme: 'red';
@if ($theme == 'red') {
color: red;
} @else if ($theme == 'blue') {
color: blue;
} @else {
color: green;
}
@mixin triangle($direction:top,$size:30px,$border-color:black) {
width: 0;
height: 0;
display: inline-block;
border-width: $size;
border-#{$direction}-width: 0;
@if ($direction == top) {
border-color: transparent transparent $border-color transparent;
border-style: dashed dashed solid dashed;
} @else if ($direction == right) {
border-color: transparent transparent transparent $border-color;
border-style: dashed dashed dashed solid;
} @else if ($direction == bottom) {
border-color: $border-color transparent transparent transparent;
border-style: solid dashed dashed dashed;
} @else if ($direction == left) {
border-color: transparent $border-color transparent transparent;
border-style: dashed solid dashed dashed;
}
}
.p0 {
@include triangle()
}
.p1 {
@include triangle(right, 20px, red)
}
.p2 {
@include triangle(bottom, 20px, green)
}
.p3 {
@include triangle(left, 20px, yellow)
}
for 循环指令
.for {
@for $i from 1 to 4 {
.p#{$i} {
width: 10px * $i;
height: 30px;
background: red;
}
}
@for $i from 1 through 4 {
.p-#{$i} {
width: 10px * $i;
height: 30px;
background: red;
}
}
@keyframes loading {
0% {
opacity: 0.3;
transform: translateY(0px);
}
50% {
opacity: 1;
transform: translateY(-20px);
}
100% {
opacity: 0.3;
transform: translateY(0px);
}
}
#loading {
position: fixed;
top: 200px;
left: 46%;
span {
position: absolute;
width: 20px;
height: 20px;
background: #3498db;
opacity: 0.5;
border-radius: 50%;
animation: loading 1s infinite ease-in-out;
@for $i from 1 to 6 {
&:nth-child(#{$i}) {
left: 20 * ($i - 1 )+ px;
animation-delay: calc(20 * ($i - 1) / 100) + s;
}
}
}
}
}
each 指令
.each {
p {
width: 10px;
}
$color-list: red green blue yellow orange;
@each $color in $color-list {
$index: index($color-list, $color);
.p#{$index - 1} {
background: $color;
}
}
}
while 指令
.while {
$column: 12;
@while $column > 0 {
.col-sm-#{$column} {
width: calc($column / 12) * 100%;
width: calc($column / 12) * 100#{'%'};
width: unquote($string: calc($column / 12) * 100 + '%');
}
}
$column: $column - 1;
}
function 的使用
function 的使用,把一些比较复杂或者经常用到的内容进行抽离(封装),以便重复调用
混入 mixin 和函数 function 的区别:
1、混入 mixin 主要是通过传递参数的方式输出多样化的样式,为了可以实现代码服用。
2、函数的功能主要是通过传递参数后,经过函数内部的计算,最后 @return 输出一个值。
.function {
.func1 {
@function background-linear-gradient($direction,$start-color,$end-color:blue) {
@return linear-gradient($direction, $start-color, $end-color)
}
// todo 正常传参
background-image: background-linear-gradient(to right, blue, green);
background: background-linear-gradient(to right, blue);
background: background-linear-gradient($start-color: red, $direction: to right);
}
.func2 {
@function background-linear-gradient($direction,$gradients...) {
@return linear-gradient($direction, $gradients)
}
// todo 正常传参
background-image: background-linear-gradient(to right, blue, green, red);
}
}
三元表达式 if 的使用
用法:if($condition,$if-true,$if-false)
判断 $condition,如果条件成立,则返回 $if-true,如果条件不成立,则返回 $if-false 的结果
.if {
$theme: 'lignt';
color: if($theme == 'lignt', red, green);
}