| 1 |
<p>Sass is a meta-language on top of CSS thatÔs used to describe |
|---|
| 2 |
the style of a document cleanly and structurally, with more power than flat CSS allows. Sass both provides a simpler, more elegant |
|---|
| 3 |
syntax for CSS and implements various features that are useful for creating manageable stylesheets.</p> |
|---|
| 4 |
|
|---|
| 5 |
<h4>Features</h4> |
|---|
| 6 |
<ul> |
|---|
| 7 |
<li> Whitespace active</li> |
|---|
| 8 |
<li> Well-formatted output</li> |
|---|
| 9 |
<li> Elegant input</li> |
|---|
| 10 |
<li> Feature-rich</li> |
|---|
| 11 |
</ul> |
|---|
| 12 |
|
|---|
| 13 |
<h4>CSS Rules</h4> |
|---|
| 14 |
<p> |
|---|
| 15 |
Rules in flat CSS have two elements: the selector (e.g. "main", |
|---|
| 16 |
"div p", "li a:hover") and the attributes (e.g. |
|---|
| 17 |
"color: 00ff00;", "width: 5em;"). |
|---|
| 18 |
</p> |
|---|
| 19 |
|
|---|
| 20 |
<p> |
|---|
| 21 |
Sass has both of these, as well as one additional |
|---|
| 22 |
element: nested rules. |
|---|
| 23 |
|
|---|
| 24 |
</p> |
|---|
| 25 |
<h3>Rules and Selectors</h3> |
|---|
| 26 |
<p> |
|---|
| 27 |
However, some of the syntax is a little different. The syntax for selectors |
|---|
| 28 |
is the same, but instead of using brackets to delineate the attributes that |
|---|
| 29 |
belong to a particular rule, Sass uses two spaces |
|---|
| 30 |
of indentation. For example: |
|---|
| 31 |
</p> |
|---|
| 32 |
<pre> |
|---|
| 33 |
#main p |
|---|
| 34 |
<attribute> |
|---|
| 35 |
|
|---|
| 36 |
<attribute> |
|---|
| 37 |
|
|---|
| 38 |
... |
|---|
| 39 |
</pre> |
|---|
| 40 |
<h3>Attributes</h3> |
|---|
| 41 |
<p> |
|---|
| 42 |
The syntax for attributes is also slightly different. The colon is at the |
|---|
| 43 |
beginning of the attribute, rather than between the name and the value, so |
|---|
| 44 |
it‘s easier to tell what elements are attributes just by glancing at |
|---|
| 45 |
them. Attributes also don‘t have semicolons at the end; each |
|---|
| 46 |
attribute is on its own line, so they aren‘t necessary. For example: |
|---|
| 47 |
</p> |
|---|
| 48 |
<pre> |
|---|
| 49 |
#main p |
|---|
| 50 |
:color #00ff00 |
|---|
| 51 |
:width 97% |
|---|
| 52 |
|
|---|
| 53 |
</pre> |
|---|
| 54 |
<p> |
|---|
| 55 |
is compiled to: |
|---|
| 56 |
|
|---|
| 57 |
</p> |
|---|
| 58 |
<pre> |
|---|
| 59 |
#main p { |
|---|
| 60 |
color: #00ff00; |
|---|
| 61 |
width: 97% } |
|---|
| 62 |
</pre> |
|---|
| 63 |
<h3>Nested Rules</h3> |
|---|
| 64 |
<p> |
|---|
| 65 |
Rules can also be nested within each other. This signifies that the inner |
|---|
| 66 |
rule‘s selector is a child of the outer selector. For example: |
|---|
| 67 |
</p> |
|---|
| 68 |
<pre> |
|---|
| 69 |
#main p |
|---|
| 70 |
:color #00ff00 |
|---|
| 71 |
:width 97% |
|---|
| 72 |
|
|---|
| 73 |
.redbox |
|---|
| 74 |
:background-color #ff0000 |
|---|
| 75 |
:color #000000 |
|---|
| 76 |
|
|---|
| 77 |
</pre> |
|---|
| 78 |
<p> |
|---|
| 79 |
is compiled to: |
|---|
| 80 |
|
|---|
| 81 |
</p> |
|---|
| 82 |
<pre> |
|---|
| 83 |
#main p { |
|---|
| 84 |
color: #00ff00; |
|---|
| 85 |
width: 97%; } |
|---|
| 86 |
#main p .redbox { |
|---|
| 87 |
background-color: #ff0000; |
|---|
| 88 |
color: #000000; } |
|---|
| 89 |
</pre> |
|---|
| 90 |
<p> |
|---|
| 91 |
This makes insanely complicated CSS layouts with lots of nested selectors |
|---|
| 92 |
very simple: |
|---|
| 93 |
</p> |
|---|
| 94 |
<pre> |
|---|
| 95 |
#main |
|---|
| 96 |
:width 97% |
|---|
| 97 |
|
|---|
| 98 |
p, div |
|---|
| 99 |
:font-size 2em |
|---|
| 100 |
a |
|---|
| 101 |
:font-weight bold |
|---|
| 102 |
|
|---|
| 103 |
pre |
|---|
| 104 |
:font-size 3em |
|---|
| 105 |
</pre> |
|---|
| 106 |
<p> |
|---|
| 107 |
is compiled to: |
|---|
| 108 |
|
|---|
| 109 |
</p> |
|---|
| 110 |
<pre> |
|---|
| 111 |
#main { |
|---|
| 112 |
width: 97%; } |
|---|
| 113 |
#main p, #main div { |
|---|
| 114 |
font-size: 2em; } |
|---|
| 115 |
#main p a, #main div a { |
|---|
| 116 |
font-weight: bold; } |
|---|
| 117 |
#main pre { |
|---|
| 118 |
font-size: 3em; } |
|---|
| 119 |
|
|---|
| 120 |
</pre> |
|---|
| 121 |
<h3>Attribute Namespaces</h3> |
|---|
| 122 |
<p> |
|---|
| 123 |
CSS has quite a few attributes that are in "namespaces;" for |
|---|
| 124 |
instance, "font-family," "font-size," and |
|---|
| 125 |
|
|---|
| 126 |
"font-weight" are all in the "font" namespace. In CSS, |
|---|
| 127 |
if you want to set a bunch of attributes in the same namespace, you have to |
|---|
| 128 |
type it out each time. Sass offers a shortcut for |
|---|
| 129 |
this: just write the namespace one, then indent each of the sub-attributes |
|---|
| 130 |
within it. For example: |
|---|
| 131 |
|
|---|
| 132 |
</p> |
|---|
| 133 |
<pre> |
|---|
| 134 |
.funky |
|---|
| 135 |
:font |
|---|
| 136 |
:family fantasy |
|---|
| 137 |
:size 30em |
|---|
| 138 |
:weight bold |
|---|
| 139 |
</pre> |
|---|
| 140 |
<p> |
|---|
| 141 |
is compiled to: |
|---|
| 142 |
</p> |
|---|
| 143 |
|
|---|
| 144 |
<pre> |
|---|
| 145 |
.funky { |
|---|
| 146 |
font-family: fantasy; |
|---|
| 147 |
font-size: 30em; |
|---|
| 148 |
font-weight: bold; } |
|---|
| 149 |
</pre> |
|---|
| 150 |
<h2>Constants</h2> |
|---|
| 151 |
<p> |
|---|
| 152 |
Sass has support for setting document-wide |
|---|
| 153 |
constants. They‘re set using an exclamation mark followed by the |
|---|
| 154 |
name, an equals sign, and the value. An attribute can then be set to the |
|---|
| 155 |
value of a constant by following it with another equals sign. For example: |
|---|
| 156 |
|
|---|
| 157 |
</p> |
|---|
| 158 |
<pre> |
|---|
| 159 |
!main_color = #00ff00 |
|---|
| 160 |
|
|---|
| 161 |
#main |
|---|
| 162 |
:color = !main_color |
|---|
| 163 |
:p |
|---|
| 164 |
:background-color = !main_color |
|---|
| 165 |
:color #000000 |
|---|
| 166 |
</pre> |
|---|
| 167 |
<p> |
|---|
| 168 |
|
|---|
| 169 |
is compiled to: |
|---|
| 170 |
</p> |
|---|
| 171 |
<pre> |
|---|
| 172 |
#main { |
|---|
| 173 |
color: #00ff00; } |
|---|
| 174 |
#main p { |
|---|
| 175 |
background-color: #00ff00; |
|---|
| 176 |
color: #000000; } |
|---|
| 177 |
</pre> |
|---|
| 178 |
<h3>Arithmetic</h3> |
|---|
| 179 |
<p> |
|---|
| 180 |
You can even do basic arithmetic with constants. Sass recognizes numbers, colors, lengths (numbers with |
|---|
| 181 |
units), and strings (everything that‘s not one of the above), and |
|---|
| 182 |
various operators that work on various values. All the normal arithmetic |
|---|
| 183 |
operators (+, -, *, /, %, and parentheses for grouping) are defined as |
|---|
| 184 |
usual for numbers, colors, and lengths. The "+" operator is also |
|---|
| 185 |
defined for Strings as the concatenation operator. For example: |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
</p> |
|---|
| 189 |
<pre> |
|---|
| 190 |
!main_width = 10 |
|---|
| 191 |
!unit1 = em |
|---|
| 192 |
!unit2 = px |
|---|
| 193 |
!bg_color = #a5f39e |
|---|
| 194 |
|
|---|
| 195 |
#main |
|---|
| 196 |
:background-color = !bg_color |
|---|
| 197 |
p |
|---|
| 198 |
:background-color = !bg_color + #202020 |
|---|
| 199 |
:width = !main_width + !unit1 |
|---|
| 200 |
img.thumb |
|---|
| 201 |
:width = (!main_width + 15) + !unit2 |
|---|
| 202 |
</pre> |
|---|
| 203 |
<p> |
|---|
| 204 |
is compiled to: |
|---|
| 205 |
</p> |
|---|
| 206 |
<pre> |
|---|
| 207 |
#main { |
|---|
| 208 |
background-color: #a5f39e; } |
|---|
| 209 |
#main p { |
|---|
| 210 |
background-color: #c5ffbe; |
|---|
| 211 |
width: 10em; } |
|---|
| 212 |
#main img.thumb { |
|---|
| 213 |
width: 25em; } |
|---|
| 214 |
</pre> |
|---|
| 215 |
<h3>Colors</h3> |
|---|
| 216 |
<p> |
|---|
| 217 |
Not only can arithmetic be done between colors and other colors, but it can |
|---|
| 218 |
be done between colors and normal numbers. In this case, the operation is |
|---|
| 219 |
done piecewise one each of the Red, Green, and Blue components of the |
|---|
| 220 |
color. For example: |
|---|
| 221 |
</p> |
|---|
| 222 |
|
|---|
| 223 |
<pre> |
|---|
| 224 |
!main_color = #a5f39e |
|---|
| 225 |
|
|---|
| 226 |
#main |
|---|
| 227 |
:background-color = !main_color |
|---|
| 228 |
p |
|---|
| 229 |
:background-color = !bg_color + 32 |
|---|
| 230 |
</pre> |
|---|
| 231 |
<p> |
|---|
| 232 |
is compiled to: |
|---|
| 233 |
</p> |
|---|
| 234 |
<pre> |
|---|
| 235 |
#main { |
|---|
| 236 |
background-color: #a5f39e; } |
|---|
| 237 |
#main p { |
|---|
| 238 |
background-color: #c5ffbe; } |
|---|
| 239 |
</pre> |
|---|
| 240 |
<h3>Strings</h3> |
|---|
| 241 |
<p> |
|---|
| 242 |
Strings are the type that‘s used by default when an element in a bit |
|---|
| 243 |
of constant arithmetic isn‘t recognized as another type of constant. |
|---|
| 244 |
However, they can also be created explicitly be wrapping a section of code |
|---|
| 245 |
with quotation marks. Inside the quotation marks, a backslash can be used |
|---|
| 246 |
to escape quotation marks that you want to appear in the CSS. For example: |
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
</p> |
|---|
| 250 |
<pre> |
|---|
| 251 |
!content = "Hello, \"Hubert\" Bean." |
|---|
| 252 |
|
|---|
| 253 |
#main |
|---|
| 254 |
:content = "string(" + !content + ")" |
|---|
| 255 |
|
|---|
| 256 |
</pre> |
|---|
| 257 |
<p> |
|---|
| 258 |
is compiled to: |
|---|
| 259 |
</p> |
|---|
| 260 |
<pre> |
|---|
| 261 |
#main { |
|---|
| 262 |
content: string(Hello, "Hubert" Bean.) } |
|---|
| 263 |
</pre> |
|---|
| 264 |
<h3>Default Concatenation</h3> |
|---|
| 265 |
<p> |
|---|
| 266 |
All those plusses and quotes for concatenating strings can get pretty |
|---|
| 267 |
messy, though. Most of the time, if you want to concatenate stuff, you just |
|---|
| 268 |
want individual values with spaces in between them. Thus, in Sass, when two values are next to each other without |
|---|
| 269 |
an operator, they‘re simply joined with a space. For example: |
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
</p> |
|---|
| 273 |
<pre> |
|---|
| 274 |
!font_family = "sans-serif" |
|---|
| 275 |
!main_font_size = 1em |
|---|
| 276 |
|
|---|
| 277 |
#main |
|---|
| 278 |
:font |
|---|
| 279 |
:family = !font_amily |
|---|
| 280 |
:size = !main_font_size |
|---|
| 281 |
h6 |
|---|
| 282 |
:font = italic "small-caps" bold (!main_font_size + 0.1em) !font_family |
|---|
| 283 |
</pre> |
|---|
| 284 |
<p> |
|---|
| 285 |
is compiled to: |
|---|
| 286 |
</p> |
|---|
| 287 |
<pre> |
|---|
| 288 |
#main { |
|---|
| 289 |
font-family: sans-serif; |
|---|
| 290 |
font-size: 1em; } |
|---|
| 291 |
#main h6 { |
|---|
| 292 |
font: italic small-caps bold 1.1em sans-serif; } |
|---|
| 293 |
</pre> |
|---|