2014.03.13 @ Baidu
// Sass syntax: #sidebar width: 30% background-color: #faa // SCSS syntax: #sidebar { width: 30%; background-color: #faa; }
$ gem install sass $ sass -v Sass 3.3.0 (Maptastic Maple) $ sass input.sass output.css $ scss input.scss output.css $ sass-convert style.scss style.sass $ sass-convert style.sass style.scss
$width: 5em; #main { width: $width; } #main { width: 5em; }
$content: "First content"; $content: "Second content?" !default; $new_content: "First time reference" !default; #main { content: $content; new-content: $new_content; } #main { content: "First content"; new-content: "First time reference"; }
$name: foo; $attr: border; p.#{$name} { #{$attr}-color: blue; } p.foo { border-color: blue; }
header { .logo { color: green; } nav { color: blue; } } header .logo { color: green; } header nav { color: blue; }
// SCSS: .button { &.primary { background: orange; } &.secondary { background: blue; } } // Output: .button.primary { background: orange; } .button.secondary { background: blue; }
// SCSS: .button { &-primary { background: orange; } &-secondary { background: blue; } } // Output: .button-primary { background: orange; } .button-secondary { background: blue; }
// SCSS: .funky { font: { family: fantasy; size: 30em; weight: bold; } } // Output: .funky { font-family: fantasy; font-size: 30em; font-weight: bold; }
Lists are just a series of other values, separated by either spaces or commas. In fact, individual values count as lists, too: they’re just lists with one item.
1px 2px, 5px 6px == (1px 2px) (5px 6px) nth(10px 20px 30px, 1) => 10px join(10px 20px, 30px 40px) => 10px 20px 30px 40px append(10px 20px, 30px) => 10px 20px 30px
Unlike lists, maps must always be surrounded by parentheses and must always be comma-separated.
$map: (key1: value1, key2: value2, key3: value3); map-get($map, $key) map-merge($map1, $map2) map-remove($map, $key) map-keys($map) map-values($map) map-has-key($map, $key)
$themes: ( mist: ( header: #DCFAC0, text: #00968B, border: #85C79C ), spring: ( header: #F4FAC7, text: #C2454E, border: #FFB158 ) ); h1 { color: map-get(map-get($themes, spring), header); }
All imported SCSS and Sass files will be merged together into a single CSS output file.
Any variables or mixins defined in imported files can be used in the main file.
@import "foo.css"; @import "foo" screen; @import "http://foo.com/bar"; @import url(foo);
If we have _font.scss, _color.scss and _grid.scss
// main.scss: @import font, color, grid;
.error { border: 1px #f00; background-color: #fdd; } .seriousError { @extend .error; border-width: 3px; } .error, .seriousError { border: 1px #f00; background-color: #fdd; } .seriousError { border-width: 3px; }
%center { display: block; margin-left: auto; margin-right: auto; } .container { @extend %center; } .image-cover { @extend %center; } .container, .image-cover { display: block; margin-left: auto; margin-right: auto; }
@mixin center { display: block; margin-left: auto; margin-right: auto; } .container { @include center; } .image-cover { @include center; } .container { display: block; margin-left: auto; margin-right: auto; } .image-cover { display: block; margin-left: auto; margin-right: auto; }
.badge { @at-root { .info { ... } .header { ... } } } .info { ... } .header { ... }
@media print { .page { width: 8in; @at-root (without: media) { color: red; } } } @media print { .page { width: 8in; } } .page { color: red; }
$grid-width: 40px; $gutter-width: 10px; @function grid-width($n) { @return $n * $grid-width + ($n - 1) * $gutter-width; } #sidebar { width: grid-width(5); } #sidebar { width: 240px; }
$ scss --sourcemap sass/styles.scss public/styles.css // style.css: /*# sourceMappingURL=style.css.map */
$ sass --style NAME input.scss output.css
#main { color: #fff; background-color: #000; } #main p { width: 10em; } .huge { font-size: 10em; font-weight: bold; text-decoration: underline; }
#main { color: #fff; background-color: #000; } #main p { width: 10em; } .huge { font-size: 10em; font-weight: bold; text-decoration: underline; }
#main { color: #fff; background-color: #000; } #main p { width: 10em; } .huge { font-size: 10em; font-weight: bold; text-decoration: underline; }
#main{color:#fff;background-color:#000}#main p{width:10em}.huge{font-size:10em;font-weight:bold;text-decoration:underline}