当前位置:   article > 正文

SASS 官方文档速通

SASS 官方文档速通

前言:参考 Sass 中文网

一. 特色功能

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

 二. 语法格式

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

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

 三. 用法

1. 注释 /* */ 与 //

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

  1. /* This comment is
  2. * several lines long.
  3. * since it uses the CSS comment syntax,
  4. * it will appear in the CSS output. */
  5. body {
  6. color: black;
  7. }
  8. // These comments are only one line long each.
  9. // They won't appear in the CSS output,
  10. // since they use the single-line comment syntax.
  11. a {
  12. color: green;
  13. }
  14. // 编译后
  15. /* This comment is
  16. * several lines long.
  17. * since it uses the CSS comment syntax,
  18. * it will appear in the CSS output. */
  19. body {
  20. color: black;
  21. }
  22. a {
  23. color: green;
  24. }

2. 嵌套

2.1 嵌套规则

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

  1. #main p {
  2. color: #00ff00;
  3. width: 97%;
  4. .redbox {
  5. background-color: #ff0000;
  6. color: #000000;
  7. }
  8. }
  9. // 编译后
  10. #main p {
  11. color: #00ff00;
  12. width: 97%;
  13. }
  14. #main p .redbox {
  15. background-color: #ff0000;
  16. color: #000000;
  17. }

2.2 父选择器 &

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

  1. a {
  2. font-weight: bold;
  3. text-decoration: none;
  4. &:hover {
  5. text-decoration: underline;
  6. }
  7. body.firefox & {
  8. font-weight: normal;
  9. }
  10. }
  11. // 编译后
  12. a {
  13. font-weight: bold;
  14. text-decoration: none;
  15. }
  16. a:hover {
  17. text-decoration: underline;
  18. }
  19. body.firefox a {
  20. font-weight: normal;
  21. }

2.3 属性嵌套

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

  1. .funky {
  2. font: {
  3. family: fantasy;
  4. size: 30em;
  5. weight: bold;
  6. }
  7. }
  8. // 编译后
  9. .funky {
  10. font-family: fantasy;
  11. font-size: 30em;
  12. font-weight: bold;
  13. }

3 变量 $

以 $ 开头定义变量:

  1. // 定义
  2. $width: 5em;
  3. // 使用
  4. #main {
  5. width: $width;
  6. }

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

  1. #main {
  2. $width: 5em !global;
  3. width: $width;
  4. }
  5. #sidebar {
  6. width: $width;
  7. }
  8. // 编译后
  9. #main {
  10. width: 5em;
  11. }
  12. #sidebar {
  13. width: 5em;
  14. }

4. 插值语句 #{}

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

  1. $name: foo;
  2. $attr: border;
  3. p.#{$name} {
  4. #{$attr}-color: blue;
  5. }
  6. // 编译后
  7. p.foo {
  8. border-color: blue;
  9. }

5. @import 导入

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

@import "foo.scss";

嵌套 @import

  1. .example {
  2. color: red;
  3. }
  4. #main {
  5. @import "example";
  6. }
  7. // 编译后
  8. #main .example {
  9. color: red;
  10. }

6. @extend 继承

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

  1. .error {
  2. border: 1px #f00;
  3. background-color: #fdd;
  4. }
  5. .error.intrusion {
  6. background-image: url("/image/hacked.png");
  7. }
  8. .seriousError {
  9. @extend .error;
  10. border-width: 3px;
  11. }
  12. // 编译后
  13. .error,
  14. .seriousError {
  15. border: 1px #f00;
  16. background-color: #fdd;
  17. }
  18. .error.intrusion,
  19. .seriousError.intrusion {
  20. background-image: url("/image/hacked.png");
  21. }
  22. .seriousError {
  23. border-width: 3px;
  24. }

7. @if 条件

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

  1. p {
  2. @if 1 + 1 == 2 {
  3. border: 1px solid;
  4. }
  5. @if 5 < 3 {
  6. border: 2px dotted;
  7. }
  8. @if null {
  9. border: 3px double;
  10. }
  11. }
  12. // 编译后
  13. p {
  14. border: 1px solid;
  15. }

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

  1. $type: monster;
  2. p {
  3. @if $type == ocean {
  4. color: blue;
  5. } @else if $type == matador {
  6. color: red;
  7. } @else if $type == monster {
  8. color: green;
  9. } @else {
  10. color: black;
  11. }
  12. }
  13. // 编译后
  14. p {
  15. color: green;
  16. }

8. @for 循环

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

  1. @for $i from 1 through 3 {
  2. .item-#{$i} {
  3. width: 2em * $i;
  4. }
  5. }
  6. // 编译后
  7. .item-1 {
  8. width: 2em;
  9. }
  10. .item-2 {
  11. width: 4em;
  12. }
  13. .item-3 {
  14. width: 6em;
  15. }

9. @each 循环

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

  1. @each $animal in puma, sea-slug, egret, salamander {
  2. .#{$animal}-icon {
  3. background-image: url("/images/#{$animal}.png");
  4. }
  5. }
  6. // 编译后
  7. .puma-icon {
  8. background-image: url("/images/puma.png");
  9. }
  10. .sea-slug-icon {
  11. background-image: url("/images/sea-slug.png");
  12. }
  13. .egret-icon {
  14. background-image: url("/images/egret.png");
  15. }
  16. .salamander-icon {
  17. background-image: url("/images/salamander.png");
  18. }

10. @while 循环

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

  1. $i: 6;
  2. @while $i > 0 {
  3. .item-#{$i} {
  4. width: 2em * $i;
  5. }
  6. $i: $i - 2;
  7. }
  8. // 编译后
  9. .item-6 {
  10. width: 12em;
  11. }
  12. .item-4 {
  13. width: 8em;
  14. }
  15. .item-2 {
  16. width: 4em;
  17. }

11. @mixin 混合指令

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

11.1 @mixin 定义、@include 引入

  1. @mixin large-text {
  2. font: {
  3. family: Arial;
  4. size: 20px;
  5. weight: bold;
  6. }
  7. color: #ff0000;
  8. }
  9. .page-title {
  10. @include large-text;
  11. padding: 4px;
  12. margin-top: 10px;
  13. }
  14. // 编译后
  15. .page-title {
  16. font-family: Arial;
  17. font-size: 20px;
  18. font-weight: bold;
  19. color: #ff0000;
  20. padding: 4px;
  21. margin-top: 10px;
  22. }

11.2 参数

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

  1. @mixin sexy-border($color, $width) {
  2. border: {
  3. color: $color;
  4. width: $width;
  5. style: dashed;
  6. }
  7. }
  8. p {
  9. @include sexy-border(blue, 1in);
  10. }
  11. // 编译后
  12. p {
  13. border-color: blue;
  14. border-width: 1in;
  15. border-style: dashed;
  16. }

11.3 参数变量

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

  1. ```scss
  2. @mixin box-shadow($shadows...) {
  3. -moz-box-shadow: $shadows;
  4. -webkit-box-shadow: $shadows;
  5. box-shadow: $shadows;
  6. }
  7. .shadows {
  8. @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
  9. }
  10. // 编译后
  11. .shadowed {
  12. -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  13. -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  14. box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  15. }

12. @function 函数

  1. $grid-width: 40px;
  2. $gutter-width: 10px;
  3. @function grid-width($n) {
  4. @return $n * $grid-width + ($n - 1) * $gutter-width;
  5. }
  6. #sidebar {
  7. width: grid-width(5);
  8. }
  9. // 编译后
  10. #sidebar {
  11. width: 240px;
  12. }
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号