1、sass语法

1.1 css的编译模式

  • css --- 普通
  • sass / scss --- 高效 // *********
  • less --- 高效

1.2 sass介绍

  • 来源: ruby语言
  • 基础的版本,后缀名为sass:没有,只能通过缩进来实现 -- 可读性差、难以维护
// css
.box {width: 100px}
// .sass
.box width: 100px; // 据说是这样
  • .scss 后缀 --- 可读性高、便于维护
html { background: red }
// scss语法--嵌套 --- 权重
.box {
color: blue;
.test {
font-size: 20px;
}
}
// ==》
.box {color: blue;}
.box .test { font-size: 20px;}

1.3 如何使用scss

最终需要使用的是css文件,编写的scss文件

转换工具 gulp / webpack / ruby工具 / 编辑器插件

选用gulp来处理scss文件

2、sass用法

2.1 安装 sass 模块

cnpm i gulp-sass -S (推荐)

cnpm i gulp-sass-china -S

2.2 配置处理scss文件gulp的任务

gulp.task('scss2css', () => {
gulp.src('scss/**/*')
.pipe(concat('main.scss')) // 合并scss文件
.pipe(sass()) // 转为css文件
.pipe(gulp.dest('dist/css'))
.pipe(minifyCss()) // 压缩
.pipe(rename('main.min.css'))
.pipe(gulp.dest('dist/css'))
.pipe(connect.reload())
}) // 在watch中监听
gulp.watch('scss/**/*', ['scss2css'])
// 在build中构建
gulp.task('build', ['copy-html', 'copy-css', 'copy-js', 'copy-data', 'copy-assets', 'scss2css'], () => {
console.log('success')
})

3、学习scss 语法

3.1 学习scss的注释语句

  • 单行注释 --- 推荐使用

使用//来完成单行注释---和js类似
并不会编译成css

  • 多行注释

使用的/* */ 来完成多行注释 --- 和 js类似
先编译成css文件,然后再注释掉css文件

3.2 变量

scss给css赋予了动态语言的特性(变量、函数、条件判断、循环....)

scss对于;很敏感,记住写;

3.2.1 单值变量

// scss
$baseColor: red;
.box {
background-color: $baseColor;
} // ==> css
.box {
background-color: red; }

3.2.2 scss做四则运算

// scss
$baseColor: red;
html {
color: $baseColor;
border: 1px solid $baseColor - 80;
background-color: $baseColor / 2;
background: $baseColor + 200;
} // ==> css
html {
color: red;
border: 1px solid #af0000;
background-color: maroon;
background: #ffc8c8; }

3.2.3 多值变量

多值变量使用 nth(变量名, 索引值) 索引值起始值为1 --- 类似于css中 nth-child

还不如写多个单值变量 --- 并不是 --- 假设需要设定一组button按钮的样式

// scss
$baseColor: red blue;
html {
background: nth($baseColor, 1);
color: nth($baseColor, 2);
}
// ==> css
html {
background: red;
color: blue; }

3.2.4 复杂变量 - 循环

// scss
$list: (top 30px) (right 30px) (bottom 10px) (left 40px);
@each $name, $value in $list{
.box-#{$name} {
width: $value;
}
} // ==>
.box-top {
width: 30px; } .box-right {
width: 30px; } .box-bottom {
width: 10px; } .box-left {
width: 40px; }
// scss
$headers: (h1: 32px, h2: 20px, h3: 14px);
@each $key, $value in $headers {
#{$key} {
font-size: $value;
}
}
// ==> css
h1 {
font-size: 32px; } h2 {
font-size: 20px; } h3 {
font-size: 14px; }

3.3 scss嵌套

// scss
html {
font-size: 12px;
body {
background: #f66;
header {
width: 100%;
height: 40px;
nav {
background-color: #00f;
}
}
section {
width: 100%;
min-height: 500px;
}
footer {
width: 100%;
height: 60px;
}
}
}
// ==> css
html {
font-size: 12px; }
html body {
background: #f66; }
html body header {
width: 100%;
height: 40px; }
html body header nav {
background-color: #00f; }
html body section {
width: 100%;
min-height: 500px; }
html body footer {
width: 100%;
height: 60px; }

3.4 mixin 混入

  • 无参数
// scss
@mixin marginCenter {
margin: 0 auto;
} .container {
width: 1000px;
min-height: 600px;
// margin: 0 auto;
@include marginCenter();
} // ==> css
.container {
width: 1000px;
min-height: 600px;
margin: 0 auto; }
  • 有参数
// scss
@mixin margin($top, $right, $bottom, $left) {
margin: $top $right $bottom $left;
}
.container {
@include margin(10px, 10px, 20px, 20px);
} // ==> css
.container {
margin: 10px 10px 20px 20px; }
  • 解决兼容问题
//scss
@mixin flexbox {
display:-webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
} .nav {
ul {
@include flexbox();
li{
color: #333;
}
}
}
footer {
ul {
@include flexbox();
li{
font-size: 14px;
}
}
} // ==> css
.nav ul {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex; }
.nav ul li {
color: #333; } footer ul {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex; }
footer ul li {
font-size: 14px; }
  • 混入 默认参数
// scss
@mixin opacity($val: 1) {
opacity: $val;
}
.box {
@include opacity();
}
.box1 {
@include opacity(0.5);
}
// ==> css
.box {
opacity: 1; } .box1 {
opacity: 0.5; }

3.5 扩展 / 继承

// scss
.active {
background-color: #f66;
color: #fff;
}
.success {
// background-color: #f66;
// color: #fff;
@extend .active;
font-size: 30px;
}
// ==> css
.active, .success {
background-color: #f66;
color: #fff; } .success {
font-size: 30px; }

3.6 函数

// scss
@function dark($color, $val) {
@return $color - $val;
}
@function light($color, $val) {
@return $color + $val;
}
.text {
color: dark(red, 200);
}
.text1 {
color: light(red, 200);
}
// ==>
.text {
color: #370000; } .text1 {
color: #ffc8c8; }

3.7 条件判断

// scss
// @mixin flex($val: 1) {
// box-flex:$val;
// -webkit-box-flex:$val;
// -moz-box-flex:$val;
// flex:$val;
// -webkit-flex:$val;
// } @mixin flex($val) {
@if $val == auto {
box-flex:1;
-webkit-box-flex:1;
-moz-box-flex:1;
flex:1;
-webkit-flex:1;
} @else {
box-flex:$val;
-webkit-box-flex:$val;
-moz-box-flex:$val;
flex:$val;
-webkit-flex:$val;
}
} .test {
@include flex(auto);
} li {
@include flex(3);
}
// ==> css
.test {
box-flex: 1;
-webkit-box-flex: 1;
-moz-box-flex: 1;
flex: 1;
-webkit-flex: 1; } li {
box-flex: 3;
-webkit-box-flex: 3;
-moz-box-flex: 3;
flex: 3;
-webkit-flex: 3; }

3.8 导入另一个scss文件

// val.scss 变量
$baseColor: red;
$baseFontSize: 12px;
// base.scss 混入
@mixin flex {
flex: 1
}
// test.scss 应用
@import 'val.scss';
@import 'base.scss'; .box {
@include flex();
font-size: $baseFontSize;
} // ==》 css
.box {
flex: 1;
font-size: 12px; }
```0

sass的语法及其用法的更多相关文章

  1. sass 基本语法

    sass语法 文件后缀名 sass有两种后缀名文件:一种后缀名为sass,不使用大括号和分号:另一种就是我们这里使用的scss文件,这种和我们平时写的css文件格式差不多,使用大括号和分号. 而本教程 ...

  2. ES6语法 promise用法

    ES6语法 promise用法 function doSomething(){ return new Promise((resolve,reject)=>{ resolve('jjjj');// ...

  3. sass高级语法的补充

    1. 继承 2.混入 3.函数 我这篇博客需要点基础才能看懂, 但我这篇博客是对上一篇的 sass高级语法 的补充 从这方面来看也无所谓了

  4. sass高级语法

    github地址:https://github.com/lily1010/sass/tree/master/course03 用到的sass语法是: sass --watch test.scss:te ...

  5. sass笔记-2|Sass基础语法之让样式表更具条理性和可读性

    这一篇主要详述保持sass条理性和可读性的3个最基本方法--嵌套.导入和注释. 零. 变量 变量本身的作用是为了保持属性值的可维护性,把所有需要维护的属性值放在同一个地方,快速更改,处处生效,可谓售后 ...

  6. sass初级语法

    github地址:https://github.com/lily1010/sass/tree/master/course01 用到的sass语法是: sass --watch test.scss:te ...

  7. sass中级语法

    github地址:https://github.com/lily1010/sass/tree/master/course02 用到的sass语法是: sass --watch test.scss:te ...

  8. CSS语法与用法小字典

    前言:这是上学时期对CSS学习的整理,一直没见过光,由于不是专门做前端开发的,难免写不到重点,但对于看懂CSS,和掌握一些基本的用法,熟悉里面的门路还是大有裨益的.由于是从word中贴过来的,排版和格 ...

  9. Sass基础语法

    Sass是CSS3语言的扩展,在CSS的基础之上添加了新特性和语法,能省事地写出更好的样式表.Sass引擎是基于Ruby的. 导入Sass文件: @import "colors" ...

随机推荐

  1. 条款7:为多态基类析构函数声明为virtual

    基类指针指向子类对象. 子类对象必须位于堆.因此为了避免泄漏内存资源,当指针不使用时,delete掉每一个对象非常重要.但是如果基类的析构函数不声明为virtual.那么指向子类对象的指针delete ...

  2. nginx部署静态资源

    第一步.推荐使用EditPlus中ftp工具 安装,然后点击File->FTP->FTPUPLOAD->Settings->add.然后进行配置: 这样只是为了方便编辑Linu ...

  3. 【LeetCode】抽样 sampling(共4题)

    第一部分 水塘抽样 reservoir sampling 水塘抽样的原理:(应该开一篇新文章)pssss [382]Linked List Random Node (2018年11月15日,新算法) ...

  4. python基础:5.请编写一个函数实现将IP地址转换成一个整数。

    如 10.3.9.12 转换规则为:        10            00001010
         3            00000011
         9          ...

  5. python3输出中文报错的原因,及解决办法(基于pycharm)

    通常python3里面如果有中文,在不连接其他设备和程序的情况下,报错信息大致如下: SyntaxError: Non-UTF-8 code starting with '\xd6' in file ...

  6. restful接口风格

    一.定义 REST全称是Representational State Transfer, 中文意思是表述性状态转移. REST指的是一组架构约束条件和原则,如果一个架构符合REST的约束条件和原则,我 ...

  7. List和Tuple的中的method对比

  8. 【leetcode】878. Nth Magical Number

    题目如下: 解题思路:本题求的是第N个Magical Number,我们可以很轻松的知道这个数的取值范围 [min(A,B), N*max(A,B)].对于知道上下界求具体数字的题目,可以考虑用二分查 ...

  9. Excel,此文件中的某些文本格式可能已经更改,因为它已经超出最多允许的字体数。

    既然是超出最多允许的字体数,那么就不要循环创建IFont.先创建一个IFont font=wk.CreateFont();后面都使用它即可.

  10. 大量TIME_WAIT连接的解决办法

    1.使用keep alive连接(待补充) 2.修改tcp参数 根据TCP协议的连接断开规定,发起socket主动关闭的一方,socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个 ...