講者:鄒適齊(Rex Tsou)
Official Site: sass-lang.com
在 CSS 的基礎上增加功能,由於是超集,在 SCSS 內可以直接寫 CSS,不會有錯誤(舊專案直接把 *.css 改成 *.scss 就可以了)
Syntactically Awesome StyleSheets
.block
width: 600px
height: 300px
.text
color: red
Sassy CSS (Sass version 3)
.card {
background: #EFEFEF;
.title {
font-size: 1.2em;
}
}
原始語言(便於編寫與閱讀)
目標語言(電腦執行效率高)
然後依照這個專案的說明來使用
自動化建置工具
// 設定檔範例:gulpfile.js
gulp.task('css', () => {
gulp.src('src/sass/**/*.scss')
.pipe($.plumber())
.pipe($.sass.sync({
outputStyle: 'nested', // expanded, nested, compact, compressed
precision: 10,
includePath: ['.'],
}).on('error', $.sass.logError))
.pipe($.autoprefixer({ browsers: ['last 2 versions'] }))
.pipe(gulp.dest('dist/css')) // output folder
.pipe(browserSync.stream())
.pipe($.notify("Compile Sass Complete!"))
});
GUI 介面 vs. CLI 介面
(大部分前端工程師使用 CLI,因為容易調整設定,且可以配合其他工具使用)
# terminal
sass in.scss out.css
// your project folder
// src/sass => dist/css
.
├── README.md
├── dist
│ ├── assets
│ │ ├── backgrounds
│ │ ├── icon
│ │ └── materials
│ ├── css
│ │ ├── layout
│ │ │ ├── about.css
│ │ │ ├── contact.css
│ │ │ └── index.css
│ │ └── main.css
│ ├── index.html
│ ├── js
│ │ └── bundle.js
│ ├── module
│ │ └── card-case.html
│ └── partial
│ ├── default-script.html
│ ├── footer.html
│ └── head.html
├── gulpfile.babel.js
├── index.html
├── package-lock.json
├── package.json
└── src
├── assets
│ ├── backgrounds
│ ├── icon
│ └── materials
├── js
│ ├── main.js
│ └── utils
│ ├── index.js
│ └── toggle-active.js
├── sass
│ ├── _base.scss
│ ├── _font.scss
│ ├── _variable.scss
│ ├── layout // 分頁面
│ │ ├── about.scss
│ │ ├── contact.scss
│ │ └── index.scss
│ ├── main.scss
│ ├── mixin // 共用常用的邏輯
│ │ ├── _assets.scss
│ │ ├── _breakpoint.scss
│ │ └── _mixins.scss
│ ├── module // 模組化的元件
│ │ ├── _card-case.scss
│ │ ├── _developing.scss
│ │ ├── _footer.scss
│ │ ├── _header.scss
│ │ └── _mask.scss
│ └── vendor // 第三方套件
│ └── normalize-scss
└── views
├── index.pug
├── module
│ └── card-case.pug
└── partial
├── default-script.pug
├── footer.pug
└── head.pug
// 我是單行註解,編譯後就不見了
/* 我是多行註解
而且編譯後還會在 */
...
/* 我是多行註解
而且編譯後還會在 */
用 $ 開頭即為變數,先定義後使用。
$color--red: #F55; // 定義紅色
$color--blue: #55F; // 定義藍色
.text {
color: $color--red;
border: $color--blue;
}
...
.text {
color: #F55;
border: #55F;
}
注意 scope(區域變數、全域變數)
.cute-box {
$width: 300px; // 區域變數,作用範圍僅有括號內
$height: 300px !global; // 全域
width: $width;
}
.another-box {
width: $width; // 這樣是拿不到的
height: $height; // 這樣可以
}
主要有 8 種資料型態,不需要死記
1.2
13
10px
"foo"
'bar'
baz
blue
#04a3f9
rgba(255, 0, 0, 0.5)
true
false
null
1.5em 1em 0 2em // 用空格分隔
Helvetica, Arial, sans-serif // 或用逗號分隔
(key1: value1, key2: value2)
如果覺得不夠...
- [Sass/SCSS 簡明入門教學](https://blog.techbridge.cc/2017/06/30/sass-scss-tutorial-introduction/) - [建立 Sass 環境](https://www.slideshare.net/chihchengwang3/sass-node-sass) - [Sass Guidelines](https://sass-guidelin.es/) - [Sass color maps](https://gist.github.com/akccakcctw/afdd74914e0b2d6b28ed815030da9f22) - [Gulp for beginners](https://css-tricks.com/gulp-for-beginners/)
- [撰寫可管理、可維護的 CSS 高階技巧](https://github.com/doggy8088/CSS-Guidelines) - [Medium’s CSS is actually pretty f***ing good](https://medium.com/@fat/mediums-css-is-actually-pretty-fucking-good-b8e2a6c78b06) - [CSS-TRICKS: BEM 101](https://css-tricks.com/bem-101/) - [css architecture for design systems](http://bradfrost.com/blog/post/css-architecture-for-design-systems/) - [Should You Chain or Extend CSS Classes?](https://frontstuff.io/should-you-chain-or-extend-css-classes)