forked from saturnflyer/radiant-sheets-extension
-
Notifications
You must be signed in to change notification settings - Fork 7
/
sass.html
82 lines (64 loc) · 11.8 KB
/
sass.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<h1 id='features'>Features</h1>
<p>Although it’s nice that every CSS file is a valid SCSS file, that’s not why you’re learning about SCSS. You’re learning because it offers <em>more</em> than CSS. SCSS provides all sorts of useful features on top of CSS. This tutorial will walk you through some of the most fundamental ones, along with examples that you can add to your <code>style.scss</code> and play with.</p>
<h2 id='nesting'>Nesting</h2>
<p>Often when writing CSS, you’ll have several selectors that all begin with the same thing. For example, you might have “#navbar ul”, “#navbar li”, and “#navbar li a”. It’s a pain to repeat the beginning over and over again, especially when it gets long.</p>
<p>Sass allows you to avoid this by nesting the child selectors within the parent selector.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

#navbar {
 width: 80%;
 height: 23px;

 ul { list-style-type: none; }
 li {
 float: left;
 a { font-weight: bold; }
 }
}
</pre>
<pre class='css'>/* style.css */

#navbar {
 width: 80%;
 height: 23px; }
 #navbar ul {
 list-style-type: none; }
 #navbar li {
 float: left; }
 #navbar li a {
 font-weight: bold; }
</pre>
</div>
<p>Notice how the output is formatted to reflect the original nesting of the selectors. This is the default CSS style, but there are <a href='/docs/yardoc/file.SASS_REFERENCE.html#output_style'>other styles</a> for all sorts of CSS formatting preferences. There’s even one for compressing the CSS as much as possible!</p>
<p>You can also nest properties, so you don’t have to repeat stuff like “<code>border-left-</code>” all the time.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

.fakeshadow {
 border: {
 style: solid;
 left: {
 width: 4px;
 color: #888;
 }
 right: {
 width: 2px;
 color: #ccc;
 }
 }
</pre>
<pre class='css'>/* style.css */

.fakeshadow {
 border-style: solid;
 border-left-width: 4px;
 border-left-color: #888;
 border-right-width: 2px;
 border-right-color: #ccc; }
</pre>
</div>
<h3 id='parent_references'>Parent References</h3>
<p>What about pseudoclasses, like <code>:hover</code>? There isn’t a space between them and their parent selector, but it’s still possible to nest them. You just need to use the Sass special character <code>&</code>. In a selector, <code>&</code> will be replaced verbatim with the parent selector.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

a {
 color: #ce4dd6;
 &:hover { color: #ffb3ff; }
 &:visited { color: #c458cb; }
}
</pre>
<pre class='css'>/* style.css */

a {
 color: #ce4dd6; }
 a:hover {
 color: #ffb3ff; }
 a:visited {
 color: #c458cb; }
</pre>
</div>
<h2 id='variables'>Variables</h2>
<p>Sass allows you to declare variables that can be used throughout your stylesheet. Variables begin with <code>$</code> and are declared just like properties. They can have any value that’s allowed for a CSS property, such as colors, numbers (with units), or text.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

$main-color: #ce4dd6;
$style: solid;

#navbar {
 border-bottom: {
 color: $main-color;
 style: $style;
 }
}

a {
 color: $main-color;
 &:hover { border-bottom: $style 1px; }
}
</pre>
<pre class='css'>/* style.css */

#navbar {
 border-bottom-color: #ce4dd6;
 border-bottom-style: solid; }

a {
 color: #ce4dd6; }
 a:hover {
 border-bottom: solid 1px; }
</pre>
</div>
<p>Variables allow you to re-use colors, sizes, and other values without repeating yourself. This means that changes that should be small, such as tweaking the coloring or the sizing, can be done in one place, not all over the stylesheet.</p>
<h3 id='operations_and_functions'>Operations and Functions</h3>
<p>In addition to just using variable values as they’re defined, you can also modify and combine them using math and useful predefined functions. This allows you to compute element sizing and even coloration dynamically.</p>
<p>The standard math operations (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, and <code>%</code>) are supported for numbers, even those with units. For colors, there are all sorts of useful functions for changing the <a href='/docs/yardoc/Sass/Script/Functions.html#lighten-instance_method'>lightness</a>, <a href='/docs/yardoc/Sass/Script/Functions.html#adjust_hue-instance_method'>hue</a>, <a href='/docs/yardoc/Sass/Script/Functions.html#saturate-instance_method'>saturation</a>, and <a href='/docs/yardoc/Sass/Script/Functions.html'>more</a>.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

#navbar {
 $navbar-width: 800px;
 $items: 5;
 $navbar-color: #ce4dd6;

 width: $navbar-width;
 border-bottom: 2px solid $navbar-color;

 li {
 float: left;
 width: $navbar-width/$items - 10px;

 background-color:
 lighten($navbar-color, 20%);
 &:hover {
 background-color:
 lighten($navbar-color, 10%);
 }
 }
}
</pre>
<pre class='css'>/* style.css */

#navbar {
 width: 800px;
 border-bottom: 2px solid #ce4dd6; }
 #navbar li {
 float: left;
 width: 150px;
 background-color: #e5a0e9; }
 #navbar li:hover {
 background-color: #d976e0; }
</pre>
</div>
<h3 id='interpolation'>Interpolation</h3>
<p>Variables can be used for more than just property values. You can use <code>#{}</code> to insert them into property names or selectors.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

$side: top;
$radius: 10px;

.rounded- {
 border-#{$side}-radius: $radius;
 -moz-border-radius-#{$side}: $radius;
 -webkit-border-#{$side}-radius: $radius;
}
</pre>
<pre class='css'>/* style.css */

.rounded-top {
 border-top-radius: 10px;
 -moz-border-radius-top: 10px;
 -webkit-border-top-radius: 10px; }
</pre>
</div>
<h2 id='mixins'>Mixins</h2>
<p>Mixins are one of the most powerful Sass features. They allow re-use of styles – properties or even selectors – without having to copy and paste them or move them into a non-semantic class.</p>
<p>Mixins are defined using the “<code>@mixin</code>” directive, which takes a block of styles that can then be included in another selector using the “<code>@include</code>” directive.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

@mixin rounded-top {
 $side: top;
 $radius: 10px;

 border-#{$side}-radius: $radius;
 -moz-border-radius-#{$side}: $radius;
 -webkit-border-#{$side}-radius: $radius;
}

#navbar li { @include rounded-top; }
#footer { @include rounded-top; }
</pre>
<pre class='css'>/* style.css */

#navbar li {
 border-top-radius: 10px;
 -moz-border-radius-top: 10px;
 -webkit-border-top-radius: 10px; }

#footer {
 border-top-radius: 10px;
 -moz-border-radius-top: 10px;
 -webkit-border-top-radius: 10px; }
</pre>
</div>
<h3 id='arguments'>Arguments</h3>
<p>The real power of mixins comes when you pass them arguments. Arguments are declared as a parenthesized, comma-separated list of variables. Each of those variables is assigned a value each time the mixin is used.</p>
<p>Mixin arguments can also be given default values just like you’d declare them normally. Then the user of the mixin can choose not to pass that argument and it will be assigned the default value.</p>
<div class='compare'>
<pre class='sass'>/* style.scss */

@mixin rounded($side, $radius: 10px) {
 border-#{$side}-radius: $radius;
 -moz-border-radius-#{$side}: $radius;
 -webkit-border-#{$side}-radius: $radius;
}

#navbar li { @include rounded(top); }
#footer { @include rounded(top, 5px); }
#sidebar { @include rounded(left, 8px); }
</pre>
<pre class='css'>/* style.css */

#navbar li {
 border-top-radius: 10px;
 -moz-border-radius-top: 10px;
 -webkit-border-top-radius: 10px; }

#footer {
 border-top-radius: 5px;
 -moz-border-radius-top: 5px;
 -webkit-border-top-radius: 5px; }

#sidebar {
 border-left-radius: 8px;
 -moz-border-radius-left: 8px;
 -webkit-border-left-radius: 8px; }
</pre>
</div>
<h2 id='id1'><code>@import</code></h2>
<p>Stylesheets can get pretty big. CSS has an <code>@import</code> directive that allows you to break your styles up into multiple stylesheets, but each stylesheet takes a separate (slow) HTTP request. That’s why Sass’s <code>@import</code> directive pulls in the stylesheets directly. Not only that, but any variables or mixins defined in <code>@import</code>ed files are available to the files that import them.</p>
<p>Sass has a naming convention for files that are meant to be imported (called “partials”): they begin with an underscore. Let’s create a partial called <strong><code>_rounded.scss</code></strong> to hold our <code>rounded</code> mixin.</p>
<p>In order to support both <code>.scss</code> and <code>.sass</code> files, Sass allows files to be imported without specifying a file extension. That means we can just import <code>"rounded"</code>, rather than <code>"rounded.scss"</code>.</p>
<div class='compare'>
<pre class='sass'>/* _rounded.scss */

@mixin rounded($side, $radius: 10px) {
 border-#{$side}-radius: $radius;
 -moz-border-radius-#{$side}: $radius;
 -webkit-border-#{$side}-radius: $radius;
}
</pre>
<pre class='css'>/* style.css */

#navbar li {
 border-top-radius: 10px;
 -moz-border-radius-top: 10px;
 -webkit-border-top-radius: 10px; }

#footer {
 border-top-radius: 5px;
 -moz-border-radius-top: 5px;
 -webkit-border-top-radius: 5px; }

#sidebar {
 border-left-radius: 8px;
 -moz-border-radius-left: 8px;
 -webkit-border-left-radius: 8px; }
</pre>
<pre class='sass'>/* style.scss */

@import "rounded";

#navbar li { @include rounded(top); }
#footer { @include rounded(top, 5px); }
#sidebar { @include rounded(left, 8px); }
</pre>
</div>