文章詳情頁(yè)
sass 常用備忘案例詳解
瀏覽:224日期:2022-06-03 10:14:46
一、變量
所有變量以$開頭
$font_size: 12px; .container{ font-size: $font_size; }
如果變量嵌套在字符串中,需要寫在#{}中
$side : left; .rounded { border-#{$side}: 1px solid #000; }
二、嵌套
層級(jí)嵌套
.container{ display: none; .header{ width: 100%; } }
屬性嵌套,注意,border后需要加上冒號(hào):
.container { border: { width: 1px; } }
可以通過(guò)&引用父元素,常用在各種偽類
.link{ &:hover{ color: green; } }
三、mixin
簡(jiǎn)單理解,是可以重用的代碼塊,通過(guò)@include 命令
// mixin @mixin focus_style { outline: none; } div { @include focus_style; }
編譯后生成
div { outline: none; }
還可指定參數(shù)、缺省值
// 參數(shù)、缺省值 @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; }
四、繼承
通過(guò)@extend,一個(gè)選擇器可以繼承另一個(gè)選擇器的樣式。例子如下
// 繼承 .class1{ float: left; } .class2{ @extend .class1; width: 200px; }
編譯后生成
.class1, .class2 { float: left; } .class2 { width: 200px; }
五、運(yùn)算
直接上例子
.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 來(lái)插入外部文件
@import "outer.scss";
也可插入普通css文件
@import "outer.css";
自定義函數(shù)
通過(guò)@function 來(lái)自定義函數(shù)
@function higher($h){ @return $h * 2; } .container{ height: higher(100px); }
編譯后輸出
.container { height: 200px; }
注釋
兩種風(fēng)格的注釋
// 單行注釋,編譯后消失
/* 標(biāo)準(zhǔn)的CSS注釋,會(huì)保留到編譯后的代碼中 */
如果重要的注釋,壓縮編譯后還想保留,可在 /* 后面加上 !
/*! 重要注釋,壓縮編譯也不會(huì)消失 */
參考:
到此這篇關(guān)于sass 常用備忘案例詳解的文章就介紹到這了,更多相關(guān)sass 常用備忘內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. vue使用Sass時(shí)報(bào)錯(cuò)問題的解決方法2. node.js降低版本的方式詳解(解決sass和node.js沖突問題)3. vue-element-admin中node-sass換成dart-sass,安裝依賴報(bào)code 128多種問題的解決方法4. vue在install時(shí)node-sass@4.14.1 postinstall:node scripts/build.js錯(cuò)誤解決5. 解決vue中使用less/sass及使用中遇到無(wú)效的問題6. 解決vue cli4升級(jí)sass-loader(v8)后報(bào)錯(cuò)問題
排行榜
