From 62c6f95e9d161967cfffa43a3f9b3f8d73e2ef5f Mon Sep 17 00:00:00 2001 From: Sean Corrales Date: Mon, 5 Oct 2015 10:51:27 -0500 Subject: Initial work on learn Sass file. --- sass.html.markdown | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 234 insertions(+) create mode 100644 sass.html.markdown (limited to 'sass.html.markdown') diff --git a/sass.html.markdown b/sass.html.markdown new file mode 100644 index 00000000..35af1e67 --- /dev/null +++ b/sass.html.markdown @@ -0,0 +1,234 @@ +--- +language: sass +contributors: + - ["Sean Corrales", "https://github.com/droidenator"] +filename: learnsass.scss +--- + +Sass is a CSS pre-processor. It adds several features that plain +CSS lacks such as variables, mixins, basic math, and inheritance. + +Initially, Sass was written using spacing and indention instead +of brackets and semi-colons; these files use the extension '.sass'. +Sass was later revised to use brackets and semi-colons and become +a superset of CSS3. This new version uses the extension ".scss". +Using ".scss" means that any valid CSS3 file can be converted to +Sass by simply changing the file extension to '.scss'. + +If you're already familiar with CSS3, you'll be able to pick up Sass +relatively quickly. It does not provide any new styling options but rather +the tools to write your CSS more efficiently and make maintenance much +easier. + +Sass files must be compiled into CSS. You can use any number of commandline +tools to compile Sass into CSS. Many IDEs also offer Sass compilation, as well. + + +```sass +/* Like CSS, Sass uses slash-asterisk to denote comments */ + +/* #################### + ## VARIABLES + #################### */ + +/* Sass allows you to define variables that can be used throughout + your stylesheets. Variables are defined by placing a '$' in front + of a string. Many users like to keep their variables in a single file */ +$primary-color: #0000ff; +$headline-size: 24px; + +/* Variables can be used in any CSS declaration. This allows you to change + a single value in one place. */ +a { + color: $primary-color; +} + +h1 { + color: $primary-color; + font-size: $headline-size; +} + +/* After compiling the Sass files into CSS, you'll have the following code + in your generated CSS file */ + +a { + color: #0000ff; +} + +h1 { + color: #0000ff; + font-size: 24px; +} + +/* #################### + ## NESTING + #################### */ + +/* Nesting allows you to easily group together statements and nest them + in a way that indicates their hierarchy */ +article { + font-size: 14px; + + a { + text-decoration: underline; + } + + ul { + list-style-type: disc; + + li { + text-indent: 3em; + } + } + + pre, img { + display: inline-block; + float: left; + } +} + +/* The above will compile into the following CSS */ +article { + font-size: 14px; +} + +article a { + text-decoration: underline; +} + +article ul { + list-style-type: disc; +} + +article ul li { + text-indent: 3em; +} + +article pre, +article img { + display: inline-block; + float: left; +} + +/* It is recommended to not nest too deeply as this can cause issues with + specificity and make your CSS harder to work with and maintain. Best practices + recommend going no more than 3 levels deep when nesting. */ + +/* #################### + ## MIXINS + #################### */ +/* Mixins allow you to define reusable chunks of CSS. They can take one or more + arguments to allow you to make reusable pieces of styling. */ +@mixin form-button($color, $size, $border-radius) { + color: $color; + font-size: $size; + border-radius: $border-radius; +} + +/* Mixins are invoked within a CSS declaration. */ +.user-form .submit { + @include form-button(#0000ff, 16px, 4px); + margin: 10px; +} + +/* The above mixin will compile into the following css */ +.user-form .submit { + color: #0000ff; + font-size: 16px; + border-radius: 4px; + margin: 10px; +} + +/* #################### + ## EXTEND/INHERITANCE + #################### */ + +/* #################### + ## MATH OPERATIONS + #################### */ + +``` + +## Usage + +Save any CSS you want in a file with extension `.css`. + +```xml + + + + + + + +
+
+ +``` + +## Precedence + +As you noticed an element may be targetted by more than one selector. +and may have a property set on it in more than one. +In these cases, one of the rules takes precedence over others. + +Given the following CSS: + +```css +/*A*/ +p.class1[attr='value'] + +/*B*/ +p.class1 {} + +/*C*/ +p.class2 {} + +/*D*/ +p {} + +/*E*/ +p { property: value !important; } + +``` + +and the following markup: + +```xml +

+

+``` + +The precedence of style is as followed: +Remember, the precedence is for each **property**, not for the entire block. + +* `E` has the highest precedence because of the keyword `!important`. + It is recommended to avoid this unless it is strictly necessary to use. +* `F` is next, because it is inline style. +* `A` is next, because it is more "specific" than anything else. + more specific = more specifiers. here 3 specifiers: 1 tagname `p` + + class name `class1` + 1 attribute `attr='value'` +* `C` is next. although it has the same specificness as `B` + but it appears last. +* Then is `B` +* and lastly is `D`. + +## Compatibility + +Most of the features in CSS2 (and gradually in CSS3) are compatible across +all browsers and devices. But it's always vital to have in mind the compatibility +of what you use in CSS with your target browsers. + +[QuirksMode CSS](http://www.quirksmode.org/css/) is one of the best sources for this. + +To run a quick compatibility check, [CanIUse](http://caniuse.com) is a great resource. + +## Further Reading + +* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/) +* [QuirksMode CSS](http://www.quirksmode.org/css/) +* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context) -- cgit v1.2.3 From 1bc457737577a01af47a8be5879904dd23bcc410 Mon Sep 17 00:00:00 2001 From: Sean Corrales Date: Mon, 5 Oct 2015 13:52:53 -0500 Subject: Finishing up documentation for additional Sass functionality. Removing CSS specific instructions from usage, compatibility, and further reading sections. --- sass.html.markdown | 300 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 232 insertions(+), 68 deletions(-) (limited to 'sass.html.markdown') diff --git a/sass.html.markdown b/sass.html.markdown index 35af1e67..d1e0721f 100644 --- a/sass.html.markdown +++ b/sass.html.markdown @@ -25,7 +25,11 @@ tools to compile Sass into CSS. Many IDEs also offer Sass compilation, as well. ```sass -/* Like CSS, Sass uses slash-asterisk to denote comments */ +/* Like CSS, Sass uses slash-asterisk to denote comments. Slash-asterisk comments + can span multiple lines. These comments will appear in your compiled CSS */ + +// Sass also supports single line comments that use double slashes. These comments will +// not be rendered in your compiled CSS /* #################### ## VARIABLES @@ -113,12 +117,52 @@ article img { /* It is recommended to not nest too deeply as this can cause issues with specificity and make your CSS harder to work with and maintain. Best practices recommend going no more than 3 levels deep when nesting. */ + +/* ############################### + ## REFERENCE PARENT SELECTORS + ############################### */ + +/* Reference parent selectors are used when you're nesting statements and want to + reference the parent selector from within the nested statements. You can reference + a parent using & */ + +a { + text-decoration: none; + color: #ff0000; + + &:hover { + text-decoration: underline; + } + + body.noLinks & { + display: none; + } +} + +/* The above Sass will compile into the CSS below */ + +a { + text-decoration: none; + color: #ff0000; +} + +a:hover { + text-decoration: underline; +} + +body.noLinks a { + display: none; +} + /* #################### ## MIXINS #################### */ + /* Mixins allow you to define reusable chunks of CSS. They can take one or more - arguments to allow you to make reusable pieces of styling. */ + arguments to allow you to make reusable pieces of styling. Mixins can also + be very helpful when dealing with vendor prefixes. */ + @mixin form-button($color, $size, $border-radius) { color: $color; font-size: $size; @@ -126,109 +170,229 @@ article img { } /* Mixins are invoked within a CSS declaration. */ + .user-form .submit { @include form-button(#0000ff, 16px, 4px); - margin: 10px; } /* The above mixin will compile into the following css */ + .user-form .submit { color: #0000ff; font-size: 16px; border-radius: 4px; - margin: 10px; } /* #################### - ## EXTEND/INHERITANCE + ## FUNCTIONS #################### */ - + +/* Sass provides functions that can be used to accomplish a variety of tasks. Consider the following */ + +body { + width: round(10.25px); +} + +.footer { + background-color: fade_out(#000000, 0.25) +} + +/* The above Sass will compile into the following CSS */ + +body { + width: 10px; +} + +.footer { + background-color: rgba(0, 0, 0, 0.75); +} + +/* You may also define your own functions. Functions are very similar to mixins. When trying to choose between + a function or a mixin, remember that functions are best for returning values while mixins are best for + generating CSS while functions are better for logic that might be used throughout your Sass code. The + examples in the Math Operators' section are ideal candidates for becoming a reusable function. */ + +/* This function will take a target size and the parent size and calculate and return the percentage */ + +@function calculate-percentage($target-size, $parent-size) { + @return $target-size / $parent-size * 100%; +} + +/* Functions can be invoked by using their name and passing in the required arguments */ + +$main-content: calculate-percentage(600px, 960px); + +.main-content { + width: $main-content; +} + +.sidebar { + width: calculate-percentage(300px, 960px); +} + +/* The above Sass will compile into the following CSS */ + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + /* #################### - ## MATH OPERATIONS - #################### */ + ## EXTEND/INHERITANCE + #################### */ -``` +/* Sass allows you to extend an existing CSS statement. This makes it + very easy to write CSS that does not violate DRY principles. Any + CSS statement can be extended */ + +.content-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} -## Usage +.message-window { + @extend .content-window; + background-color: #0000ff; +} -Save any CSS you want in a file with extension `.css`. +.notification-window { + @extend .content-window; + background-color: #ff0000; +} -```xml - - +.settings-window { + @extend .content-window; + background-color: #ccc; +} - - +/* The above Sass will be compile into the following CSS */ - -
-
+.content-window, +.message-window, +.notification-window, +.settings-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} -``` +.message-window { + background-color: #0000ff; +} + +.notification-window { + background-color: #ff0000; +} + +.settings-window { + background-color: #ccc; +} + +/* Extending a CSS statement is preferable to creating a mixin + because of the way it groups together the classes that all share + the same base styling. If this was done with a mixin, the font-size, + padding, color, and border-radius would be duplicated for each statement + that called the mixin. While it won't affect your workflow, it will + add unnecessary bloat to the files created by the Sass compiler. */ + +/* ######################### + ## PLACEHOLDER SELECTORS + ######################### */ + +/* Placeholders are useful when creating a CSS statement to extend. If you wanted to create + a CSS statement that was exclusively used with @extend, you can do so using a placeholder. + Placeholders begin with a '%' instead of '.' or '#'. Placeholders will not appear in the + compiled CSS. */ + +%content-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + @extend %content-window; + background-color: #0000ff; +} -## Precedence +/* The above Sass would compile to the following CSS */ -As you noticed an element may be targetted by more than one selector. -and may have a property set on it in more than one. -In these cases, one of the rules takes precedence over others. +.message-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} -Given the following CSS: +.message-window { + background-color: #0000ff; +} + +/* #################### + ## MATH OPERATIONS + #################### */ + +/* Sass provides the following operators: +, -, *, /, and %. These can + be useful for calculating values directly in your Sass files instead + of using values that you've already calculated by hand. Below is an example + of a setting up a simple two column design. */ + +$content-area: 960px; +$main-content: 600px; +$sidebar-content: 300px; -```css -/*A*/ -p.class1[attr='value'] +$main-size: $main-content / $content-area * 100%; +$sidebar-size: $sidebar-content / $content-area * 100%; +$gutter: 100% - ($main-size + $sidebar-size); -/*B*/ -p.class1 {} +body { + width: 100%; +} -/*C*/ -p.class2 {} +.main-content { + width: $main-size; +} -/*D*/ -p {} +.sidebar { + width: $sidebar-size; +} -/*E*/ -p { property: value !important; } +.gutter { + width: $gutter; +} -``` +/* The above Sass would compile to the CSS below */ -and the following markup: +body { + width: 100%; +} -```xml -

-

-``` +.main-content { + width: 62.5%; +} -The precedence of style is as followed: -Remember, the precedence is for each **property**, not for the entire block. +.sidebar { + width: 31.25%; +} -* `E` has the highest precedence because of the keyword `!important`. - It is recommended to avoid this unless it is strictly necessary to use. -* `F` is next, because it is inline style. -* `A` is next, because it is more "specific" than anything else. - more specific = more specifiers. here 3 specifiers: 1 tagname `p` + - class name `class1` + 1 attribute `attr='value'` -* `C` is next. although it has the same specificness as `B` - but it appears last. -* Then is `B` -* and lastly is `D`. +.gutter { + width: 6.25%; +} + +``` -## Compatibility +## Usage -Most of the features in CSS2 (and gradually in CSS3) are compatible across -all browsers and devices. But it's always vital to have in mind the compatibility -of what you use in CSS with your target browsers. -[QuirksMode CSS](http://www.quirksmode.org/css/) is one of the best sources for this. -To run a quick compatibility check, [CanIUse](http://caniuse.com) is a great resource. +## Compatibility -## Further Reading -* [Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade](http://www.vanseodesign.com/css/css-specificity-inheritance-cascaade/) -* [QuirksMode CSS](http://www.quirksmode.org/css/) -* [Z-Index - The stacking context](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Understanding_z_index/The_stacking_context) +## Further Reading -- cgit v1.2.3 From 25d5d07dd342dc57831785032b813b3c2b3a5a9e Mon Sep 17 00:00:00 2001 From: Sean Corrales Date: Thu, 8 Oct 2015 15:37:54 -0500 Subject: Updating function instructions. --- sass.html.markdown | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'sass.html.markdown') diff --git a/sass.html.markdown b/sass.html.markdown index d1e0721f..9bc72478 100644 --- a/sass.html.markdown +++ b/sass.html.markdown @@ -188,7 +188,8 @@ body.noLinks a { #################### */ /* Sass provides functions that can be used to accomplish a variety of tasks. Consider the following */ - + +/* Functions can be invoked by using their name and passing in the required arguments */ body { width: round(10.25px); } @@ -207,19 +208,19 @@ body { background-color: rgba(0, 0, 0, 0.75); } -/* You may also define your own functions. Functions are very similar to mixins. When trying to choose between - a function or a mixin, remember that functions are best for returning values while mixins are best for - generating CSS while functions are better for logic that might be used throughout your Sass code. The - examples in the Math Operators' section are ideal candidates for becoming a reusable function. */ - -/* This function will take a target size and the parent size and calculate and return the percentage */ +/* You may also define your own functions. Functions are very similar to mixins. When trying + to choose between a function or a mixin, remember that functions are best for returning + values while mixins are best for generating CSS while functions are better for logic that + might be used throughout your Sass code. The examples in the Math Operators' section are + ideal candidates for becoming a reusable function. */ + +/* This function will take a target size and the parent size and calculate and return + the percentage */ @function calculate-percentage($target-size, $parent-size) { @return $target-size / $parent-size * 100%; } -/* Functions can be invoked by using their name and passing in the required arguments */ - $main-content: calculate-percentage(600px, 960px); .main-content { -- cgit v1.2.3 From 9c267f14736f813c5f420d50d93f344faec261cb Mon Sep 17 00:00:00 2001 From: Sean Corrales Date: Mon, 12 Oct 2015 17:21:03 -0500 Subject: Cleaning up formatting. Adding usage and compatibility text --- sass.html.markdown | 89 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 47 insertions(+), 42 deletions(-) (limited to 'sass.html.markdown') diff --git a/sass.html.markdown b/sass.html.markdown index 9bc72478..77e82a09 100644 --- a/sass.html.markdown +++ b/sass.html.markdown @@ -20,16 +20,13 @@ relatively quickly. It does not provide any new styling options but rather the tools to write your CSS more efficiently and make maintenance much easier. -Sass files must be compiled into CSS. You can use any number of commandline -tools to compile Sass into CSS. Many IDEs also offer Sass compilation, as well. - - ```sass -/* Like CSS, Sass uses slash-asterisk to denote comments. Slash-asterisk comments - can span multiple lines. These comments will appear in your compiled CSS */ +/* Like CSS, Sass uses slash-asterisk to denote comments. Slash-asterisk + comments can span multiple lines. These comments will appear + in your compiled CSS */ -// Sass also supports single line comments that use double slashes. These comments will -// not be rendered in your compiled CSS +// Sass also supports single line comments that use double slashes. These +// comments will not be rendered in your compiled CSS /* #################### ## VARIABLES @@ -52,8 +49,7 @@ h1 { font-size: $headline-size; } -/* After compiling the Sass files into CSS, you'll have the following code - in your generated CSS file */ +/* Generated CSS result */ a { color: #0000ff; @@ -91,7 +87,7 @@ article { } } -/* The above will compile into the following CSS */ +/* Generated CSS result */ article { font-size: 14px; } @@ -115,16 +111,16 @@ article img { } /* It is recommended to not nest too deeply as this can cause issues with - specificity and make your CSS harder to work with and maintain. Best practices - recommend going no more than 3 levels deep when nesting. */ + specificity and make your CSS harder to work with and maintain. Best + practices recommend going no more than 3 levels deep when nesting. */ /* ############################### ## REFERENCE PARENT SELECTORS ############################### */ -/* Reference parent selectors are used when you're nesting statements and want to - reference the parent selector from within the nested statements. You can reference - a parent using & */ +/* Reference parent selectors are used when you're nesting statements and want + to reference the parent selector from within the nested statements. You can + reference a parent using & */ a { text-decoration: none; @@ -139,7 +135,7 @@ a { } } -/* The above Sass will compile into the CSS below */ +/* Generated CSS result */ a { text-decoration: none; @@ -160,8 +156,8 @@ body.noLinks a { #################### */ /* Mixins allow you to define reusable chunks of CSS. They can take one or more - arguments to allow you to make reusable pieces of styling. Mixins can also - be very helpful when dealing with vendor prefixes. */ + arguments to allow you to make reusable pieces of styling. Mixins very + helpful when dealing with vendor prefixes. */ @mixin form-button($color, $size, $border-radius) { color: $color; @@ -175,7 +171,7 @@ body.noLinks a { @include form-button(#0000ff, 16px, 4px); } -/* The above mixin will compile into the following css */ +/* Generated CSS result */ .user-form .submit { color: #0000ff; @@ -187,9 +183,11 @@ body.noLinks a { ## FUNCTIONS #################### */ -/* Sass provides functions that can be used to accomplish a variety of tasks. Consider the following */ +/* Sass provides functions that can be used to accomplish a variety of + tasks. Consider the following */ -/* Functions can be invoked by using their name and passing in the required arguments */ +/* Functions can be invoked by using their name and passing in the + required arguments */ body { width: round(10.25px); } @@ -198,7 +196,7 @@ body { background-color: fade_out(#000000, 0.25) } -/* The above Sass will compile into the following CSS */ +/* Generated CSS result */ body { width: 10px; @@ -208,14 +206,15 @@ body { background-color: rgba(0, 0, 0, 0.75); } -/* You may also define your own functions. Functions are very similar to mixins. When trying - to choose between a function or a mixin, remember that functions are best for returning - values while mixins are best for generating CSS while functions are better for logic that - might be used throughout your Sass code. The examples in the Math Operators' section are - ideal candidates for becoming a reusable function. */ - -/* This function will take a target size and the parent size and calculate and return - the percentage */ +/* You may also define your own functions. Functions are very similar to + mixins. When trying to choose between a function or a mixin, remember + that functions are best for returning values while mixins are best for + generating CSS while functions are better for logic that might be used + throughout your Sass code. The examples in the Math Operators' section + are ideal candidates for becoming a reusable function. */ + +/* This function will take a target size and the parent size and calculate + and return the percentage */ @function calculate-percentage($target-size, $parent-size) { @return $target-size / $parent-size * 100%; @@ -231,7 +230,7 @@ $main-content: calculate-percentage(600px, 960px); width: calculate-percentage(300px, 960px); } -/* The above Sass will compile into the following CSS */ +/* Generated CSS result */ .main-content { width: 62.5%; @@ -241,9 +240,9 @@ $main-content: calculate-percentage(600px, 960px); width: 31.25%; } -/* #################### +/* ##################### ## EXTEND/INHERITANCE - #################### */ + ##################### */ /* Sass allows you to extend an existing CSS statement. This makes it very easy to write CSS that does not violate DRY principles. Any @@ -271,7 +270,7 @@ $main-content: calculate-percentage(600px, 960px); background-color: #ccc; } -/* The above Sass will be compile into the following CSS */ +/* Generated CSS result */ .content-window, .message-window, @@ -306,10 +305,10 @@ $main-content: calculate-percentage(600px, 960px); ## PLACEHOLDER SELECTORS ######################### */ -/* Placeholders are useful when creating a CSS statement to extend. If you wanted to create - a CSS statement that was exclusively used with @extend, you can do so using a placeholder. - Placeholders begin with a '%' instead of '.' or '#'. Placeholders will not appear in the - compiled CSS. */ +/* Placeholders are useful when creating a CSS statement to extend. If you + wanted to create a CSS statement that was exclusively used with @extend, + you can do so using a placeholder. Placeholders begin with a '%' instead + of '.' or '#'. Placeholders will not appear in the compiled CSS. */ %content-window { font-size: 14px; @@ -323,7 +322,7 @@ $main-content: calculate-percentage(600px, 960px); background-color: #0000ff; } -/* The above Sass would compile to the following CSS */ +/* Generated CSS result */ .message-window { font-size: 14px; @@ -369,7 +368,7 @@ body { width: $gutter; } -/* The above Sass would compile to the CSS below */ +/* Generated CSS result */ body { width: 100%; @@ -391,9 +390,15 @@ body { ## Usage +Sass files must be compiled into CSS. You can use any number of commandline +tools to compile Sass into CSS. Many IDEs also offer Sass compilation, as well. +[Compass](http://compass-style.org/) is one of the more popular tools for Sass compilation. ## Compatibility +Sass can be used in any project as long as you have something to compile it +into CSS. You'll want to verify that the CSS you're using is compatible +with your target browsers. -## Further Reading +[QuirksMode CSS](http://www.quirksmode.org/css/) and [CanIUse](http://caniuse.com) great resources for checking compatibility. \ No newline at end of file -- cgit v1.2.3 From c357be714fe39c8c97e4d1ac12b3cdfb673d1e1e Mon Sep 17 00:00:00 2001 From: Sean Corrales Date: Tue, 13 Oct 2015 09:51:26 -0500 Subject: Fixing formatting. Writing section about imports and partials. --- sass.html.markdown | 74 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 16 deletions(-) (limited to 'sass.html.markdown') diff --git a/sass.html.markdown b/sass.html.markdown index 77e82a09..e03231ff 100644 --- a/sass.html.markdown +++ b/sass.html.markdown @@ -28,9 +28,9 @@ easier. // Sass also supports single line comments that use double slashes. These // comments will not be rendered in your compiled CSS -/* #################### +/* ############ ## VARIABLES - #################### */ + ############ */ /* Sass allows you to define variables that can be used throughout your stylesheets. Variables are defined by placing a '$' in front @@ -60,9 +60,9 @@ h1 { font-size: 24px; } -/* #################### +/* ########## ## NESTING - #################### */ + ########## */ /* Nesting allows you to easily group together statements and nest them in a way that indicates their hierarchy */ @@ -114,9 +114,9 @@ article img { specificity and make your CSS harder to work with and maintain. Best practices recommend going no more than 3 levels deep when nesting. */ -/* ############################### +/* ############################# ## REFERENCE PARENT SELECTORS - ############################### */ + ############################# */ /* Reference parent selectors are used when you're nesting statements and want to reference the parent selector from within the nested statements. You can @@ -151,9 +151,9 @@ body.noLinks a { } -/* #################### +/* ######### ## MIXINS - #################### */ + ######### */ /* Mixins allow you to define reusable chunks of CSS. They can take one or more arguments to allow you to make reusable pieces of styling. Mixins very @@ -179,9 +179,9 @@ body.noLinks a { border-radius: 4px; } -/* #################### +/* ############ ## FUNCTIONS - #################### */ + ############ */ /* Sass provides functions that can be used to accomplish a variety of tasks. Consider the following */ @@ -240,6 +240,48 @@ $main-content: calculate-percentage(600px, 960px); width: 31.25%; } +/* ####################### + ## PARTIALS AND IMPORTS + ####################### */ +/* Sass allows you to create partial files. This can help keep your Sass + code modularized. Partial files should begin with an '_', e.g. _reset.css. + Partials are not generated into CSS. */ + +/* Consider the following CSS which we'll put in a file called _reset.css */ + +html, +body, +ul, +ol { + margin: 0; + padding: 0; +} + +/* Sass offers @import which can be used to import partials into a file. + This differs from the traditional CSS @import statement which makes + another HTTP request to fetch the imported file. Sass takes the + imported file and combines it with the compiled code. */ + +@import 'reset'; + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + +/* Generated CSS result */ + +html, body, ul, ol { + margin: 0; + padding: 0; +} + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + + /* ##################### ## EXTEND/INHERITANCE ##################### */ @@ -301,9 +343,9 @@ $main-content: calculate-percentage(600px, 960px); that called the mixin. While it won't affect your workflow, it will add unnecessary bloat to the files created by the Sass compiler. */ -/* ######################### +/* ######################## ## PLACEHOLDER SELECTORS - ######################### */ + ######################## */ /* Placeholders are useful when creating a CSS statement to extend. If you wanted to create a CSS statement that was exclusively used with @extend, @@ -335,9 +377,9 @@ $main-content: calculate-percentage(600px, 960px); background-color: #0000ff; } -/* #################### +/* ################## ## MATH OPERATIONS - #################### */ + ################## */ /* Sass provides the following operators: +, -, *, /, and %. These can be useful for calculating values directly in your Sass files instead @@ -397,8 +439,8 @@ tools to compile Sass into CSS. Many IDEs also offer Sass compilation, as well. ## Compatibility -Sass can be used in any project as long as you have something to compile it +Sass can be used in any project as long as you have a program to compile it into CSS. You'll want to verify that the CSS you're using is compatible with your target browsers. -[QuirksMode CSS](http://www.quirksmode.org/css/) and [CanIUse](http://caniuse.com) great resources for checking compatibility. \ No newline at end of file +[QuirksMode CSS](http://www.quirksmode.org/css/) and [CanIUse](http://caniuse.com) are great resources for checking compatibility. \ No newline at end of file -- cgit v1.2.3 From 7a885b86c6b7054aaf48275c252de6ccfc718098 Mon Sep 17 00:00:00 2001 From: Sean Corrales Date: Wed, 14 Oct 2015 15:22:46 -0500 Subject: Adding sections on Sass functions, import, partials, and math operations. Adding some comments regarding best practices. Adding section on compatibility. --- sass.html.markdown | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 215 insertions(+) (limited to 'sass.html.markdown') diff --git a/sass.html.markdown b/sass.html.markdown index 509aee9b..02bec47f 100644 --- a/sass.html.markdown +++ b/sass.html.markdown @@ -3,6 +3,7 @@ language: sass filename: learnsass.scss contributors: - ["Laura Kyle", "https://github.com/LauraNK"] + - ["Sean Corrales", "https://github.com/droidenator"] --- Sass is a CSS extension language that adds features such as variables, nesting, mixins and more. @@ -11,6 +12,7 @@ Sass (and other preprocessors, such as [Less](http://lesscss.org/)) help develop Sass has two different syntax options to choose from. SCSS, which has the same syntax as CSS but with the added features of Sass. Or Sass (the original syntax), which uses indentation rather than curly braces and semicolons. This tutorial is written using SCSS. +If you're already familiar with CSS3, you'll be able to pick up Sass relatively quickly. It does not provide any new styling options but rather the tools to write your CSS more efficiently and make maintenance much easier. ```scss @@ -121,6 +123,69 @@ div { +/*Functions +==============================*/ + + + +/* Sass provides functions that can be used to accomplish a variety of + tasks. Consider the following */ + +/* Functions can be invoked by using their name and passing in the + required arguments */ +body { + width: round(10.25px); +} + +.footer { + background-color: fade_out(#000000, 0.25) +} + +/* Compiles to: */ + +body { + width: 10px; +} + +.footer { + background-color: rgba(0, 0, 0, 0.75); +} + +/* You may also define your own functions. Functions are very similar to + mixins. When trying to choose between a function or a mixin, remember + that mixins are best for generating CSS while functions are better for + logic that might be used throughout your Sass code. The examples in + the Math Operators' section are ideal candidates for becoming a reusable + function. */ + +/* This function will take a target size and the parent size and calculate + and return the percentage */ + +@function calculate-percentage($target-size, $parent-size) { + @return $target-size / $parent-size * 100%; +} + +$main-content: calculate-percentage(600px, 960px); + +.main-content { + width: $main-content; +} + +.sidebar { + width: calculate-percentage(300px, 960px); +} + +/* Compiles to: */ + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + + /*Extend (Inheritance) ==============================*/ @@ -150,6 +215,12 @@ div { border-color: #22df56; } +/* Extending a CSS statement is preferable to creating a mixin + because of the way it groups together the classes that all share + the same base styling. If this was done with a mixin, the width, + height, and border would be duplicated for each statement that + called the mixin. While it won't affect your workflow, it will + add unnecessary bloat to the files created by the Sass compiler. */ @@ -172,6 +243,7 @@ ul { /* '&' will be replaced by the parent selector. */ /* You can also nest pseudo-classes. */ /* Keep in mind that over-nesting will make your code less maintainable. +Best practices recommend going no more than 3 levels deep when nesting. For example: */ ul { @@ -212,6 +284,140 @@ ul li a { +/*Partials and Imports +==============================*/ + + + +/* Sass allows you to create partial files. This can help keep your Sass + code modularized. Partial files should begin with an '_', e.g. _reset.css. + Partials are not generated into CSS. */ + +/* Consider the following CSS which we'll put in a file called _reset.css */ + +html, +body, +ul, +ol { + margin: 0; + padding: 0; +} + +/* Sass offers @import which can be used to import partials into a file. + This differs from the traditional CSS @import statement which makes + another HTTP request to fetch the imported file. Sass takes the + imported file and combines it with the compiled code. */ + +@import 'reset'; + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + +/* Compiles to: */ + +html, body, ul, ol { + margin: 0; + padding: 0; +} + +body { + font-size: 16px; + font-family: Helvetica, Arial, Sans-serif; +} + + + +/*Placeholder Selectors +==============================*/ + + + +/* Placeholders are useful when creating a CSS statement to extend. If you + wanted to create a CSS statement that was exclusively used with @extend, + you can do so using a placeholder. Placeholders begin with a '%' instead + of '.' or '#'. Placeholders will not appear in the compiled CSS. */ + +%content-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + @extend %content-window; + background-color: #0000ff; +} + +/* Compiles to: */ + +.message-window { + font-size: 14px; + padding: 10px; + color: #000; + border-radius: 4px; +} + +.message-window { + background-color: #0000ff; +} + + + +/*Math Operations +==============================*/ + + + +/* Sass provides the following operators: +, -, *, /, and %. These can + be useful for calculating values directly in your Sass files instead + of using values that you've already calculated by hand. Below is an example + of a setting up a simple two column design. */ + +$content-area: 960px; +$main-content: 600px; +$sidebar-content: 300px; + +$main-size: $main-content / $content-area * 100%; +$sidebar-size: $sidebar-content / $content-area * 100%; +$gutter: 100% - ($main-size + $sidebar-size); + +body { + width: 100%; +} + +.main-content { + width: $main-size; +} + +.sidebar { + width: $sidebar-size; +} + +.gutter { + width: $gutter; +} + +/* Compiles to: */ + +body { + width: 100%; +} + +.main-content { + width: 62.5%; +} + +.sidebar { + width: 31.25%; +} + +.gutter { + width: 6.25%; +} + ``` @@ -226,6 +432,15 @@ Because people were constantly writing it as "SASS", the creator of the language If you want to play with Sass in your browser, check out [SassMeister](http://sassmeister.com/). You can use either syntax, just go into the settings and select either Sass or SCSS. + +## Compatibility + +Sass can be used in any project as long as you have a program to compile it +into CSS. You'll want to verify that the CSS you're using is compatible +with your target browsers. + +[QuirksMode CSS](http://www.quirksmode.org/css/) and [CanIUse](http://caniuse.com) are great resources for checking compatibility. + ## Further reading * [Official Documentation](http://sass-lang.com/documentation/file.SASS_REFERENCE.html) -- cgit v1.2.3