Scss - 简单笔记

  原文链接:scss 教程

  手头上疯狂在用 scss,虽然可以在里面写原生的 css, 但是为了保持风格的一致性,还是滚去看了看 scss 文档。

 

一、变量

  变量的引入是 scss 的一个核心特性,变量用来存储需要在CSS中复用的信息,例如颜色和字体。SASS通过$符号去声明一个变量。

1
2
3
4
5
6
7
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
 
body {
  font: 100% $font-stack;
  color: $primary-color;
}

  编译后,变量的值会替换所有他们的位置。

 

二、嵌套

  SASS允许开发人员以嵌套的方式使用CSS,但是过度的使用嵌套会让产生的CSS难以维护,因此要慎用,下面的例子表达了一个典型的网站导航样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
 
  li { display: inline-block; }
 
  a {
    display: block;
    padding: 6px 12px;
    text-decoration: none;
  }
}

   编译后:

1
2
3
4
5
6
7
8
9
10
nav ul {
  margin: 0;
  padding: 0;
  list-style: none; }
nav li {
  display: inline-block; }
nav a {
  display: block;
  padding: 6px 12px;
  text-decoration: none; }

 

三、引入

  SASS能够将代码分割为多个片段,并以underscore风格的下划线作为其命名前缀(_partial.scss),SASS会通过这些下划线来辨别哪些文件是SASS片段,并且不让片段内容直接生成为CSS文件,从而只是在使用@import指令的位置被导入。

  CSS原生的@import会通过额外的HTTP请求获取引入的样式片段,而SASS的@import则会直接将这些引入的片段合并至当前CSS文件,并且不会产生新的HTTP请求。

1
2
3
4
5
6
// _reset.scss
 
html, body, ul, ol {
  margin0;
  padding: 0;
}

 

1
2
3
4
5
6
7
// base.scss
 
@import '../xx/xx/_reset';
body {
  font: 100% Helvetica, sans-serif;
  background-color: #efefef;
}

  编译后:

1
2
3
4
5
6
7
html, body, ul, ol {
  margin: 0;
  padding: 0; }
 
body {
  font: 100% Helvetica, sans-serif;
  background-color: #efefef; }

 

四、混合

  混合(Mixin)用来分组那些需要在页面中复用的CSS声明,开发人员可以通过向Mixin传递变量参数来让代码更加灵活,该特性在添加浏览器兼容性前缀的时候非常有用,SASS目前使用@mixin name指令来进行混合操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 定义
@mixin border-radius($radius) {
    border-radius: $radius;
    -ms-border-radius: $radius;
    -moz-border-radius: $radius;
    -webkit-border-radius: $radius;
};
 
// 在需要的地方事由 @include 来引入并传值
#father {
    @include border-radius(10px);
    width: 60%;
    margin: 0 auto;
}

上面的代码建立了一个名为 border-radius 的 Mixin,并传递了一个变量 $radius 作为参数,然后在后续代码中通过 @include border-radius(10px) 使用该Mixin,最终编译的结果如下:

1
2
3
4
5
6
.box {
    border-radius: 10px;
    -ms-border-radius: 10px;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
}

 

五、继承

  继承是SASS中非常重要的一个特性,可以通过@extend指令在选择器之间复用CSS属性,并且不会产生冗余的代码,下面例子将会通过SASS提供的继承机制建立一系列样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 这段代码不会被输出到最终生成的CSS文件,因为它没有被任何代码所继承。
%other-styles {
  display: flex;
  flex-wrap: wrap;
}
 
// 下面代码会正常输出到生成的CSS文件,因为它被其接下来的代码所继承。
%message-common {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}
 
.message {
  @extend %message-common;
}
 
.success {
  @extend %message-common;
  border-color: green;
}
 
.error {
  @extend %message-common;
  border-color: red;
}
 
.warning {
  @extend %message-common;
  border-color: yellow;
}

 上面代码将.message中的CSS属性应用到了.success、.error、.warning上面,魔法将会发生在最终生成的CSS当中。这种方式能够避免在HTML元素上书写多个class选择器,最终生成的CSS样式是下面这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.message, .success, .error, .warning {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}
 
.success {
  border-color: green;
}
 
.error {
  border-color: red;
}
 
.warning {
  border-color: yellow;
}

 

五、操作符

  SASS提供了标准的算术运算符,例如+、-、*、/、%。在接下来的例子里,我们尝试在aside和article选择器当中对宽度进行简单的计算。

1
2
3
4
5
6
7
8
9
10
11
.container { width: 100%; }
 
article[role="main"] {
  float: left;
  width: 600px / 960px * 100%;
}
 
aside[role="complementary"] {
  float: right;
  width: 300px / 960px * 100%;
}

   上面代码最终编译后的样子是:

1
2
3
4
5
6
7
8
9
10
11
12
13
.container {
  width: 100%;
}
 
article[role="main"] {
  float: left;
  width: 62.5%;
}
 
aside[role="complementary"] {
  float: right;
  width: 31.25%;
}

 

六、引用父级选择器"&"

  Scss使用"&"关键字在CSS规则中引用父级选择器,例如在嵌套使用伪类选择器的场景下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*===== SCSS =====*/
a {
  font-weight: bold;
  text-decoration: none;
  &:hover { text-decoration: underline; }
  body.firefox & { font-weight: normal; }
}
 
/*===== CSS =====*/
a {
  font-weight: bold;
  text-decoration: none;
}
 
a:hover {
  text-decoration: underline;
}
 
body.firefox a {
  font-weight: normal;
}

   无论CSS规则嵌套的深度怎样,关键字"&"都会使用父级选择器级联替换全部其出现的位置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*===== SCSS =====*/
#main {
  color: black;
  a {
    font-weight: bold;
    &:hover { color: red; }
  }
}
 
/*===== CSS =====*/
#main {
  color: black;
}
 
#main a {
  font-weight: bold;
}
#main a:hover {
  color: red;
}

   "&"必须出现在复合选择器开头的位置,后面再连接自定义的后缀,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*===== SCSS =====*/
#main {
  color: black;
  &-sidebar { border: 1px solid; }
}
 
/*===== CSS =====*/
#main {
  color: black;
}
 
#main-sidebar {
    border: 1px solid;
}

 

posted @   shiweiqianju  阅读(342)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示