Menu

  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay
  • Home
  • Work
    • Cloud
      • Virtualization
      • IaaS
      • PaaS
    • Java
    • Go
    • C
    • C++
    • JavaScript
    • PHP
    • Python
    • Architecture
    • Others
      • Assembly
      • Ruby
      • Perl
      • Lua
      • Rust
      • XML
      • Network
      • IoT
      • GIS
      • Algorithm
      • AI
      • Math
      • RE
      • Graphic
    • OS
      • Linux
      • Windows
      • Mac OS X
    • BigData
    • Database
      • MySQL
      • Oracle
    • Mobile
      • Android
      • IOS
    • Web
      • HTML
      • CSS
  • Life
    • Cooking
    • Travel
    • Gardening
  • Gallery
  • Video
  • Music
  • Essay

Sass学习笔记

23
Feb
2015

Sass学习笔记

By Alex
/ in CSS
0 Comments
简介

Sass是对CSS语言的扩展,为其提供了:变量、嵌套规则、混入、内联(inline)导入等语言特性。

语法风格

Sass具有两套语法风格。

一种是旧的、基于缩进的语法风格,通常称为Sass,扩展名.sass 。这种语法风格使用缩进而不是代码块来表示嵌套规则,使用换行符而非分号来分隔属性,因而比较简洁。

另一种是新的、基于代码块的语法风格,通常称为Sassy CSS,扩展名.scss 。符合CSS3语法的文件都可以是SCSS文件。

使用命令行,可以在这两种格式进行转换:

Shell
1
2
3
4
5
# 将SASS转换为SCSS格式
sass-convert style.sass style.scss
 
# 将 SCSS转换为SASS格式
$ sass-convert style.scss style.sass

本文主要使用SCSS的语法风格。

使用SASS
基于命令行

Sass命令行基于Ruby语言开发,你需要先安装、配置好Ruby环境 。然后执行:

Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装Sass命令行工具
gem install sass
 
# 转换为标准CSS文件
sass input.scss output.css
# 指定输出风格:
# nested:嵌套缩进的CSS代码,默认值
# expanded:没有缩进的、扩展的代码代码
# compact:简洁格式的代码代码
# compressed:压缩后的代码代码,用于生产环境
sass --style compressed test.sass test.css
 
# 监控文件的改动并自动更新
sass --watch input.scss:output.css
CSS语言扩展

本章内容介绍Sass对标准CSS语言的扩展,Sass的某些特性与未来的CSS 4规范是兼容的。

嵌套规则

Sass允许一个CSS规则嵌套编写在另外一个规则的内部,这样可以避免重复书写父选择器:

Sass
1
2
3
4
5
6
7
8
9
#main p {
    color: #00ff00;
    width: 97%;
    // 嵌套规则
    .redbox {
        background-color: #ff0000;
        color: #000000;
    }
}

转换后的CSS为:

CSS
1
2
3
4
5
6
7
8
#main p {
    color: #00ff00;
    width: 97%;
}
#main p .redbox {
    background-color: #ff0000;
    color: #000000;
}
引用父选择器

Sass支持以 & 来引用父选择器,转换时,该符号使用父选择器代替:

Sass
1
2
3
4
5
6
7
8
9
10
11
a {
    font-weight: bold;
    // 父选择器放在前面
    &:hover {
        text-decoration: underline;
    }
    // 也可以放在后面
    body.firefox & {
        font-weight: normal;
    }
}

转换后的结果:

CSS
1
2
3
4
5
6
7
8
9
a {
    font-weight: bold;
}
a:hover {
    text-decoration: underline;
}
body.firefox a {
    font-weight: normal;
}
嵌套属性

你可以把某些同类属性合并到一个块中:

Sass
1
2
3
4
5
6
7
.funky {
    font: {
        family: fantasy;
        size: 30em;
        weight: bold;
    }
}
注释

Sass支持两种风格的注释: /* */ 和 // 

SassScript

除了普通的CSS语言扩展外,Sass还支持脚本机制。你可以定义变量、使用函数。任何CSS属性都可以用脚本来指定。

变量

你可以使用 $ 前缀声明SassScript变量:

Sass
1
2
3
4
5
6
$width: 5em;
 
#main {
    // 引用变量
    width: $width;
}

变量的作用域取决于它所处于的块。除了使用$前缀以外,旧式的 ! 前缀仍然被支持;除了使用 : 定义变量值以外,旧式的 = 依然被支持。

数据类型
类型 说明
数字 举例:1.2  10  10px
字符串

可以没有引号、单引号包围、双引号包围

通常SCSS中使用哪种格式的字符串,则转换后的CSS也使用哪一种。例外情况是 #{} 插值:

Sass
1
2
3
4
5
6
7
@mixin firefox-message($selector) {
    body.firefox #{$selector}:before {
        content: "Hi, Firefox users!";
    }
}
 
@include firefox-message(".header");

转换后引用字符串会变为非引用格式:

CSS
1
2
3
4
/* .header周围的引号被自动去掉 */
body.firefox .header:before {
    content: "Hi, Firefox users!";
}
颜色 举例:#FF0000  red rgba(255, 0, 0, 0.5)
布尔 true  false
空值 null
值列表

以空格或者逗号分隔。例如: Helvetica, Arial, sans-serif 

某些SassScript函数能够操控列表,例如nth函数访问列表的成员,join函数把列表连接为字符串,append函数附加新值到列表中

列表中可以包含其它列表,例如 1px 2px, 5px 6px ,如果内外列表使用相同的分隔符,则必须用括号来区分: (1px 2px) (5px 6px) 

映射

例如: (key1: value1, key2: value2) 

与列表不同,映射必须以括号为界,且必须以逗号分隔条目

某些SassScript函数能够操控映射,例如map-get可以获取一个值,map-merge可以添加条目

映射可以作为列表使用,它相当于键值对的列表

操作符
操作符 说明
==   != 相等性判断,可以用于任何数据类型
+ - * / %

算术运算符。数字类型支持此类运算符,单位的转换会自动进行

由于CSS允许 / 作为分隔数字的房时,Sass必须支持它。只有使用函数、变量时, / 才会被解析为除法运算符:

Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
p {
    font: 10px/8px;             // 不解释为除法,原样输出
    $width: 1000px;
    width: $width/2;            // 使用变量,解释为除法
    width: round(1.5)/2;        // 使用函数,解释为除法
    height: (500px/2);          // 使用括号,解释为除法
    margin-left: 5px + 8px/2px; // 使用+号,解释为除法
    font: (italic bold 10px/8px); // 列表中,不解释为除法,原样输出
}
/* 转换结果 */
p {
    font: 10px/8px;
    width: 500px;
    width: 1;
    height: 250px;
    margin-left: 9px;
    font: italic bold 10px/8px;
}

使用 - 操作符时,需要注意:

  1. 作为减法运算符时,前后应都有空格
  2. 作为负号或者一元操作符时,前面应有空格,后面不能有空格
  3. 在基于空格的列表中使用一元操作符,需要用括号包围: 10px (-$var)  

使用 + 可以进行字符串连接:

Sass
1
2
3
4
5
6
7
p {
    cursor: e + -resize;
}
// 转换后:
p {
    cursor: e-resize;
}

颜色支持算术运算符,RGB将会分别运算:

Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
p {
    color: #111 + #222;
}
// 转换后:
p {
    color: #333333;
}
 
p {
    color: rgba(255, 0, 0, 0.75) + rgba(0, 255, 0, 0.75);
}
// 透明度不受影响:
p {
    color: rgba(255, 255, 0, 0.75);
}
< > <= >= 关系运算符,数字类型支持此类运算符
and  or  not 用于布尔值的运算
函数

SassScript定义了一些额外的函数,调用方式与普通CSS函数一致:

Sass
1
2
3
4
5
6
7
p {
    color: hsl(0, 100%, 50%);
}
// 转换后:
p {
    color: red;
}
关键字参数

调用SassScript函数时,可以明确指定参数名称,这样可以改变传参顺序:

Sass
1
2
3
p {
    color: hsl($hue: 0, $saturation: 100%, $lightness: 50%);
}
常用函数

所有函数的列表:http://sass-lang.com/documentation/Sass/Script/Functions.html

函数 说明
lighten 提高颜色亮度: lighten(#cc3, 10%) // #d6d65c 
darken 降低颜色亮度
grayscale 灰度化颜色: grayscale(#cc3) // #808080 
complement 获得补偿色
invert 获得反色
插值#{}

你可以在选择器、属性名处使用插值来引用变量:

Sass
1
2
3
4
5
6
7
8
9
$name: foo;
$attr: border;
p.#{$name} {
    #{$attr}-color: blue;
}
// 转换后
p.foo {
    border-color: blue;
}

在属性值中,通常直接使用变量。如果想把插值周围的操作符作为普通CSS字符看待,可以使用插值:

Sass
1
2
3
4
5
6
7
8
9
10
11
p {
    $font-size: 12px;
    $line-height: 30px;
    // 这里的 / 不会作为除法看待
    font: #{$font-size}/#{$line-height};
}
 
// 转换后
p {
    font: 12px/30px;
}
&与SassScript 

在SassScript中,&符号同样代表父选择器:

Sass
1
2
3
4
.foo.bar .baz.bang, .bip.qux {
    $selector: &;
    // 这里变量$selector的值为 ((".foo.bar" ".baz.bang"), ".bip.qux")
}

如果父选择器不存在,则&的值为null。你可以依此判断父选择器是否存在:

Sass
1
2
3
4
5
6
7
8
9
10
11
@mixin does-parent-exist {
    @if & {
        &:hover {
            color: red;
        }
    } @else {
        a {
            color: red;
        }
    }
}
默认值

特殊语法 !default 用来给变量赋默认值:

Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$content: "First content";
// 如果$content没有赋值过,则设置为Second content?
$content: "Second content?" !default;
$new_content: "First time reference" !default;
 
#main {
    content: $content;
    new-content: $new_content;
}
 
// 转换后:
#main {
    content: "First content";
    new-content: "First time reference";
}
@规则和指令
@extend

从另外一个选择器继承属性:

Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.class1 {
    border: 1px solid #ddd;
}
 
.class2 {
    @extend .class1;
    font-size: 120%;
}
 
 
/* 转换后 */
.class1, .class2 {
    border: 1px solid #ddd;  // .class2选择器自动获得该属性
}
 
.class2 {
    font-size: 120%;
}

在同一个规则中,该指令可以出现多次。

@mixin

混入,定义一个可复用的代码块:

Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
@mixin left {
    float: left;
    margin-left: 10px;
}
div {
    @include left;
}
 
/* 转换后 */
div {
    float: left;
    margin-left: 10px;
}

上述示例类似于@extend,但是混入本身不会引入选择器定义。混入的强大之处在于它支持参数: 

Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 指定一个value参数,还提供了默认值
@mixin left($value: 10px) {
    float: left;
    margin-right: $value;
}
 
div {
    @include left(20px);
    // 使用默认值
    @include left();
    @include left;
}
 
/* 转换后:*/
div {
    float: left;
    margin-right: 20px;
}
@import

用于把外部文件插入到当前文档中:

Sass
1
2
3
4
5
@import "path/filename.scss";
@import "http://foo.com/bar";
@import url(foo);
// 同时引入多个文件
@import "rounded-corners", "text-shadow";

如果插入的是CSS文件,则等价于CSS3的@import指令。 

如果你不希望Sass编译某个scss文件,可以让他的名字以下划线开头。这种文件可以用于被导入,导入时需要去掉下划线前缀:

Sass
1
2
// 导入_colors.scss
@import "colors";

@import可以嵌套在规则内部使用:

example.scss
Sass
1
2
3
.example {
  color: red;
}

Sass
1
2
3
4
5
6
7
8
#main {
    @import "example";
}
 
// 转换后
#main .example {
    color: red;
}
@function

用于自定义函数:

Sass
1
2
3
4
5
6
7
8
9
10
11
@function square($n) {
    @return $n * $n;
}
 
#squre {
    size: square(10);
}
 
// 转换后
#squre {
    size: 10000; }
流程控制

SassScript支持条件分支、循环等基本的流程控制。

@if
Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$width: 100;
div {
    @if $width >= 100 {
        height: 100px;
    } @else if ($width <50) {
        height: 50px;
    } @else {
        height: 75px;
    }
}
 
// 转换后:
div {
    height: 100px;
}
@for

注意以开区间结束:

Sass
1
2
3
4
5
6
7
8
9
10
11
12
@for $i from 1 to 3 {
    .kssi-header-#{$i} {
        font-size: $i*10px;
    }
}
 
// 转换后:
.kssi-header-1 {
    font-size: 10px; }
 
.kssi-header-2 {
    font-size: 20px; }
@while
Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$i: 0;
@while $i < 2 {
    .icon-user-#{$i} {
        background-image: url("user-#{$i}.jpg");
    }
    $i : $i + 1;
}
 
// 转换后
 
.icon-user-0 {
    background-image: url("user-0.jpg"); }
 
.icon-user-1 {
    background-image: url("user-1.jpg"); }
@each
Sass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@each $user in user admin anonymous {
    .icon-#{$user}{
        background-image: url("#{$user}.png");
    }
}
 
// 转换后:
.icon-user {
    background-image: url("user.png"); }
 
.icon-admin {
    background-image: url("admin.png"); }
 
.icon-anonymous {
    background-image: url("anonymous.png"); }

 

← 羊年夜饭
Go语言系统编程 →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Related Posts

  • CSS3学习笔记
  • CSS知识集锦

Recent Posts

  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
  • A Comprehensive Study of Kotlin for Java Developers
  • 背诵营笔记
  • 利用LangChain和语言模型交互
  • 享学营笔记
ABOUT ME

汪震 | Alex Wong

江苏淮安人,现居北京。目前供职于腾讯云,专注容器方向。

GitHub:gmemcc

Git:git.gmem.cc

Email:gmemjunk@gmem.cc@me.com

ABOUT GMEM

绿色记忆是我的个人网站,域名gmem.cc中G是Green的简写,MEM是Memory的简写,CC则是我的小天使彩彩名字的简写。

我在这里记录自己的工作与生活,同时和大家分享一些编程方面的知识。

GMEM HISTORY
v2.00:微风
v1.03:单车旅行
v1.02:夏日版
v1.01:未完成
v0.10:彩虹天堂
v0.01:阳光海岸
MIRROR INFO
Meta
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Recent Posts
  • Investigating and Solving the Issue of Failed Certificate Request with ZeroSSL and Cert-Manager
    In this blog post, I will walk ...
  • A Comprehensive Study of Kotlin for Java Developers
    Introduction Purpose of the Study Understanding the Mo ...
  • 背诵营笔记
    Day 1 Find Your Greatness 原文 Greatness. It’s just ...
  • 利用LangChain和语言模型交互
    LangChain是什么 从名字上可以看出来,LangChain可以用来构建自然语言处理能力的链条。它是一个库 ...
  • 享学营笔记
    Unit 1 At home Lesson 1 In the ...
  • K8S集群跨云迁移
    要将K8S集群从一个云服务商迁移到另外一个,需要解决以下问题: 各种K8S资源的迁移 工作负载所挂载的数 ...
  • Terraform快速参考
    简介 Terraform用于实现基础设施即代码(infrastructure as code)—— 通过代码( ...
  • 草缸2021
    经过四个多月的努力,我的小小荷兰景到达极致了状态。

  • 编写Kubernetes风格的APIServer
    背景 前段时间接到一个需求做一个工具,工具将在K8S中运行。需求很适合用控制器模式实现,很自然的就基于kube ...
  • 记录一次KeyDB缓慢的定位过程
    环境说明 运行环境 这个问题出现在一套搭建在虚拟机上的Kubernetes 1.18集群上。集群有三个节点: ...
  • eBPF学习笔记
    简介 BPF,即Berkeley Packet Filter,是一个古老的网络封包过滤机制。它允许从用户空间注 ...
  • IPVS模式下ClusterIP泄露宿主机端口的问题
    问题 在一个启用了IPVS模式kube-proxy的K8S集群中,运行着一个Docker Registry服务 ...
  • 念爷爷
      今天是爷爷的头七,十二月七日、阴历十月廿三中午,老人家与世长辞。   九月初,回家看望刚动完手术的爸爸,发

  • 6 杨梅坑

  • liuhuashan
    深圳人才公园的网红景点 —— 流花山

  • 1 2020年10月拈花湾

  • 内核缺陷触发的NodePort服务63秒延迟问题
    现象 我们有一个新创建的TKE 1.3.0集群,使用基于Galaxy + Flannel(VXLAN模式)的容 ...
  • Galaxy学习笔记
    简介 Galaxy是TKEStack的一个网络组件,支持为TKE集群提供Overlay/Underlay容器网 ...
TOPLINKS
  • Zitahli's blue 91 people like this
  • 梦中的婚礼 64 people like this
  • 汪静好 61 people like this
  • 那年我一岁 36 people like this
  • 为了爱 28 people like this
  • 小绿彩 26 people like this
  • 彩虹姐姐的笑脸 24 people like this
  • 杨梅坑 6 people like this
  • 亚龙湾之旅 1 people like this
  • 汪昌博 people like this
  • 2013年11月香山 10 people like this
  • 2013年7月秦皇岛 6 people like this
  • 2013年6月蓟县盘山 5 people like this
  • 2013年2月梅花山 2 people like this
  • 2013年淮阴自贡迎春灯会 3 people like this
  • 2012年镇江金山游 1 people like this
  • 2012年徽杭古道 9 people like this
  • 2011年清明节后扬州行 1 people like this
  • 2008年十一云龙公园 5 people like this
  • 2008年之秋忆 7 people like this
  • 老照片 13 people like this
  • 火一样的六月 16 people like this
  • 发黄的相片 3 people like this
  • Cesium学习笔记 90 people like this
  • IntelliJ IDEA知识集锦 59 people like this
  • 基于Kurento搭建WebRTC服务器 38 people like this
  • Bazel学习笔记 37 people like this
  • PhoneGap学习笔记 32 people like this
  • NaCl学习笔记 32 people like this
  • 使用Oracle Java Mission Control监控JVM运行状态 29 people like this
  • Ceph学习笔记 27 people like this
  • 基于Calico的CNI 27 people like this
Tag Cloud
ActiveMQ AspectJ CDT Ceph Chrome CNI Command Cordova Coroutine CXF Cygwin DNS Docker eBPF Eclipse ExtJS F7 FAQ Groovy Hibernate HTTP IntelliJ IO编程 IPVS JacksonJSON JMS JSON JVM K8S kernel LB libvirt Linux知识 Linux编程 LOG Maven MinGW Mock Monitoring Multimedia MVC MySQL netfs Netty Nginx NIO Node.js NoSQL Oracle PDT PHP Redis RPC Scheduler ServiceMesh SNMP Spring SSL svn Tomcat TSDB Ubuntu WebGL WebRTC WebService WebSocket wxWidgets XDebug XML XPath XRM ZooKeeper 亚龙湾 单元测试 学习笔记 实时处理 并发编程 彩姐 性能剖析 性能调优 文本处理 新特性 架构模式 系统编程 网络编程 视频监控 设计模式 远程调试 配置文件 齐塔莉
Recent Comments
  • qg on Istio中的透明代理问题
  • heao on 基于本地gRPC的Go插件系统
  • 黄豆豆 on Ginkgo学习笔记
  • cloud on OpenStack学习笔记
  • 5dragoncon on Cilium学习笔记
  • Archeb on 重温iptables
  • C/C++编程:WebSocketpp(Linux + Clion + boostAsio) – 源码巴士 on 基于C/C++的WebSocket库
  • jerbin on eBPF学习笔记
  • point on Istio中的透明代理问题
  • G on Istio中的透明代理问题
  • 绿色记忆:Go语言单元测试和仿冒 on Ginkgo学习笔记
  • point on Istio中的透明代理问题
  • 【Maven】maven插件开发实战 – IT汇 on Maven插件开发
  • chenlx on eBPF学习笔记
  • Alex on eBPF学习笔记
  • CFC4N on eBPF学习笔记
  • 李运田 on 念爷爷
  • yongman on 记录一次KeyDB缓慢的定位过程
  • Alex on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • will on Istio中的透明代理问题
  • haolipeng on 基于本地gRPC的Go插件系统
  • 吴杰 on 基于C/C++的WebSocket库
©2005-2025 Gmem.cc | Powered by WordPress | 京ICP备18007345号-2