sass:常用备忘
一、变量
所有变量以$开头
$font_size: 12px; .container{ font-size: $font_size; }
如果变量嵌套在字符串中,需要写在#{}中
$side : left; .rounded { border-#{$side}: 1px solid #000; }
二、嵌套
层级嵌套
.container{ display: none; .header{ width: 100%; } }
属性嵌套,注意,border后需要加上冒号:
.container { border: { width: 1px; } }
可以通过&引用父元素,常用在各种伪类
.link{ &:hover{ color: green; } }
三、mixin
简单理解,是可以重用的代码块,通过@include 命令
// mixin @mixin focus_style { outline: none; } div { @include focus_style; }
编译后生成
div { outline: none; }
还可指定参数、缺省值
// 参数、缺省值 @mixin the_height($h: 200px) { height: $h; } .box_default { @include the_height; } .box_not_default{ @include the_height(100px); }
编译后生成
.box_default { height: 200px; } .box_not_default { height: 100px; }
四、继承
通过@extend,一个选择器可以继承另一个选择器的样式。例子如下
// 继承 .class1{ float: left; } .class2{ @extend .class1; width: 200px; }
编译后生成
.class1, .class2 { float: left; } .class2 { width: 200px; }
五、运算
直接上例子
.container{ position: relative; height: (200px/2); width: 100px + 200px; left: 50px * 2; top: 50px - 10px; }
编译后生成
.container { position: relative; height: 100px; width: 300px; left: 100px; top: 40px; }
插入文件
用@import 来插入外部文件
@import "outer.scss";
也可插入普通css文件
@import "outer.css";
自定义函数
通过@function 来自定义函数
@function higher($h){ @return $h * 2; } .container{ height: higher(100px); }
编译后输出
.container { height: 200px; }
注释
两种风格的注释
// 单行注释,编译后消失
/* 标准的CSS注释,会保留到编译后的代码中 */
如果重要的注释,压缩编译后还想保留,可在 /* 后面加上 !
/*! 重要注释,压缩编译也不会消失 */
参考:
http://www.ruanyifeng.com/blog/2012/06/sass.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义