2015-10-09 20:15:04 +03:00
---
language: sass
filename: learnsass.scss
contributors:
- ["Laura Kyle", "https://github.com/LauraNK"]
2015-10-14 23:22:46 +03:00
- ["Sean Corrales", "https://github.com/droidenator"]
2015-10-29 17:05:36 +03:00
- ["Kyle Mendes", "https://github.com/pink401k"]
2016-06-19 16:59:26 +03:00
- ["Keith Miyake", "https://github.com/kaymmm"]
2015-10-09 20:15:04 +03:00
---
2015-10-29 17:05:36 +03:00
Sass is a CSS extension language that adds features such as variables, nesting, mixins and more.
Sass (and other preprocessors, such as [Less ](http://lesscss.org/ )) help developers write maintainable and DRY (Don't Repeat Yourself) code.
2015-10-09 20:15:04 +03:00
2015-10-29 17:05:36 +03:00
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.
2015-10-09 20:15:04 +03:00
This tutorial is written using SCSS.
2015-10-29 17:05:36 +03:00
If you're already familiar with CSS3, you'll be able to pick up Sass relatively quickly. It does not provide any new styling properties but rather the tools to write your CSS more efficiently and make maintenance much easier.
2015-10-09 20:15:04 +03:00
2017-01-01 02:43:35 +03:00
```sass
2015-10-09 20:15:04 +03:00
2015-10-29 17:05:36 +03:00
2015-10-09 20:15:04 +03:00
//Single line comments are removed when Sass is compiled to CSS.
2015-10-29 17:05:36 +03:00
/* Multi line comments are preserved. */
/* Variables
============================== */
2015-10-09 20:15:04 +03:00
/* You can store a CSS value (such as a color) in a variable.
Use the '$' symbol to create a variable. */
2015-10-29 17:05:36 +03:00
2015-10-09 20:15:04 +03:00
$primary-color: #A3A4FF ;
$secondary-color: #51527F ;
2015-10-29 17:05:36 +03:00
$body-font: 'Roboto', sans-serif;
/* You can use the variables throughout your stylesheet.
Now if you want to change a color, you only have to make the change once. */
2015-10-09 20:15:04 +03:00
body {
background-color: $primary-color;
color: $secondary-color;
font-family: $body-font;
}
/* This would compile to: */
body {
background-color: #A3A4FF ;
color: #51527F ;
font-family: 'Roboto', sans-serif;
}
/* This is much more maintainable than having to change the color
each time it appears throughout your stylesheet. */
2015-10-29 17:05:36 +03:00
2016-06-19 16:59:26 +03:00
/* Control Directives
2015-10-29 17:05:36 +03:00
============================== */
2015-10-09 20:15:04 +03:00
2016-06-19 16:59:26 +03:00
/* Sass lets you use @if , @else , @for , @while , and @each to control the
compilation of your code to CSS. */
/* @if/@else blocks behave exactly as you might expect */
$debug: true !default;
@mixin debugmode {
@if $debug {
@debug "Debug mode enabled";
display: inline-block;
}
@else {
display: none;
}
}
.info {
@include debugmode;
}
/* If $debug is set to true, .info is displayed; if it's set to false then
.info is not displayed.
Note: @debug will output debugging information to the command line.
Useful for checking variables while debugging your SCSS. */
.info {
display: inline-block;
}
/* @for is a control loop that iterates through a range of values.
Particularly useful for setting styles on a collection of items.
2016-10-15 17:21:45 +03:00
There are two forms, "through" and "to". The former includes the last value,
2016-06-19 16:59:26 +03:00
the latter stops at the last value. */
@for $c from 1 to 4 {
div:nth-of-type(#{$c}) {
left: ($c - 1) * 900 / 3;
}
}
@for $c from 1 through 3 {
.myclass-#{$c} {
color: rgb($c * 255 / 3, $c * 255 / 3, $c * 255 / 3);
}
}
/* Will compile to: */
div:nth-of-type(1) {
left: 0;
}
div:nth-of-type(2) {
left: 300;
}
div:nth-of-type(3) {
left: 600;
}
.myclass-1 {
color: #555555 ;
}
.myclass-2 {
color: #aaaaaa ;
}
.myclass-3 {
color: white;
// SASS automatically converts #FFFFFF to white
}
/* @while is very straightforward: */
$columns: 4;
$column-width: 80px;
@while $columns > 0 {
.col-#{$columns} {
width: $column-width;
left: $column-width * ($columns - 1);
}
$columns: $columns - 1;
}
/* Will output the following CSS: */
.col-4 {
width: 80px;
left: 240px;
}
.col-3 {
width: 80px;
left: 160px;
}
.col-2 {
width: 80px;
left: 80px;
}
.col-1 {
width: 80px;
left: 0px;
}
2015-10-09 20:15:04 +03:00
2016-06-19 16:59:26 +03:00
/* @each functions like @for , except using a list instead of ordinal values
Note: you specify lists just like other variables, with spaces as
delimiters. */
$social-links: facebook twitter linkedin reddit;
.social-links {
@each $sm in $social-links {
.icon-#{$sm} {
background-image: url("images/#{$sm}.png");
}
}
}
/* Which will output: */
.social-links .icon-facebook {
background-image: url("images/facebook.png");
}
.social-links .icon-twitter {
background-image: url("images/twitter.png");
}
.social-links .icon-linkedin {
background-image: url("images/linkedin.png");
}
.social-links .icon-reddit {
background-image: url("images/reddit.png");
}
2016-10-15 17:21:45 +03:00
/* Mixins
2016-06-19 16:59:26 +03:00
==============================*/
2015-10-09 20:15:04 +03:00
/* If you find you are writing the same code for more than one
element, you might want to store that code in a mixin.
2015-10-29 17:05:36 +03:00
Use the '@mixin' directive, plus a name for your mixin. */
2015-10-09 20:15:04 +03:00
@mixin center {
display: block;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
}
/* You can use the mixin with '@include' and the mixin name. */
div {
@include center;
background-color: $primary-color;
}
2015-10-29 17:05:36 +03:00
/* Which would compile to: */
2015-10-09 20:15:04 +03:00
div {
display: block;
margin-left: auto;
margin-right: auto;
left: 0;
right: 0;
background-color: #A3A4FF ;
}
/* You can use mixins to create a shorthand property. */
@mixin size($width, $height) {
width: $width;
height: $height;
}
2015-10-29 17:05:36 +03:00
/* Which you can invoke by passing width and height arguments. */
2015-10-09 20:15:04 +03:00
.rectangle {
@include size(100px, 60px);
}
.square {
@include size(40px, 40px);
}
2015-10-29 17:05:36 +03:00
/* Compiles to: */
2015-10-09 20:15:04 +03:00
.rectangle {
width: 100px;
2015-10-29 17:05:36 +03:00
height: 60px;
2015-10-09 20:15:04 +03:00
}
.square {
width: 40px;
2015-10-29 17:05:36 +03:00
height: 40px;
2015-10-09 20:15:04 +03:00
}
2015-10-29 17:05:36 +03:00
/* Functions
============================== */
/* Sass provides functions that can be used to accomplish a variety of
2015-10-14 23:22:46 +03:00
tasks. Consider the following */
2015-10-29 17:05:36 +03:00
/* Functions can be invoked by using their name and passing in the
2015-10-14 23:22:46 +03:00
required arguments */
body {
2015-10-29 17:05:36 +03:00
width: round(10.25px);
2015-10-14 23:22:46 +03:00
}
.footer {
2016-06-19 16:59:26 +03:00
background-color: fade_out(#000000, 0.25);
2015-10-14 23:22:46 +03:00
}
/* Compiles to: */
body {
width: 10px;
}
.footer {
background-color: rgba(0, 0, 0, 0.75);
2015-10-29 17:05:36 +03:00
}
/* You may also define your own functions. Functions are very similar to
2015-10-14 23:22:46 +03:00
mixins. When trying to choose between a function or a mixin, remember
2015-10-29 17:05:36 +03:00
that mixins are best for generating CSS while functions are better for
logic that might be used throughout your Sass code. The examples in
2016-10-15 17:21:45 +03:00
the 'Math Operators' section are ideal candidates for becoming a reusable
2015-10-14 23:22:46 +03:00
function. */
2015-10-29 17:05:36 +03:00
/* This function will take a target size and the parent size and calculate
2015-10-14 23:22:46 +03:00
and return the percentage */
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
@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%;
}
2015-10-09 20:15:04 +03:00
2015-10-29 17:05:36 +03:00
/* Extend (Inheritance)
============================== */
2015-10-09 20:15:04 +03:00
2015-10-29 17:05:36 +03:00
/* Extend is a way to share the properties of one selector with another. */
2015-10-09 20:15:04 +03:00
.display {
@include size(5em, 5em);
border: 5px solid $secondary-color;
}
.display-success {
@extend .display;
border-color: #22df56 ;
}
/* Compiles to: */
.display, .display-success {
width: 5em;
height: 5em;
2015-10-29 17:05:36 +03:00
border: 5px solid #51527F ;
2015-10-09 20:15:04 +03:00
}
.display-success {
2015-10-29 17:05:36 +03:00
border-color: #22df56 ;
2015-10-09 20:15:04 +03:00
}
2015-10-29 17:05:36 +03:00
/* Extending a CSS statement is preferable to creating a mixin
because of the way Sass 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
2015-10-14 23:22:46 +03:00
called the mixin. While it won't affect your workflow, it will
add unnecessary bloat to the files created by the Sass compiler. */
2015-10-09 20:15:04 +03:00
2015-10-29 17:05:36 +03:00
/* Nesting
============================== */
2015-10-09 20:15:04 +03:00
2015-10-29 17:05:36 +03:00
/* Sass allows you to nest selectors within selectors */
2015-10-09 20:15:04 +03:00
ul {
list-style-type: none;
margin-top: 2em;
2015-10-29 17:05:36 +03:00
2015-10-09 20:15:04 +03:00
li {
2015-10-29 17:05:36 +03:00
background-color: #FF0000 ;
}
2015-10-09 20:15:04 +03:00
}
/* '& ' will be replaced by the parent selector. */
2015-10-11 16:52:30 +03:00
/* You can also nest pseudo-classes. */
2015-10-09 20:15:04 +03:00
/* Keep in mind that over-nesting will make your code less maintainable.
2015-10-14 23:22:46 +03:00
Best practices recommend going no more than 3 levels deep when nesting.
2015-10-09 20:15:04 +03:00
For example: */
ul {
list-style-type: none;
margin-top: 2em;
2015-10-29 17:05:36 +03:00
2015-10-09 20:15:04 +03:00
li {
background-color: red;
2015-10-29 17:05:36 +03:00
2015-10-09 20:15:04 +03:00
& :hover {
background-color: blue;
}
2015-10-29 17:05:36 +03:00
2015-10-09 20:15:04 +03:00
a {
color: white;
}
2015-10-29 17:05:36 +03:00
}
2015-10-09 20:15:04 +03:00
}
/* Compiles to: */
ul {
list-style-type: none;
margin-top: 2em;
}
ul li {
background-color: red;
}
ul li:hover {
background-color: blue;
}
ul li a {
color: white;
}
2015-10-29 17:05:36 +03:00
/* Partials and Imports
============================== */
2015-10-14 23:22:46 +03:00
/* 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. */
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
/* Consider the following CSS which we'll put in a file called _reset.css */
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
html,
body,
ul,
ol {
margin: 0;
padding: 0;
}
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
/* Sass offers @import which can be used to import partials into a file.
2015-10-29 17:05:36 +03:00
This differs from the traditional CSS @import statement which makes
another HTTP request to fetch the imported file. Sass takes the
2015-10-14 23:22:46 +03:00
imported file and combines it with the compiled code. */
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
@import 'reset';
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
body {
font-size: 16px;
font-family: Helvetica, Arial, Sans-serif;
}
/* Compiles to: */
html, body, ul, ol {
margin: 0;
padding: 0;
2015-10-29 17:05:36 +03:00
}
2015-10-14 23:22:46 +03:00
body {
font-size: 16px;
font-family: Helvetica, Arial, Sans-serif;
}
2015-10-29 17:05:36 +03:00
/* Placeholder Selectors
============================== */
2015-10-14 23:22:46 +03:00
/* 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. */
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
%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 ;
}
2015-10-29 17:05:36 +03:00
/* Math Operations
============================== */
2015-10-14 23:22:46 +03:00
/* 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. */
2015-10-29 17:05:36 +03:00
2015-10-14 23:22:46 +03:00
$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%;
}
2015-10-29 17:05:36 +03:00
```
2015-10-09 20:15:04 +03:00
## SASS or Sass?
2015-10-29 17:05:36 +03:00
Have you ever wondered whether Sass is an acronym or not? You probably haven't, but I'll tell you anyway. The name of the language is a word, "Sass", and not an acronym.
Because people were constantly writing it as "SASS", the creator of the language jokingly called it "Syntactically Awesome StyleSheets".
2015-10-09 20:15:04 +03:00
2015-10-11 16:52:30 +03:00
## Practice Sass
2015-10-09 20:15:04 +03:00
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.
2015-10-14 23:22:46 +03:00
## Compatibility
2016-10-15 17:21:45 +03:00
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.
2015-10-29 17:05:36 +03:00
[QuirksMode CSS ](http://www.quirksmode.org/css/ ) and [CanIUse ](http://caniuse.com ) are great resources for checking compatibility.
2015-10-14 23:22:46 +03:00
2015-10-09 20:15:04 +03:00
## Further reading
* [Official Documentation ](http://sass-lang.com/documentation/file.SASS_REFERENCE.html )
2015-10-11 16:52:30 +03:00
* [The Sass Way ](http://thesassway.com/ ) provides tutorials (beginner-advanced) and articles.