Getting Started with Scss/Sass

Offical Doc:
https://sass-lang.com/

0. Preprocessing

CSS on its own can be fun, but stylesheets are getting larger, more complex, and harder to maintain. This is where a preprocessor can help. Sass has features that don’t exist in CSS yet like nesting, mixins, inheritance, and other nifty goodies that help you write robust, maintainable CSS.

You can also watch individual files or directories with the –watch flag. The watch flag tells Sass to watch your source files for changes, and re-compile CSS each time you save your Sass. If you wanted to watch (instead of manually build) your input.scss file, you’d just add the watch flag to your command, like so:

1
sass --watch input.scss output.css

1. Variables

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

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

body
font: 100% $font-stack
color: $primary-color

2. Nesting

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

li
display: inline-block

a
display: block
padding: 6px 12px
text-decoration: none

3. Partials

TODO…

4. Modules

TODO…

5. Mixins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
.info {
background: DarkGray;
box-shadow: 0 0 1px rgba(169, 169, 169, 0.25);
color: #fff;
}

.alert {
background: DarkRed;
box-shadow: 0 0 1px rgba(139, 0, 0, 0.25);
color: #fff;
}

.success {
background: DarkGreen;
box-shadow: 0 0 1px rgba(0, 100, 0, 0.25);
color: #fff;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@mixin theme($theme: DarkGray) {
background: $theme;
box-shadow: 0 0 1px rgba($theme, .25);
color: #fff;
}

.info {
@include theme;
}
.alert {
@include theme($theme: DarkRed);
}
.success {
@include theme($theme: DarkGreen);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
@mixin theme($theme: DarkGray)
background: $theme
box-shadow: 0 0 1px rgba($theme, .25)
color: #fff

.info
@include theme

.alert
@include theme($theme: DarkRed)

.success
@include theme($theme: DarkGreen)

6. Inheritance

TODO…

7. Operators

1
2
3
4
5
6
7
8
9
10
11
12
.container {
display: flex;
}

article[role=main] {
width: 62.5%;
}

aside[role=complementary] {
width: 31.25%;
margin-left: auto;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@use "sass:math";

.container {
display: flex;
}

article[role="main"] {
width: math.div(600px, 960px) * 100%;
}

aside[role="complementary"] {
width: math.div(300px, 960px) * 100%;
margin-left: auto;
}
1
2
3
4
5
6
7
8
9
10
11
@use "sass:math"

.container
display: flex

article[role="main"]
width: math.div(600px, 960px) * 100%

aside[role="complementary"]
width: math.div(300px, 960px) * 100%
margin-left: auto

Getting Started with Scss/Sass
https://www.hardyhu.cn/2024/12/14/Getting-Started-with-Scss-Sass/
Author
John Doe
Posted on
December 14, 2024
Licensed under