当前位置: 首页 > news >正文

SASS 官方文档速通

前言:参考 Sass 中文网。

一. 特色功能

Sass 是一款强化 CSS 的辅助工具,在 CSS 语法的基础上增加了变量、嵌套、混合、导入等高级功能。有助于组织管理样式文件,更高效地开发项目。

 二. 语法格式

.scss 拓展名:在 CSS3 语法的基础上进行拓展。

.sass 拓展名:用 “缩进” 代替 “花括号” ,用 “换行” 代替 “分号” 。

 三. 用法

1. 注释 /* */ 与 //

多行注释 /* */,单行注释 //,前者会被输出到编译后的 CSS 文件中,而后者则不会,例如:

/* This comment is* several lines long.* since it uses the CSS comment syntax,* it will appear in the CSS output. */
body {color: black;
}
// These comments are only one line long each.
// They won't appear in the CSS output,
// since they use the single-line comment syntax.
a {color: green;
}// 编译后
/* This comment is* several lines long.* since it uses the CSS comment syntax,* it will appear in the CSS output. */
body {color: black;
}
a {color: green;
}

2. 嵌套

2.1 嵌套规则

避免重复输入父选择器,令复杂的 CSS 结构易于管理。

#main p {color: #00ff00;width: 97%;.redbox {background-color: #ff0000;color: #000000;}
}// 编译后
#main p {color: #00ff00;width: 97%;
}
#main p .redbox {background-color: #ff0000;color: #000000;
}

2.2 父选择器 &

在嵌套 CSS 中,访问外层的父选择器,使用 & 代表外层的父选择器。

a {font-weight: bold;text-decoration: none;&:hover {text-decoration: underline;}body.firefox & {font-weight: normal;}
}// 编译后
a {font-weight: bold;text-decoration: none;
}
a:hover {text-decoration: underline;
}
body.firefox a {font-weight: normal;
}

2.3 属性嵌套

如 font-family, font-size, font-weight 。为了便于管理,避免重复输入。可以这样写:

.funky {font: {family: fantasy;size: 30em;weight: bold;}
}// 编译后
.funky {font-family: fantasy;font-size: 30em;font-weight: bold;
}

3 变量 $

以 $ 开头定义变量:

// 定义
$width: 5em;// 使用
#main {width: $width;
}

块级变量可以通过 !global 升级为全局变量:

#main {$width: 5em !global;width: $width;
}
#sidebar {width: $width;
}// 编译后
#main {width: 5em;
}
#sidebar {width: 5em;
}

4. 插值语句 #{}

通过 #{} 可以在选择器或属性名中使用变量:

$name: foo;
$attr: border;
p.#{$name} {#{$attr}-color: blue;
}// 编译后
p.foo {border-color: blue;
}

5. @import 导入

Sass 拓展了 @import 的功能,允许其导入 SCSS 或 Sass 文件。可以使用导入文件的变量和混合指令(mixin)。

@import "foo.scss";

嵌套 @import

.example {color: red;
}
#main {@import "example";
}// 编译后
#main .example {color: red;
}

6. @extend 继承

一个元素使用的样式与另一个元素完全相同,但又添加了额外的样式。提取出公用样式,然后使用 @extend 继承。

.error {border: 1px #f00;background-color: #fdd;
}
.error.intrusion {background-image: url("/image/hacked.png");
}
.seriousError {@extend .error;border-width: 3px;
}// 编译后
.error,
.seriousError {border: 1px #f00;background-color: #fdd;
}
.error.intrusion,
.seriousError.intrusion {background-image: url("/image/hacked.png");
}
.seriousError {border-width: 3px;
}

7. @if 条件

当 @if 的表达式返回值不是 false 或者 null 时,条件成立,输出 {} 内的代码:

p {@if 1 + 1 == 2 {border: 1px solid;}@if 5 < 3 {border: 2px dotted;}@if null {border: 3px double;}
}// 编译后
p {border: 1px solid;
}

@if 后可以跟多个 @else if ,或 @else 。如果 @if 失败,Sass 将逐条执行 @else if ,如果全失败,则执行 @else 声明,例如:

$type: monster;
p {@if $type == ocean {color: blue;} @else if $type == matador {color: red;} @else if $type == monster {color: green;} @else {color: black;}
}// 编译后
p {color: green;
}

8. @for 循环

@for 指令可以在限制的范围内重复输出格式。

@for $i from 1 through 3 {.item-#{$i} {width: 2em * $i;}
}// 编译后
.item-1 {width: 2em;
}
.item-2 {width: 4em;
}
.item-3 {width: 6em;
}

9. @each 循环

@each 遍历的是一连串的值,也就是值列表。

@each $animal in puma, sea-slug, egret, salamander {.#{$animal}-icon {background-image: url("/images/#{$animal}.png");}
}// 编译后
.puma-icon {background-image: url("/images/puma.png");
}
.sea-slug-icon {background-image: url("/images/sea-slug.png");
}
.egret-icon {background-image: url("/images/egret.png");
}
.salamander-icon {background-image: url("/images/salamander.png");
}

10. @while 循环

@while 指令重复输出直到返回结果为 false。这样可以实现比 @for 更复杂的循环,只是很少会用到。例如:

$i: 6;
@while $i > 0 {.item-#{$i} {width: 2em * $i;}$i: $i - 2;
}// 编译后
.item-6 {width: 12em;
}
.item-4 {width: 8em;
}
.item-2 {width: 4em;
}

11. @mixin 混合指令

混合指令(Mixin)用于定义可重复使用的样式。

11.1 @mixin 定义、@include 引入

@mixin large-text {font: {family: Arial;size: 20px;weight: bold;}color: #ff0000;
}
.page-title {@include large-text;padding: 4px;margin-top: 10px;
}// 编译后
.page-title {font-family: Arial;font-size: 20px;font-weight: bold;color: #ff0000;padding: 4px;margin-top: 10px;
}

11.2 参数

参数用于给混合指令中的样式设定变量。

@mixin sexy-border($color, $width) {border: {color: $color;width: $width;style: dashed;}
}
p {@include sexy-border(blue, 1in);
}// 编译后
p {border-color: blue;border-width: 1in;border-style: dashed;
}

11.3 参数变量

不确定混合指令需要多少参数,可以使用参数变量 … 声明

```scss
@mixin box-shadow($shadows...) {-moz-box-shadow: $shadows;-webkit-box-shadow: $shadows;box-shadow: $shadows;
}
.shadows {@include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}// 编译后
.shadowed {-moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;-webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

12. @function 函数

$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;
}
http://www.lryc.cn/news/295171.html

相关文章:

  • 《动手学深度学习(PyTorch版)》笔记7.4
  • 关于自动驾驶概念的学习和一些理解
  • C++ dfs搜索枚举(四十八)【第八篇】
  • 【优先级队列(大顶堆 小顶堆)】【遍历哈希表键值对】Leetcode 347 前K个高频元素
  • Java设计模式-模板方法模式(14)
  • 【C++ 二维前缀和】约会
  • 基于Springboot的社区疫情防控平台
  • JAVA中的类方法
  • rust嵌入式开发之RTICvsEmbassy
  • Bug地狱 #1 突然宕机,企业级应用到底怎么了
  • 使用 Python、Elasticsearch 和 Kibana 分析波士顿凯尔特人队
  • 探索C语言结构体:编程中的利器与艺术
  • Git介绍与常用命令总结
  • 机器学习 | 探索朴素贝叶斯算法的应用
  • 【无刷电机学习】电流采样电路硬件方案
  • 对于协同过滤算法我自己的一些总结和看法
  • 数据库管理phpmyadmin
  • Oracle数据表ID自增操作
  • npm WARN deprecated uuid@3.4.0: Please upgrade to version 7 or higher
  • 第2节、让电机转起来【51单片机+L298N步进电机系列教程】
  • 1154: 第多少天
  • 【C语言初阶-const作用详解】const修饰变量、const修饰指针(图文详解版)
  • 线程协作工具类【CountDownLatch倒数门闩、Semaphore信号量、CyclicBarrier循环栏栅、Condition接口】
  • Python 函数式编程进阶:map、filter、reduce
  • 大模型|基础_word2vec
  • 14.2 url后端过滤器(❤❤)
  • Leetcode 377 组合总和 Ⅳ
  • CleanMyMacX4.14.6如何清理mac垃圾内存
  • Java 学习和实践笔记(1)
  • 【自然语言处理-工具篇】spaCy<1>--介绍及安装指南