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

Sass 常用的功能!

Sass 常用功能

Sass 功能有很多,这边只列举一些比较常用的。

嵌套规则 (Nested Rules)

Sass 允许将一套 CSS 样式嵌套进另一套样式中,内层的样式将它外层的选择器作为父选择器。

编译前

.box {.box1 {background-color: red;}.box2 {background-color: blueviolet;}
}

编译后

.box .box1 {background-color: red;
}
.box .box2 {background-color: blueviolet;
}
.box .box3 {background-color: blue;
}

父选择器 & (Referencing Parent Selectors: &)

在嵌套 CSS 规则时,有时也需要直接使用嵌套外层的父选择器。

编译前

button {width: 100px;height: 30px;&:hover {background-color: red;}
}

编译后

button {width: 100px;height: 30px;
}
button:hover {background-color: red;
}

属性嵌套 (Nested Properties)

有些 CSS 属性遵循相同的命名空间 ( namespace ),比如 font-family , font-size , font-weight 都以 font 作为属性的命名空间。为了便于管理这样的属性,同时也为了避免了重复输入,Sass 允许将属性嵌套在命名空间中。

编译前

.my-content {padding: {top: 10px;right: 10px;bottom: 10px;left: 10px;}font: {size: 30px;weight: bold;}
}

编译后

.my-content {padding-top: 10px;padding-right: 10px;padding-bottom: 10px;padding-left: 10px;font-size: 30px;font-weight: bold;
}

注释 /* / 与 // (Comments: / */ and //)

单行注释不会被编译到 css 文件中,多行注释中可以使用差值语句 “``” ,类型 ES6 的模板字符串。

单行注释

编译前

// These comments are only one line long each.
// They won't appear in the CSS output,
// since they use the single-line comment syntax..my-content {width: 100%;height: 100%;
}

编译后

.my-content {width: 100%;height: 100%;
}

多行注释

编译前

/* This comment is* several lines long.* since it uses the CSS comment syntax,* it will appear in the CSS output. */.my-content {width: 100%;height: 100%;
}

编译后

/* This comment is* several lines long.* since it uses the CSS comment syntax,* it will appear in the CSS output. */
.my-content {width: 100%;height: 100%;
}

变量 $ (Variables: $)

编译前

$width: 100px;
$height: 100px;.box {width: $width;height: $height;
}

编译后

.box {width: 100px;height: 100px;
}

运算 (Operations)

编译前

.box {width: 100px + 100px;height: 200px / 2;background-color: royalblue;
}

编译后

.box {width: 200px;height: 100px;background-color: royalblue;
}

插值语句 #{} (Interpolation: #{})

编译前

$name: box;
$attr: background;.box {.#{$name}1 {width: 100px;height: 100px;#{$attr}-color: red;}.#{$name}2 {width: 100px;height: 100px;#{$attr}-color: blueviolet;}.#{$name}3 {width: 100px;height: 100px;#{$attr}-color: blue;}
}

编译后

.box .box1 {width: 100px;height: 100px;background-color: red;
}
.box .box2 {width: 100px;height: 100px;background-color: blueviolet;
}
.box .box3 {width: 100px;height: 100px;background-color: blue;
}

@import

Sass 拓展了 @import 的功能,允许其导入 SCSS 或 Sass 文件。被导入的文件将合并编译到同一个 CSS 文件中,另外,被导入的文件中所包含的变量或者混合指令 ( mixin ) 都可以在导入的文件中使用。

编译前(base.scss)

@import './reset.scss';.box {.#{$name}1 {width: 100px;height: 100px;#{$attr}-color: red;}.#{$name}2 {width: 100px;height: 100px;#{$attr}-color: blueviolet;}.#{$name}3 {width: 100px;height: 100px;#{$attr}-color: blue;}
}

编译前(reset.scss)

* {margin: 0;padding: 0;
}$name: box;
$attr: background;

编译后(base.css)

* {margin: 0;padding: 0;
}.box .box1 {width: 100px;height: 100px;background-color: red;
}
.box .box2 {width: 100px;height: 100px;background-color: blueviolet;
}
.box .box3 {width: 100px;height: 100px;background-color: blue;
}

嵌套 @import

编译前(base.scss)

.box {@import 'box';
}

编译前(box.scss)

.box1 {width: 100px;height: 100px;background-color: red;
}.box2 {width: 100px;height: 100px;background-color: blue;
}.box3 {width: 100px;height: 100px;background-color: blueviolet;
}

编译后(base.css)

.box .box1 {width: 100px;height: 100px;background-color: red;
}
.box .box2 {width: 100px;height: 100px;background-color: red;
}
.box .box3 {width: 100px;height: 100px;background-color: red;
}

@extend

编译前

.box {width: 100px;height: 100px;
}.box {.box1 {@extend .box;background-color: red;}.box2 {@extend .box;background-color: blueviolet;}.box3 {@extend .box;background-color: blue;}
}

编译后

.box,
.box .box1,
.box .box2,
.box .box3 {width: 100px;height: 100px;
}.box .box1 {background-color: red;
}
.box .box2 {background-color: blueviolet;
}
.box .box3 {background-color: blue;
}

@if

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

编译前

$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;
}

@for

@for 指令可以在限制的范围内重复输出格式,每次按要求(变量的值)对输出结果做出变动。这个指令包含两种格式:@for $var from “start” through “end”,或者 @for $var from “start” to “end” 。

区别在于 through 与 to 的含义:当使用 through 时,条件范围包含 “start” 与 “end” 的值,而使用 to 时条件范围只包含 “start” 的值不包含 “end” 的值。另外, $var 可以是任何变量,比如 $i ; “start” 和 “end” 必须是整数值。

编译前

@for $i from 1 through 3 {.item-#{$i} {width: 2em * $i;}
}

编译后

.item-1 {width: 2em;
}.item-2 {width: 4em;
}.item-3 {width: 6em;
}

@each

@each 指令的格式是 $var in “list”, $var 可以是任何变量名,比如 $length 或者 n a m e ,而 " l i s t " 是一连串的值,也就是值列表。 @ e a c h 是一个循环语句, name,而 "list" 是一连串的值,也就是值列表。@each是一个循环语句, name,而"list"是一连串的值,也就是值列表。@each是一个循环语句,key、$value、相当于 javascript 中的对象键值对,名字可以自定义。

编译前

@each $key, $value in (h1: 2em, h2: 1.5em, h3: 1.2em) {#{$key} {font-size: $value;}
}

编译后

h1 {font-size: 2em;
}h2 {font-size: 1.5em;
}h3 {font-size: 1.2em;
}

@while

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

编译前

$i: 3;
@while $i > 0 {.item-#{$i} {width: 2em * $i;}$i: $i - 1;
}

编译后

.item-3 {width: 6em;
}.item-2 {width: 4em;
}.item-1 {width: 2em;
}

混合指令 @mixin & @include

使用 @mixin 指令定义混合样式,使用 @include 指令引用混合样式,格式是在其后添加混合名称,以及需要的参数(可选)。

编译前

@mixin box {width: 100px;height: 100px;
}.box {.box1 {@include box;background-color: red;}.box2 {@include box;background-color: blueviolet;}.box3 {@include box;background-color: blue;}
}

编译后

.box .box1 {width: 100px;height: 100px;background-color: red;
}
.box .box2 {width: 100px;height: 100px;background-color: blueviolet;
}
.box .box3 {width: 100px;height: 100px;background-color: blue;
}

参数 (Arguments)

参数用于给混合指令中的样式设定变量,并且赋值使用。在定义混合指令的时候,按照变量的格式,通过逗号分隔,将参数写进圆括号里。引用指令时,按照参数的顺序,再将所赋的值对应写进括号。

编译前

@mixin box($color) {width: 100px;height: 100px;background-color: $color;
}.box {.box1 {@include box(red);}.box2 {@include box(blueviolet);}.box3 {@include box(blue);}
}

编译后

.box .box1 {width: 100px;height: 100px;background-color: red;
}
.box .box2 {width: 100px;height: 100px;background-color: blueviolet;
}
.box .box3 {width: 100px;height: 100px;background-color: blue;
}

函数指令 (Function Directives)

Sass 支持自定义函数,并能在任何属性值或 Sass script 中使用。

编译前

@function box-width($width) {@return $width * 2;
}.box {.box1 {width: box-width(100px);height: 100px;background-color: red;}.box2 {width: box-width(100px);height: 100px;background-color: blueviolet;}.box3 {width: box-width(100px);height: 100px;background-color: blue;}
}

编译后

.box .box1 {width: 200px;height: 100px;background-color: red;
}
.box .box2 {width: 200px;height: 100px;background-color: blueviolet;
}
.box .box3 {width: 200px;height: 100px;background-color: blue;
}

原文链接:菜园前端

相关文章:

Sass 常用的功能!

Sass 常用功能 Sass 功能有很多,这边只列举一些比较常用的。 嵌套规则 (Nested Rules) Sass 允许将一套 CSS 样式嵌套进另一套样式中,内层的样式将它外层的选择器作为父选择器。 编译前 .box {.box1 {background-color: red;}.box2 {background-col…...

chmod命令详细使用说明

chmod命令详细使用说明 chmod是Unix和类Unix系统上用于更改文件或目录权限的命令。它是"change mode"的缩写。在Linux和其他类Unix操作系统中,文件和目录具有权限位,用来控制哪些用户可以访问、读取、写入或执行它们。chmod命令允许用户修改这…...

ICC2如何计算Gate Count?

我正在「拾陆楼」和朋友们讨论有趣的话题,你⼀起来吧?知识星球入口 我们认为gate count等于standard cell(非physical only)总面积 / 最小驱动二输入与非门面积。 ICC2没有专门的命令去报告gate count,只能自己计算,使用report_d…...

Qtday3作业

作业 头文件 #ifndef WIDGET_H #define WIDGET_H#include <QPushButton> #include <QTextToSpeech> #include <QWidget> #include <QDebug> #include <QTimer> //定时器类 #include <QTime> //时间类 #include <QTimerEvent>…...

全球程序员需要知道的50+网址,有多少你第一次听说?

作为程序员&#xff0c;需要知道的50网址&#xff0c;有多少你第一次听说 GitHub (github.com): 最大的代码托管平台&#xff0c;开源项目和代码分享的社区。程序员可以在这里找到各种有趣的项目&#xff0c;参与开源贡献或托管自己的代码。 Stack Overflow (stackoverflow.co…...

Matlab中实现对一幅图上的局部区域进行放大

大家好&#xff0c;我是带我去滑雪&#xff01; 局部放大图可以展示图像中的细节信息&#xff0c;使图像更加直观和精美&#xff0c;此次使用magnify工具实现对绘制的figure选择区域绘制&#xff0c;图像效果如下&#xff1a; 1、基本图像绘制 这里选择绘制一个散点图&#xff…...

mysql-速成补充

目录 1.演示事务 ​编辑 1.1 read-uncommitted 1.2 read-committed 1.3 repeatable read 1.4 幻读 1.5 serializable 1.6 savepoint 2 变量 2.1 语法 2.2 举例 3 存储过程和函数 3.1 特点和语法 3.2 举例 4.函数 4.1 语法 4.2 举例 5 流程控制 5.1 分…...

微信小程序,商城底部工具栏的实现

效果演示&#xff1a; 前提条件&#xff1a; 去阿里云矢量图标&#xff0c;下载8个图标&#xff0c;四个黑&#xff0c;四个红&#xff0c;如图&#xff1a; 新建文件夹icons&#xff0c;把图标放到该文件夹&#xff0c;然后把该文件夹移动到该项目的文件夹里面。如图所示 app…...

Lab———Git使用指北

Lab———Git使用指北 &#x1f916;:使用IDEA Git插件实际工作流程 &#x1f4a1; 本文从实际使用的角度出发&#xff0c;以IDEA Git插件为基本讲述了如果使用IDEA的Git插件来解决实际开发中的协作开发问题。本文从 远程仓库中拉取项目&#xff0c;在本地分支进行开发&#x…...

ChatGPT的工作原理:从输入到输出

&#x1f337;&#x1f341; 博主 libin9iOak带您 Go to New World.✨&#x1f341; &#x1f984; 个人主页——libin9iOak的博客&#x1f390; &#x1f433; 《面试题大全》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33…...

redis数据库与主从复制

目录 一 基本操作 二 执行流程 三 reids持久化 四 rdb和aof持久化的过程 五 为什么会有内存碎片 六 redis组从复制 一 基本操作 set :存放数据 例如 set 键值 内容 set k kokoko k就是键值 kokoko就是内容 get:获取数据 例如 get k 就会出来 k对应的数据 keys 查询键…...

js加载和长任务

js加载和长任务 本文将讲解以下浏览器如何加载js&#xff0c;并介绍一些可以提高网页加载速度的方法。 Evaluate Script 如果我们在devtools的performance中分析过网站的加载性能&#xff0c;可能会看到一个很长的任务&#xff0c;叫做Evaluate Script. 在这种情况下&#x…...

利用Stable diffusion Ai 制作艺术二维码超详细参数和教程

大家有没有发现最近这段时间网上出现了各种各样的AI艺术二维码&#xff0c;这种二维码的出现&#xff0c;简直是对二维码的“颠覆式创新”&#xff0c;直接把传统的二维码提升了一个维度&#xff01;作为设计师的我们怎么可以不会呢&#xff1f; 今天就教大家怎么制作这种超有艺…...

【C语言课程设计】图书管理系统

引言&#xff1a; 图书管理系统是一个重要的信息管理系统&#xff0c;对于图书馆和书店等机构来说&#xff0c;它能够方便地管理图书的录入、显示、查询、修改和删除等操作。本实验基于C语言开发了一个简单的图书管理系统&#xff0c;通过账户名和密码进行系统访问和权限控制&a…...

在 ArcGIS Pro 中使用 H3 创建蜂窝六边形

H3是Uber开发的分层索引系统,它使用六边形来平铺地球表面。H3在二十面体(一个具有20个三角形面和12个顶点的形状)上构建其六边形网格。由于仅用六边形不可能平铺二十面体,因此每个分辨率需要12个五边形来完成网格。分层索引网格意味着每个六边形都可以细分为子单元六边形。…...

创建Electron项目

一、使用vite 构建 electron项目 npm init vitelatest Need to install the following packages:create-vitelatest Ok to proceed? (y) y √ Project name: ... CertificateDownload √ Package name: ... certificatedownload √ Select a framework: Vue √ Select a var…...

Spring Boot实践一

一、Spring Boot简介 Spring Boot是一个基于Spring框架的快速开发应用程序的工具。它提供了一种快速、方便的方式来创建基于Spring的应用程序&#xff0c;而无需繁琐的配置。Spring Boot通过自动配置和约定大于配置的方式&#xff0c;使得开发者可以更加专注于业务逻辑的实现&…...

简单认识NoSQL的Redis配置与优化

文章目录 一、关系型数据库与非关系型数据库1、关系型数据库&#xff1a;2、非关系型数据库3、关系型数据库和非关系型数据库区别&#xff1a;4、非关系型数据库应用场景 二.Redis1、简介2、优点&#xff1a;3、Redis为什么这么快&#xff1f; 三、Redis 安装部署1、安装配置2、…...

开发一个RISC-V上的操作系统(二)—— 系统引导程序(Bootloader)

目录 文章传送门 一、什么是Bootloader 二、简单的启动程序 三、上板测试 文章传送门 开发一个RISC-V上的操作系统&#xff08;一&#xff09;—— 环境搭建_riscv开发环境_Patarw_Li的博客-CSDN博客 开发一个RISC-V上的操作系统&#xff08;二&#xff09;—— 系统引导…...

Git安装与学习

Git学习网站 Git安装教程 镜像网站 https://registry.npmmirror.com/binary.html 镜像下载是网站对服务器的一个保护措施之一&#xff0c;就是A站点下载的数据同 B站点下载的数据完全一样&#xff0c;b站点就是A站点的一面镜子。 所以镜像下载下来和原站点一摸一样。...

线程与协程

1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指&#xff1a;像函数调用/返回一样轻量地完成任务切换。 举例说明&#xff1a; 当你在程序中写一个函数调用&#xff1a; funcA() 然后 funcA 执行完后返回&…...

前端导出带有合并单元格的列表

// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...

Java - Mysql数据类型对应

Mysql数据类型java数据类型备注整型INT/INTEGERint / java.lang.Integer–BIGINTlong/java.lang.Long–––浮点型FLOATfloat/java.lang.FloatDOUBLEdouble/java.lang.Double–DECIMAL/NUMERICjava.math.BigDecimal字符串型CHARjava.lang.String固定长度字符串VARCHARjava.lang…...

镜像里切换为普通用户

如果你登录远程虚拟机默认就是 root 用户&#xff0c;但你不希望用 root 权限运行 ns-3&#xff08;这是对的&#xff0c;ns3 工具会拒绝 root&#xff09;&#xff0c;你可以按以下方法创建一个 非 root 用户账号 并切换到它运行 ns-3。 一次性解决方案&#xff1a;创建非 roo…...

python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...

如何为服务器生成TLS证书

TLS&#xff08;Transport Layer Security&#xff09;证书是确保网络通信安全的重要手段&#xff0c;它通过加密技术保护传输的数据不被窃听和篡改。在服务器上配置TLS证书&#xff0c;可以使用户通过HTTPS协议安全地访问您的网站。本文将详细介绍如何在服务器上生成一个TLS证…...

【单片机期末】单片机系统设计

主要内容&#xff1a;系统状态机&#xff0c;系统时基&#xff0c;系统需求分析&#xff0c;系统构建&#xff0c;系统状态流图 一、题目要求 二、绘制系统状态流图 题目&#xff1a;根据上述描述绘制系统状态流图&#xff0c;注明状态转移条件及方向。 三、利用定时器产生时…...

现代密码学 | 椭圆曲线密码学—附py代码

Elliptic Curve Cryptography 椭圆曲线密码学&#xff08;ECC&#xff09;是一种基于有限域上椭圆曲线数学特性的公钥加密技术。其核心原理涉及椭圆曲线的代数性质、离散对数问题以及有限域上的运算。 椭圆曲线密码学是多种数字签名算法的基础&#xff0c;例如椭圆曲线数字签…...

【git】把本地更改提交远程新分支feature_g

创建并切换新分支 git checkout -b feature_g 添加并提交更改 git add . git commit -m “实现图片上传功能” 推送到远程 git push -u origin feature_g...

【笔记】WSL 中 Rust 安装与测试完整记录

#工作记录 WSL 中 Rust 安装与测试完整记录 1. 运行环境 系统&#xff1a;Ubuntu 24.04 LTS (WSL2)架构&#xff1a;x86_64 (GNU/Linux)Rust 版本&#xff1a;rustc 1.87.0 (2025-05-09)Cargo 版本&#xff1a;cargo 1.87.0 (2025-05-06) 2. 安装 Rust 2.1 使用 Rust 官方安…...