当前位置: 首页 > 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站点的一面镜子。 所以镜像下载下来和原站点一摸一样。...

Ubuntu系统下交叉编译openssl

一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机&#xff1a;Ubuntu 20.04.6 LTSHost&#xff1a;ARM32位交叉编译器&#xff1a;arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动

一、前言说明 在2011版本的gb28181协议中&#xff0c;拉取视频流只要求udp方式&#xff0c;从2016开始要求新增支持tcp被动和tcp主动两种方式&#xff0c;udp理论上会丢包的&#xff0c;所以实际使用过程可能会出现画面花屏的情况&#xff0c;而tcp肯定不丢包&#xff0c;起码…...

【解密LSTM、GRU如何解决传统RNN梯度消失问题】

解密LSTM与GRU&#xff1a;如何让RNN变得更聪明&#xff1f; 在深度学习的世界里&#xff0c;循环神经网络&#xff08;RNN&#xff09;以其卓越的序列数据处理能力广泛应用于自然语言处理、时间序列预测等领域。然而&#xff0c;传统RNN存在的一个严重问题——梯度消失&#…...

抖音增长新引擎:品融电商,一站式全案代运营领跑者

抖音增长新引擎&#xff1a;品融电商&#xff0c;一站式全案代运营领跑者 在抖音这个日活超7亿的流量汪洋中&#xff0c;品牌如何破浪前行&#xff1f;自建团队成本高、效果难控&#xff1b;碎片化运营又难成合力——这正是许多企业面临的增长困局。品融电商以「抖音全案代运营…...

微服务商城-商品微服务

数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...

LCTF液晶可调谐滤波器在多光谱相机捕捉无人机目标检测中的作用

中达瑞和自2005年成立以来&#xff0c;一直在光谱成像领域深度钻研和发展&#xff0c;始终致力于研发高性能、高可靠性的光谱成像相机&#xff0c;为科研院校提供更优的产品和服务。在《低空背景下无人机目标的光谱特征研究及目标检测应用》这篇论文中提到中达瑞和 LCTF 作为多…...

API网关Kong的鉴权与限流:高并发场景下的核心实践

&#x1f525;「炎码工坊」技术弹药已装填&#xff01; 点击关注 → 解锁工业级干货【工具实测|项目避坑|源码燃烧指南】 引言 在微服务架构中&#xff0c;API网关承担着流量调度、安全防护和协议转换的核心职责。作为云原生时代的代表性网关&#xff0c;Kong凭借其插件化架构…...

C++--string的模拟实现

一,引言 string的模拟实现是只对string对象中给的主要功能经行模拟实现&#xff0c;其目的是加强对string的底层了解&#xff0c;以便于在以后的学习或者工作中更加熟练的使用string。本文中的代码仅供参考并不唯一。 二,默认成员函数 string主要有三个成员变量&#xff0c;…...

PydanticAI快速入门示例

参考链接&#xff1a;https://ai.pydantic.dev/#why-use-pydanticai 示例代码 from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel from pydantic_ai.providers.openai import OpenAIProvider# 配置使用阿里云通义千问模型 model OpenAIMode…...

Java严格模式withResolverStyle解析日期错误及解决方案

在Java中使用DateTimeFormatter并启用严格模式&#xff08;ResolverStyle.STRICT&#xff09;时&#xff0c;解析日期字符串"2025-06-01"报错的根本原因是&#xff1a;模式字符串中的年份格式yyyy被解释为YearOfEra&#xff08;纪元年份&#xff09;&#xff0c;而非…...