1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] [SCSS] Add SCSS examples to the Sass reference. This still needs some work.

This commit is contained in:
Nathan Weizenbaum 2010-01-02 18:41:31 -08:00
parent 8d67313ad8
commit 7a29af357e

View file

@ -233,20 +233,35 @@ but instead of using brackets to delineate the properties that belong to a parti
Sass uses indentation. Sass uses indentation.
For example: For example:
{.sass-ex}
#main p #main p
<property> <property>
<property> <property>
... ...
{.scss-ex}
#main p {
<property>
<property>
<property>
}
Like CSS, you can stretch selectors over multiple lines. Like CSS, you can stretch selectors over multiple lines.
However, unlike CSS, you can only do this if each line but the last However, unlike CSS, you can only do this if each line but the last
ends with a comma. ends with a comma.
For example: For example:
{.sass-ex}
.users #userTab, .users #userTab,
.posts #postsTab .posts #postsTab
<property> <property>
{.scss-ex}
.users #userTab,
.posts #postsTab {
<property>
}
### Properties ### Properties
There are two different ways to write CSS properties. There are two different ways to write CSS properties.
@ -256,10 +271,17 @@ However, Sass properties don't have semicolons at the end;
each property is on its own line, so they aren't necessary. each property is on its own line, so they aren't necessary.
For example: For example:
{.sass-ex}
#main p #main p
color: #00ff00 color: #00ff00
width: 97% width: 97%
{.scss-ex}
#main p {
color: #00ff00;
width: 97%;
}
is compiled to: is compiled to:
#main p { #main p {
@ -272,10 +294,17 @@ rather than between the name and the value,
so it's easier to tell what elements are properties just by glancing at them. so it's easier to tell what elements are properties just by glancing at them.
For example: For example:
{.sass-ex}
#main p #main p
:color #00ff00 :color #00ff00
:width 97% :width 97%
{.scss-ex}
#main p {
color: #00ff00;
width: 97%;
}
is compiled to: is compiled to:
#main p { #main p {
@ -292,6 +321,7 @@ Rules can also be nested within each other.
This signifies that the inner rule's selector is a child of the outer selector. This signifies that the inner rule's selector is a child of the outer selector.
For example: For example:
{.sass-ex}
#main p #main p
color: #00ff00 color: #00ff00
width: 97% width: 97%
@ -300,6 +330,17 @@ For example:
background-color: #ff0000 background-color: #ff0000
color: #000000 color: #000000
{.scss-ex}
#main p {
color: #00ff00;
width: 97%;
.redbox {
background-color: #ff0000;
color: #000000;
}
}
is compiled to: is compiled to:
#main p { #main p {
@ -311,6 +352,7 @@ is compiled to:
This makes insanely complicated CSS layouts with lots of nested selectors very simple: This makes insanely complicated CSS layouts with lots of nested selectors very simple:
{.sass-ex}
#main #main
width: 97% width: 97%
@ -322,6 +364,18 @@ This makes insanely complicated CSS layouts with lots of nested selectors very s
pre pre
font-size: 3em font-size: 3em
{.scss-ex}
#main {
width: 97%;
p, div {
font-size: 2em;
a { font-weight: bold; }
}
pre { font-size: 3em; }
}
is compiled to: is compiled to:
#main { #main {
@ -345,6 +399,7 @@ The ampersand is automatically replaced by the parent selector,
instead of having it prepended. instead of having it prepended.
This allows you to cleanly create pseudo-classes: This allows you to cleanly create pseudo-classes:
{.sass-ex}
a a
font-weight: bold font-weight: bold
text-decoration: none text-decoration: none
@ -353,6 +408,14 @@ This allows you to cleanly create pseudo-classes:
&:visited &:visited
font-weight: normal font-weight: normal
{.scss-ex}
a {
font-weight: bold;
text-decoration: none;
&:hover { text-decoration: underline; }
&:visited { font-weight: normal; }
}
Which would become: Which would become:
a { a {
@ -366,6 +429,7 @@ Which would become:
It also allows you to add selectors at the base of the hierarchy, It also allows you to add selectors at the base of the hierarchy,
which can be useuful for targeting certain styles to certain browsers: which can be useuful for targeting certain styles to certain browsers:
{.sass-ex}
#main #main
width: 90% width: 90%
#sidebar #sidebar
@ -374,6 +438,16 @@ which can be useuful for targeting certain styles to certain browsers:
.ie6 & .ie6 &
margin-left: 40% margin-left: 40%
{.scss-ex}
#main {
width: 90%;
#sidebar {
float: left;
margin-left: 20%;
.ie6 & { margin-left: 40% }
}
}
Which would become: Which would become:
#main { #main {
@ -396,12 +470,22 @@ just write the namespace one,
then indent each of the sub-properties within it. then indent each of the sub-properties within it.
For example: For example:
{.sass-ex}
.funky .funky
font: font:
family: fantasy family: fantasy
size: 30em size: 30em
weight: bold weight: bold
{.scss-ex}
.funky {
font: {
family: fantasy;
size: 30em;
weight: bold;
}
}
is compiled to: is compiled to:
.funky { .funky {
@ -434,8 +518,12 @@ a space, and any arguments to it -
just like CSS directives. just like CSS directives.
For example: For example:
{.sass-ex}
@import red.sass @import red.sass
{.scss-ex}
@import red.sass;
Some directives can also control whether or how many times Some directives can also control whether or how many times
a chunk of Sass is output. a chunk of Sass is output.
Those are documented under Control Directives. Those are documented under Control Directives.
@ -461,8 +549,12 @@ and, failing that, will assume a relevant CSS file will be available.
For example, For example,
{.sass-ex}
@import foo.sass @import foo.sass
{.scss-ex}
@import foo.sass;
would compile to would compile to
.foo { .foo {
@ -470,16 +562,24 @@ would compile to
whereas whereas
{.sass-ex}
@import foo.css @import foo.css
{.scss-ex}
@import foo.css;
would compile to would compile to
@import foo.css; @import foo.css;
Finally, Finally,
{.sass-ex}
@import foo @import foo
{.scss-ex}
@import foo;
might compile to either, might compile to either,
depending on whether or not a file called "foo.sass" existed. depending on whether or not a file called "foo.sass" existed.
@ -495,8 +595,12 @@ For example, you might have `_colors.sass`.
Then no `_colors.css` file would be created, Then no `_colors.css` file would be created,
and you can do and you can do
{.sass-ex}
@import colors.sass @import colors.sass
{.scss-ex}
@import colors.sass;
### `@debug` ### `@debug`
The `@debug` directive prints the value of a SassScript expression The `@debug` directive prints the value of a SassScript expression
@ -505,8 +609,12 @@ It's useful for debugging Sass files
that have complicated SassScript going on. that have complicated SassScript going on.
For example: For example:
{.sass-ex}
@debug 10em + 12em @debug 10em + 12em
{.scss-ex}
@debug 10em + 12em;
outputs: outputs:
Line 1 DEBUG: 22em Line 1 DEBUG: 22em
@ -516,10 +624,17 @@ outputs:
Sass behaves as you'd expect for normal CSS @-directives. Sass behaves as you'd expect for normal CSS @-directives.
For example: For example:
{.sass-ex}
@font-face @font-face
font-family: "Bitstream Vera Sans" font-family: "Bitstream Vera Sans"
src: url(http://foo.bar/bvs") src: url(http://foo.bar/bvs")
{.scss-ex}
@font-face {
font-family: "Bitstream Vera Sans";
src: url(http://foo.bar/bvs");
}
compiles to: compiles to:
@font-face { @font-face {
@ -528,6 +643,7 @@ compiles to:
and and
{.sass-ex}
@media print @media print
#sidebar #sidebar
display: none display: none
@ -535,6 +651,13 @@ and
#main #main
background-color: white background-color: white
{.scss-ex}
@media print {
#sidebar { display: none; }
#main { background-color: white; }
}
compiles to: compiles to:
@media print { @media print {
@ -554,12 +677,19 @@ The `@if` statement takes a SassScript expression
and prints the code nested beneath it if the expression returns and prints the code nested beneath it if the expression returns
anything other than `false`: anything other than `false`:
{.sass-ex}
p p
@if 1 + 1 == 2 @if 1 + 1 == 2
border: 1px solid border: 1px solid
@if 5 < 3 @if 5 < 3
border: 2px dotted border: 2px dotted
{.scss-ex}
p {
@if 1 + 1 == 2 { border: 1px solid; }
@if 5 < 3 { border: 2px dotted; }
}
is compiled to: is compiled to:
p { p {
@ -572,6 +702,7 @@ the `@else if` statements are tried in order
until one succeeds or the `@else` is reached. until one succeeds or the `@else` is reached.
For example: For example:
{.sass-ex}
!type = "monster" !type = "monster"
p p
@if !type == "ocean" @if !type == "ocean"
@ -583,6 +714,20 @@ For example:
@else @else
color: black color: black
{.scss-ex}
!type = "monster";
p {
@if !type == "ocean" {
color: blue;
} @else if !type == "matador" {
color: red;
} @else if !type == "monster" {
color: green;
} @else {
color: black;
}
}
is compiled to: is compiled to:
p { p {
@ -602,10 +747,16 @@ from `<start>` to `<end>`,
including `<end>` if `through` is used. including `<end>` if `through` is used.
For example: For example:
{.sass-ex}
@for !i from 1 through 3 @for !i from 1 through 3
.item-#{!i} .item-#{!i}
width = 2em * !i width = 2em * !i
{.scss-ex}
@for !i from 1 through 3 {
.item-#{!i} { width = 2em * !i; }
}
is compiled to: is compiled to:
.item-1 { .item-1 {
@ -623,12 +774,20 @@ be used to achieve more complex looping than the `@for`
statement is capable of. statement is capable of.
For example: For example:
{.sass-ex}
!i = 6 !i = 6
@while !i > 0 @while !i > 0
.item-#{!i} .item-#{!i}
width = 2em * !i width = 2em * !i
!i = !i - 2 !i = !i - 2
{.scss-ex}
!i = 6;
@while !i > 0 {
.item-#{!i} { width = 2em * !i; }
!i = !i - 2;
}
is compiled to: is compiled to:
.item-6 { .item-6 {
@ -651,8 +810,12 @@ SassScript can be used as the value for a property
by using `=` instead of `:`. by using `=` instead of `:`.
For example: For example:
{.sass-ex}
color = #123 + #234 color = #123 + #234
{.scss-ex}
color = #123 + #234;
is compiled to: is compiled to:
color: #357; color: #357;
@ -690,14 +853,24 @@ is to set and reference variables.
Variables begin with exclamation marks, Variables begin with exclamation marks,
and are set like so: and are set like so:
{.sass-ex}
!width = 5em !width = 5em
{.scss-ex}
!width = 5em;
You can then refer to them by putting an equals sign You can then refer to them by putting an equals sign
after your properties: after your properties:
{.sass-ex}
#main #main
width = !width width = !width
{.scss-ex}
#main {
width = !width;
}
Variables that are first defined in a scoped context are only Variables that are first defined in a scoped context are only
available in that context. available in that context.
@ -712,6 +885,7 @@ SassScript supports four data types:
Any text that doesn't fit into one of those types Any text that doesn't fit into one of those types
in a SassScript context will cause an error: in a SassScript context will cause an error:
{.sass-ex}
p p
!width = 5em !width = 5em
// This will cause an error // This will cause an error
@ -720,6 +894,16 @@ in a SassScript context will cause an error:
border = "#{!width} solid blue" border = "#{!width} solid blue"
border = !width "solid" "blue" border = !width "solid" "blue"
{.scss-ex}
p {
!width = 5em;
/* This will cause an error */
/* border = !width solid blue; */
/* Use one of the following forms instead: */
border = "#{!width} solid blue";
border = !width "solid" "blue";
}
is compiled to: is compiled to:
p { p {
@ -735,9 +919,15 @@ SassScript supports the standard arithmetic operations on numbers
(`+`, `-`, `*`, `/`, `%`), (`+`, `-`, `*`, `/`, `%`),
and will automatically convert between units if it can: and will automatically convert between units if it can:
{.sass-ex}
p p
width = 1in + 8pt width = 1in + 8pt
{.scss-ex}
p {
width = 1in + 8pt;
}
is compiled to: is compiled to:
p { p {
@ -758,9 +948,15 @@ This means that the operation is performed
on the red, green, and blue components in turn. on the red, green, and blue components in turn.
For example: For example:
{.sass-ex}
p p
color = #010203 + #040506 color = #010203 + #040506
{.scss-ex}
p {
color = #010203 + #040506;
}
computes `01 + 04 = 05`, `02 + 05 = 07`, and `03 + 06 = 09`, computes `01 + 04 = 05`, `02 + 05 = 07`, and `03 + 06 = 09`,
and is compiled to: and is compiled to:
@ -771,9 +967,15 @@ Arithmetic operations even work between numbers and colors,
also piecewise. also piecewise.
For example: For example:
{.sass-ex}
p p
color = #010203 * 2 color = #010203 * 2
{.scss-ex}
p {
color = #010203 * 2;
}
computes `01 * 2 = 02`, `02 * 2 = 04`, and `03 * 2 = 06`, computes `01 * 2 = 02`, `02 * 2 = 04`, and `03 * 2 = 06`,
and is compiled to: and is compiled to:
@ -788,9 +990,15 @@ to be done with them.
The arithmetic doesn't affect the alpha value. The arithmetic doesn't affect the alpha value.
For example: For example:
{.sass-ex}
p p
color = rgba(255, 0, 0, 0.75) + rgba(0, 255, 0, 0.75) color = rgba(255, 0, 0, 0.75) + rgba(0, 255, 0, 0.75)
{.scss-ex}
p {
color = rgba(255, 0, 0, 0.75) + rgba(0, 255, 0, 0.75);
}
is compiled to: is compiled to:
p { p {
@ -801,11 +1009,19 @@ The alpha channel of a color can be adjusted using the
{Sass::Script::Functions#transparentize transparentize} functions. {Sass::Script::Functions#transparentize transparentize} functions.
For example: For example:
{.sass-ex}
!translucent-red = rgba(255, 0, 0, 0.5) !translucent-red = rgba(255, 0, 0, 0.5)
p p
color = opacify(!translucent-red, 80%) color = opacify(!translucent-red, 80%)
background-color = transparentize(!translucent-red, 50%) background-color = transparentize(!translucent-red, 50%)
{.scss-ex}
!translucent-red = rgba(255, 0, 0, 0.5);
p {
color = opacify(!translucent-red, 80%);
background-color = transparentize(!translucent-red, 50%);
}
is compiled to: is compiled to:
p { p {
@ -816,9 +1032,15 @@ is compiled to:
The `+` operation can be used to concatenate strings: The `+` operation can be used to concatenate strings:
{.sass-ex}
p p
cursor = "e" + "-resize" cursor = "e" + "-resize"
{.scss-ex}
p {
cursor = "e" + "-resize";
}
is compiled to: is compiled to:
p { p {
@ -827,9 +1049,15 @@ is compiled to:
By default, if two values are placed next to one another, By default, if two values are placed next to one another,
they are concatenated with a space: they are concatenated with a space:
{.sass-ex}
p p
margin = 3px + 4px "auto" margin = 3px + 4px "auto"
{.scss-ex}
p {
margin = 3px + 4px "auto";
}
is compiled to: is compiled to:
p { p {
@ -838,9 +1066,15 @@ is compiled to:
Within a string of text, #{} style interpolation can be used to Within a string of text, #{} style interpolation can be used to
place dynamic values within the string: place dynamic values within the string:
{.sass-ex}
p p
border = "#{5px + 10px} solid #ccc" border = "#{5px + 10px} solid #ccc"
{.scss-ex}
p {
border = "#{5px + 10px} solid #ccc";
}
Finally, SassScript supports `and`, `or`, and `not` operators Finally, SassScript supports `and`, `or`, and `not` operators
for boolean values. for boolean values.
@ -848,9 +1082,15 @@ for boolean values.
Parentheses can be used to affect the order of operations: Parentheses can be used to affect the order of operations:
{.sass-ex}
p p
width = 1em + (2em * 3) width = 1em + (2em * 3)
{.scss-ex}
p {
width = 1em + (2em * 3);
}
is compiled to: is compiled to:
p { p {
@ -861,9 +1101,15 @@ is compiled to:
SassScript defines some useful functions SassScript defines some useful functions
that are called using the normal CSS function syntax: that are called using the normal CSS function syntax:
{.sass-ex}
p p
color = hsl(0, 100%, 50%) color = hsl(0, 100%, 50%)
{.scss-ex}
p {
color = hsl(0, 100%, 50%);
}
is compiled to: is compiled to:
#main { #main {
@ -877,11 +1123,17 @@ as well as instructions on defining your own in Ruby.
You can also use SassScript variables in selectors You can also use SassScript variables in selectors
and property names using #{} interpolation syntax: and property names using #{} interpolation syntax:
{.sass-ex}
!name = foo !name = foo
!attr = border !attr = border
p.#{!name} p.#{!name}
#{!attr}-color: blue #{!attr}-color: blue
{.scss-ex}
!name = foo;
!attr = border;
p.#{!name} { #{!attr}-color: blue }
is compiled to: is compiled to:
p.foo { p.foo {
@ -896,6 +1148,7 @@ but if it doesn't have a value yet, it will be given one.
For example: For example:
{.sass-ex}
!content = "First content" !content = "First content"
!content ||= "Second content?" !content ||= "Second content?"
!new_content ||= "First time reference" !new_content ||= "First time reference"
@ -904,6 +1157,16 @@ For example:
content = !content content = !content
new-content = !new_content new-content = !new_content
{.scss-ex}
!content = "First content";
!content ||= "Second content?";
!new_content ||= "First time reference";
#main {
content = !content;
new-content = !new_content;
}
is compiled to: is compiled to:
#main { #main {
@ -923,6 +1186,7 @@ classes in your markup.
To define a mixin you use a slightly modified form of selector syntax. To define a mixin you use a slightly modified form of selector syntax.
For example the `large-text` mixin is defined as follows: For example the `large-text` mixin is defined as follows:
{.sass-ex}
=large-text =large-text
font: font:
family: Arial family: Arial
@ -930,12 +1194,23 @@ For example the `large-text` mixin is defined as follows:
weight: bold weight: bold
color: #ff0000 color: #ff0000
{.scss-ex}
@mixin large-text {
font: {
family: Arial;
size: 20px;
weight: bold;
}
color: #ff0000;
}
The initial `=` marks this as a mixin rather than a standard selector. The initial `=` marks this as a mixin rather than a standard selector.
The CSS rules that follow won't be included until the mixin is referenced later on. The CSS rules that follow won't be included until the mixin is referenced later on.
Anything you can put into a standard selector, Anything you can put into a standard selector,
you can put into a mixin definition. you can put into a mixin definition.
For example: For example:
{.sass-ex}
=clearfix =clearfix
display: inline-block display: inline-block
&:after &:after
@ -947,6 +1222,19 @@ For example:
* html & * html &
height: 1px height: 1px
{.scss-ex}
@mixin clearfix {
display: inline-block;
&:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
* html & { height: 1px }
}
### Mixing It In: `+` ### Mixing It In: `+`
Inlining a defined mixin is simple, Inlining a defined mixin is simple,
@ -954,11 +1242,18 @@ just prepend a `+` symbol to the name of a mixin defined earlier in the document
So to inline the `large-text` defined earlier, So to inline the `large-text` defined earlier,
we include the statment `+large-text` in our selector definition thus: we include the statment `+large-text` in our selector definition thus:
{.sass-ex}
.page-title .page-title
+large-text +large-text
padding: 4px padding: 4px
margin: margin-top: 10px
top: 10px
{.scss-ex}
.page-title {
@include large-text;
padding: 4px;
margin-top: 10px;
}
This will produce the following CSS output: This will produce the following CSS output:
@ -976,16 +1271,28 @@ the number that can be included in a particular selector.
Mixin definitions can also include references to other mixins. Mixin definitions can also include references to other mixins.
For example: For example:
{.sass-ex}
=compound =compound
+highlighted-background +highlighted-background
+header-text +header-text
=highlighted-background =highlighted-background
background: background-color: #fc0
color: #fc0
=header-text =header-text
font: font-size: 20px
size: 20px
{.scss-ex}
@mixin compound {
@include highlighted-background;
@include header-text;
}
@mixin highlighted-background {
background-color: #fc0;
}
@mixin header-text {
font-size: 20px;
}
Mixins that only define descendent selectors, can be safely mixed Mixins that only define descendent selectors, can be safely mixed
into the top most level of a document. into the top most level of a document.
@ -994,6 +1301,7 @@ into the top most level of a document.
Mixins can take arguments which can be used with SassScript: Mixins can take arguments which can be used with SassScript:
{.sass-ex}
=sexy-border(!color) =sexy-border(!color)
border: border:
color = !color color = !color
@ -1002,6 +1310,17 @@ Mixins can take arguments which can be used with SassScript:
p p
+sexy-border("blue") +sexy-border("blue")
{.scss-ex}
@mixin sexy-border(!color) {
border: {
color = !color;
width: 1in;
style: dashed;
}
}
p { @include sexy-border("blue"); }
is compiled to: is compiled to:
p { p {
@ -1011,6 +1330,7 @@ is compiled to:
Mixins can also specify default values for their arguments: Mixins can also specify default values for their arguments:
{.sass-ex}
=sexy-border(!color, !width = 1in) =sexy-border(!color, !width = 1in)
border: border:
color = !color color = !color
@ -1019,6 +1339,15 @@ Mixins can also specify default values for their arguments:
p p
+sexy-border("blue") +sexy-border("blue")
{.scss-ex}
@mixin sexy-border(!color, !width = 1in) {
border: {
color = !color;
width = !width;
style: dashed;
}
p { @include sexy-border("blue"); }
is compiled to: is compiled to:
p { p {
@ -1039,11 +1368,19 @@ These comments output to the document as CSS comments,
and thus use the same opening sequence: `/*`. and thus use the same opening sequence: `/*`.
For example: For example:
{.sass-ex}
/* A very awesome rule. /* A very awesome rule.
#awesome.rule #awesome.rule
/* An equally awesome property. /* An equally awesome property.
awesomeness: very awesomeness: very
{.scss-ex}
/* A very awesome rule. */
#awesome.rule {
/* An equally awesome property. */
awesomeness: very;
}
becomes becomes
/* A very awesome rule. */ /* A very awesome rule. */
@ -1053,6 +1390,7 @@ becomes
You can also nest content beneath loud comments. For example: You can also nest content beneath loud comments. For example:
{.sass-ex}
#pbj #pbj
/* This rule describes /* This rule describes
the styling of the element the styling of the element
@ -1061,6 +1399,16 @@ You can also nest content beneath loud comments. For example:
background-image: url(/images/pbj.png) background-image: url(/images/pbj.png)
color: red color: red
{.scss-ex}
#pbj {
/* This rule describes
the styling of the element
that represents
a peanut butter and jelly sandwich. */
background-image: url(/images/pbj.png);
color: red;
}
becomes becomes
#pbj { #pbj {
@ -1080,11 +1428,19 @@ Simply use the familiar C-style notation for a one-line comment, `//`,
at the normal indentation level and all text following it won't be output. at the normal indentation level and all text following it won't be output.
For example: For example:
{.sass-ex}
// A very awesome rule. // A very awesome rule.
#awesome.rule #awesome.rule
// An equally awesome property. // An equally awesome property.
awesomeness: very awesomeness: very
{.scss-ex}
// A very awesome rule.
#awesome.rule {
// An equally awesome property.
awesomeness: very;
}
becomes becomes
#awesome.rule { #awesome.rule {
@ -1093,6 +1449,7 @@ becomes
You can also nest text beneath a comment to comment out a whole block. You can also nest text beneath a comment to comment out a whole block.
For example: For example:
{.sass-ex}
// A very awesome rule // A very awesome rule
#awesome.rule #awesome.rule
// Don't use these properties // Don't use these properties
@ -1100,6 +1457,15 @@ For example:
font-size: 10em font-size: 10em
color: red color: red
{.scss-ex}
// A very awesome rule
#awesome.rule {
// Don't use these properties
// color: green
// font-size: 10em
color: red;
}
becomes becomes
#awesome.rule { #awesome.rule {