VSCode中使用Scss/Sass及其基本语法

简介: VSCode中使用Scss/Sass及其基本语法

31.3.png

Sass

Sass (英文全称:Syntactically Awesome Stylesheets)

Sass 是一个 CSS 预处理器

Sass 文件后缀为 .scss

一、Sass编译环境

1、Ruby sass(已弃用)依赖Ruby环境

# 安装
$ gem install sass
# 卸载
$ gem uninstall sass

2、Dart Sass(推荐)

依赖Node.js环境

# 安装 
$ npm install -g sass
# 查看版本
$ sass --version
1.32.8 compiled with dart2js 2.10.5
# 使用
$ sass <input.scss> [output.css]

3、VSCode实时编译插件

Live Sass Compiler

点击VSCode下方的Watch Sass

二、Sass语法规则

1、变量

$baseColor: red;
body {
  background-color: $baseColor;
}

输出

body {
    background-color: red;
}

2、嵌套

.content {
    .box {
        color: red;
    }
}

输出

.content .box {
    color: red;
  }

3、引入@import

reset.scss

// reset.css
html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}

_colors.scss

// 下划线开头的文件_color.scss 不会被编译成 color.css
$text-color: red;

main.scss

// 后缀.scss可省略
@import './reset';
@import './colors';
a{
    color: $text-color
}

编译结果

main.css

html,
body,
ul,
ol {
  margin: 0;
  padding: 0;
}
a {
  color: red;
}

4、混入@mixin 与 @include

混入相当于一个函数

// 定义混入,可以传入参数
@mixin text-style {
    color: red;
    font-size: 25px;
}
// 使用混入
a{
    @include text-style;
    font-size: 26px; // 重写样式
}

输出

a {
    color: red;
    font-size: 25px;
    font-size: 26px;
}

5、继承 @extend

// 基本样式
.base-style {
  color: red;
  font-size: 25px;
}
// 继承样式
a{
    @extend .base-style;
    font-size: 26px; // 重写样式
}
.base-style, a {
    color: red;
    font-size: 25px;
}
a {
font-size: 26px;
}

参考

https://www.runoob.com/sass/sass-tutorial.html

相关文章
|
前端开发
VSCode:Easy LESS、Easy Sass自动生成css的配置
VSCode:Easy LESS、Easy Sass自动生成css的配置
499 0
|
Dart 前端开发 JavaScript
VSCode中使用Scss/Sass及其基本语法
VSCode中使用Scss/Sass及其基本语法
462 0
|
1月前
|
人工智能 自然语言处理 算法
国产新型AI编程助手—DevChat AI插件在VSCode中的应用
国产新型AI编程助手—DevChat AI插件在VSCode中的应用
|
3月前
|
人工智能 JavaScript 程序员
Fitten Code:在VSCode插件市场备受欢迎的原因是什么?
随着AI技术的不断发展,AI在编写代码方面的能力也日益强大。充分利用AI的能力能够显著提高代码编写的效率和质量。今天我将向大家介绍一款备受瞩目的AI代码神器——Fitten Code,让我们一同揭开它神秘的面纱!
273 3
|
17天前
Vscode 插件-代码敲出不同的特效
Vscode 插件-代码敲出不同的特效
|
1月前
|
NoSQL 关系型数据库 开发工具
最新最全 VSCODE 插件推荐(2023版)
最新最全 VSCODE 插件推荐(2023版)
115 0
|
1月前
|
Java
VSCode 插件Code Runner 中文提示乱码
VSCode 插件Code Runner 中文提示乱码
|
1月前
|
开发者
|
1月前
|
JavaScript 前端开发 程序员
非专业程序员常用vscode插件
这里的vscode插件, 有四分之一你没用过, 这里的vscode设置, 80%你没设置过, 挖宝随缘
49 5
|
2月前
|
Web App开发 JavaScript 前端开发
推荐常用的VSCode插件
推荐常用的VSCode插件
85 2