logo
🎨 前端开发

Sass

Sass Cheat Sheet - 快速参考指南,收录常用语法、命令与实践。

📂 分类 · 前端开发🧭 Markdown 速查🏷️ 2 个标签
#sass#scss
向下滚动查看内容
返回全部 Cheat Sheets

Sass Basics

Introduction
Variables
SCSS
滚动查看更多
$defaultLinkColor: #46eac2;

a {
	color: $defaultLinkColor;
}
String interpolation
SCSS
滚动查看更多
$wk: -webkit-;

.rounded-box {
	#{$wk}border-radius: 4px;
}
Comments
SCSS
滚动查看更多
/*
 Block comments
 Block comments
 Block comments
*/

// Line comments
Mixins
SCSS
滚动查看更多
@mixin heading-font {
	font-family: sans-serif;
	font-weight: bold;
}
h1 {
	@include heading-font;
}

See: Mixins

Nesting
SCSS
滚动查看更多
.markdown-body {
	a {
		color: blue;
		&:hover {
			color: red;
		}
	}
}

to properties

SCSS
滚动查看更多
text: {
	// like text-align: center
	align: center;
	// like text-transform: uppercase
	transform: uppercase;
}
Extend
SCSS
滚动查看更多
.button {
    ···
}
SCSS
滚动查看更多
.push-button {
	@extend .button;
}
@import
SCSS
滚动查看更多
@import './other_sass_file';
@import '/code', 'lists';

// Plain CSS @imports
@import 'theme.css';
@import url(theme);

The .sass or .sass extension is optional.

Sass Mixins

Parameters
SCSS
滚动查看更多
@mixin font-size($n) {
	font-size: $n * 1.2em;
}
SCSS
滚动查看更多
body {
	@include font-size(2);
}
Default values
SCSS
滚动查看更多
@mixin pad($n: 10px) {
	padding: $n;
}
SCSS
滚动查看更多
body {
	@include pad(15px);
}
Default variable
SCSS
滚动查看更多
$default-padding: 10px;

@mixin pad($n: $default-padding) {
	padding: $n;
}

body {
	@include pad(15px);
}

Sass Color functions

rgba
SCSS
滚动查看更多
rgb(100, 120, 140)
rgba(100, 120, 140, .5)
rgba($color, .5)
Mixing
SCSS
滚动查看更多
mix($a, $b, 10%)   // 10% a, 90% b
Modifying HSLA
SCSS
滚动查看更多
darken($color, 5%)
lighten($color, 5%)
SCSS
滚动查看更多
saturate($color, 5%)
desaturate($color, 5%)
grayscale($color)
SCSS
滚动查看更多
adjust-hue($color, 15deg)
complement($color)    // like adjust-hue(_, 180deg)
invert($color)
SCSS
滚动查看更多
fade-in($color, .5)   // aka opacify()
fade-out($color, .5)  // aka transparentize()
rgba($color, .5)      // sets alpha to .5
Getting individual values

HSLA

SCSS
滚动查看更多
hue($color)         // 0deg..360deg
saturation($color)  // 0%..100%
lightness($color)   // 0%..100%
alpha($color)       // 0..1 (aka opacity())

RGB

SCSS
滚动查看更多
red($color)         // 0..255
green($color)
blue($color)

See: hue(), red()

Adjustments
SCSS
滚动查看更多
// Changes by fixed amounts
adjust-color($color, $blue: 5)
adjust-color($color, $lightness: -30%) // darken(_, 30%)
adjust-color($color, $alpha: -0.4)     // fade-out(_, .4)
adjust-color($color, $hue: 30deg)      // adjust-hue(_, 15deg)
SCSS
滚动查看更多
// Changes via percentage
scale-color($color, $lightness: 50%)
SCSS
滚动查看更多
// Changes one property completely
change-color($color, $hue: 180deg)
change-color($color, $blue: 250)

Supported: $red, $green, $blue, $hue, $saturation, $lightness, $alpha

Sass Other functions

Strings
SCSS
滚动查看更多
unquote('hello')
quote(hello)
SCSS
滚动查看更多
to-upper-case(hello)
to-lower-case(hello)
SCSS
滚动查看更多
str-length(hello world)
str-slice(hello, 2, 5)     // "ello" - it's 1-based, not 0-based
str-insert("abcd", "X", 1) // "Xabcd"
Units
SCSS
滚动查看更多
unit(3em)        // 'em'
unitless(100px)  // false
Numbers
SCSS
滚动查看更多
floor(3.5)
ceil(3.5)
round(3.5)
abs(3.5)
SCSS
滚动查看更多
min(1, 2, 3)
max(1, 2, 3)
SCSS
滚动查看更多
percentage(.5)   // 50%
random(3)        // 0..3
Misc
SCSS
滚动查看更多
variable-exists(red)    // checks for $red
mixin-exists(red-text)  // checks for @mixin red-text
function-exists(redify)
SCSS
滚动查看更多
global-variable-exists(red)
SCSS
滚动查看更多
selector-append('.menu', 'li', 'a')   // .menu li a
selector-nest('.menu', '&:hover li')  // .menu:hover li
selector-extend(...)
selector-parse(...)
selector-replace(...)
selector-unify(...)

Sass Feature checks

Feature check
SCSS
滚动查看更多
feature-exists(global-variable-shadowing)
Features
  • global-variable-shadowing
  • extend-selector-pseudoclass
  • units-level-3
  • at-error

Sass Loops

For loops
SCSS
滚动查看更多
@for $i from 1 through 4 {
	.item-#{$i} {
		left: 20px * $i;
	}
}
Each loops (simple)
SCSS
滚动查看更多
$menu-items: home about contact;

@each $item in $menu-items {
	.photo-#{$item} {
		background: url('#{$item}.jpg');
	}
}
Each loops (nested)
SCSS
滚动查看更多
$backgrounds: (home, 'home.jpg'), (about, 'about.jpg');

@each $id, $image in $backgrounds {
	.photo-#{$id} {
		background: url($image);
	}
}
While loops
SCSS
滚动查看更多
$i: 6;
@while $i > 0 {
	.item-#{$i} {
		width: 2em * $i;
	}
	$i: $i - 2;
}

Sass Other features

Conditionals
SCSS
滚动查看更多
@if $position == 'left' {
	position: absolute;
	left: 0;
} @else if $position == 'right' {
	position: absolute;
	right: 0;
} @else {
	position: static;
}
Interpolation
SCSS
滚动查看更多
.#{$klass} { ... }      // Class
call($function-name)    // Functions

@media #{$tablet}
font: #{$size}/#{$line-height}
url("#{$background}.jpg")
Lists
SCSS
滚动查看更多
$list: (a b c);

nth($list, 1)  // starts with 1
length($list)

@each $item in $list { ... }
Maps
SCSS
滚动查看更多
$map: (key1: value1, key2: value2, key3: value3);

map-get($map, key1)

相关 Cheat Sheets

1v1免费职业咨询
logo

Follow Us

linkedinfacebooktwitterinstagramweiboyoutubebilibilitiktokxigua

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png

地址

Level 10b, 144 Edward Street, Brisbane CBD(Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
四川省成都市武侯区桂溪街道天府大道中段500号D5东方希望天祥广场B座45A13号
Business Hub, 155 Waymouth St, Adelaide SA 5000

Disclaimer

footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

匠人学院网站上的所有内容,包括课程材料、徽标和匠人学院网站上提供的信息,均受澳大利亚政府知识产权法的保护。严禁未经授权使用、销售、分发、复制或修改。违规行为可能会导致法律诉讼。通过访问我们的网站,您同意尊重我们的知识产权。 JR Academy Pty Ltd 保留所有权利,包括专利、商标和版权。任何侵权行为都将受到法律追究。查看用户协议

© 2017-2025 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572