sass基础语法
Sass 是一种 CSS 预处理器,在 CSS 基础上提供了变量、嵌套、模块化、Mixin(混入)、运算和条件判断等功能,能够提高 CSS 的开发效率与可维护性。Sass 代码需要经过编译,最终转换为浏览器能够识别的普通 CSS。
Sass 环境搭建
Sass 与 SCSS 的区别
Sass 提供两种语法格式:推荐使用 SCSS,因为它与普通 CSS 的语法基本一致。
| 对比 | SCSS | Sass |
|---|---|---|
| 文件后缀 | .scss | .sass |
大括号 {} | 需要 | 不需要 |
分号 ; | 需要 | 不需要 |
| CSS 语法兼容性 | 兼容 CSS | 使用缩进语法 |
| 推荐程度 | 推荐 | 较少使用 |
安装 Sass
首先需要安装 Node.js,然后通过 npm 安装 Sass。
npm install -d sass编译 SCSS 文件
创建两个文件:
project/├── index.html├── style.scss└── style.css编写 style.scss:
$primary-color: #409eff;
.box { width: 200px; height: 200px; background-color: $primary-color;}执行编译:
npx sass style.scss style.css生成的 style.css:
.box { width: 200px; height: 200px; background-color: #409eff;}在 HTML 中引入:
<link rel="stylesheet" href="./style.css" />浏览器只能直接使用编译后的 CSS。
自动监听文件变化
npx sass --watch style.scss:style.css修改 SCSS 文件后,Sass 会自动重新编译 CSS。
如果需要监听整个目录:
npx sass --watch scss:css将 scss 目录中的样式编译到 css 目录。
基本语法
注释
// 单行注释,不会输出到 CSS
/* 多行注释,通常会输出到非压缩 CSS */基本嵌套
普通 CSS:
.nav { background: #333;}.nav a { color: white;}.nav a:hover { color: red;}SCSS:
.nav { background: #333; a { color: white; &:hover { color: red; } }}其中 & 表示当前嵌套规则的父选择器。
父选择器 &
.button { color: #333;
&:hover { color: red; }
&.active { color: blue; }
&--primary { background: #409eff; }
.icon { width: 20px; }}编译后的选择器:
| SCSS | CSS |
|---|---|
&:hover | .button:hover |
&.active | .button.active |
&--primary | .button--primary |
.icon | .button .icon |
属性嵌套
SCSS 还支持对具有相同前缀的 CSS 属性进行嵌套。实际开发中,普通属性写法通常更直观,按需使用即可。
.box { font: { size: 16px; weight: bold; family: Arial; }
margin: { top: 10px; bottom: 20px; }}编译后:
.box { font-size: 16px; font-weight: bold; font-family: Arial; margin-top: 10px; margin-bottom: 20px;}变量入门
定义与使用变量
Sass 使用 $ 定义变量。
$primary-color: #409eff;$font-size: 16px;$border-radius: 8px;
.button { background: $primary-color; font-size: $font-size; border-radius: $border-radius;}编译后:
.button { background: #409eff; font-size: 16px; border-radius: 8px;}变量适合统一管理项目中的颜色、字体大小、间距和圆角等属性。
变量作用域
局部变量通常只在当前作用域内有效。
$color: red; // 全局变量.box { $color: blue; // 局部变量
color: $color;}.title { color: $color;}编译后:
.box { color: blue;}.title { color: red;}变量运算
$width: 200px;$gap: 20px;
.box { width: $width + $gap; padding: $gap * 2; margin: $gap - 5px;}编译后:
.box { width: 220px; padding: 40px; margin: 15px;}Sass 变量与 CSS 变量
| 对比 | Sass 变量 | CSS 变量 |
|---|---|---|
| 声明 | $color: red; | --color: red; |
| 使用 | $color | var(--color) |
| 生效阶段 | 编译时 | 浏览器运行时 |
| JS 动态修改 | 不支持直接修改 | 支持 |
| 主要用途 | 样式复用、编译配置 | 动态主题、实时样式变化 |
嵌套,代码拆分及引入
当项目样式较多时,可以将 SCSS 拆分为多个文件,按照功能管理。
文件拆分
project/├── index.html├── scss/│ ├── _variables.scss│ ├── _mixins.scss│ ├── _reset.scss│ ├── _index.scss│ └── main.scss└── css/ └── main.css以 _ 开头的 SCSS 文件称为 Partial(局部文件),一般作为模块供其他文件加载,不单独编译成 CSS。
@use 加载模块
_variables.scss$primary-color: #409eff;$font-size: 16px;main.scss@use "variables";
.box { color: variables.$primary-color; font-size: variables.$font-size;}可以使用 as 定义别名:
@use "variables" as v;
.box { color: v.$primary-color;}也可以使用 as *,省略命名空间:
@use "variables" as *;
.box { color: $primary-color;}实际开发中推荐保留命名空间,避免不同模块之间出现命名冲突。
@forward 转发模块
假设存在两个模块:
_variables.scss$primary-color: #409eff;_mixins.scss@mixin flex-center { display: flex; justify-content: center; align-items: center;}可以通过 _index.scss 统一转发:
@forward "variables";@forward "mixins";然后在 main.scss 中使用:
@use "index" as s;
.box { color: s.$primary-color; @include s.flex-center;}@use、@forward 和 @import 的区别
| 指令 | 功能 | 推荐情况 |
|---|---|---|
@use | 加载模块并使用其成员 | 推荐 |
@forward | 转发模块的公共成员 | 推荐 |
@import | 旧式模块导入方式 | 已废弃 |
新项目应当使用 @use 和 @forward 管理 SCSS 模块。
Mixin混入的使用
Mixin 用于定义可以重复使用的样式代码块,类似于函数,可以传递参数。
定义和调用 Mixin
使用 @mixin 定义,使用 @include 调用。
@mixin flex-center { display: flex; justify-content: center; align-items: center;}
.box { @include flex-center;}
.container { @include flex-center;}编译后:
.box { display: flex; justify-content: center; align-items: center;}
.container { display: flex; justify-content: center; align-items: center;}带参数的 Mixin
@mixin flex-center($gap) { display: flex; justify-content: center; align-items: center; gap: $gap;}
.box { @include flex-center(10px);}
.container { @include flex-center(20px);}默认参数
@mixin box-style( $color: #409eff, $radius: 8px) { background: $color; border-radius: $radius;}
.box1 { @include box-style;}
.box2 { @include box-style(red, 12px);}也可以通过参数名指定传参:
.box3 { @include box-style($radius: 20px);}@content 插入样式代码块
@content 是 Mixin 中的重要语法,表示接收并插入调用时传入的样式代码块。
@mixin hover { &:hover { @content; }}
.button { color: #333; @include hover { color: red; background: #eee; }}编译后:
.button { color: #333;}
.button:hover { color: red; background: #eee;}@content 常用于封装媒体查询、伪类等需要传入不同样式的场景。
媒体查询与Mixin 的配合使用
使用 Mixin 可以将媒体查询中的断点统一封装,避免在多个地方重复编写断点。
普通媒体查询
.container { width: 100%; padding: 10px;
@media (min-width: 768px) { padding: 20px; }
@media (min-width: 1200px) { padding: 30px; }}这种写法可以直接使用,但当项目中的媒体查询较多时,可以使用 Mixin 统一管理。
封装媒体查询 Mixin
@mixin respond($device) { @if $device == tablet { @media (min-width: 768px) { @content; } } @else if $device == desktop { @media (min-width: 1200px) { @content; } }}核心语法:
| 语法 | 功能 |
|---|---|
@mixin | 定义混入 |
@include | 调用混入 |
@if | 条件判断 |
@else if | 多条件判断 |
@content | 插入调用时传入的样式 |
使用媒体查询 Mixin
@use "mixins" as m;
.container { width: 100%; padding: 10px;
@include m.respond(tablet) { padding: 20px; }
@include m.respond(desktop) { padding: 30px; }}编译后:
.container { width: 100%; padding: 10px;}
@media (min-width: 768px) { .container { padding: 20px; }}
@media (min-width: 1200px) { .container { padding: 30px; }}实际案例:响应式网格布局
HTML:
<div class="card-list"> <div class="card">卡片1</div> <div class="card">卡片2</div> <div class="card">卡片3</div> <div class="card">卡片4</div></div>SCSS:
@use "mixins" as m;
.card-list { display: grid; grid-template-columns: 1fr; gap: 16px;
// 平板:两列 @include m.respond(tablet) { grid-template-columns: repeat(2, 1fr); }
// 桌面:四列 @include m.respond(desktop) { grid-template-columns: repeat(4, 1fr); }}
.card { padding: 20px; color: white; background: #409eff; border-radius: 8px; text-align: center;}文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!














