Sass description:
As we all know, css
is not a programming language. He could not like js
and python
that have the ability to deal with logic, and even import other css
files in style can do. And Sass
in order to solve css
these problems. He allows you to use variables, nested rules mixins
, import and many other features, and is fully compatible css
syntax. Sass
Files can not be directly recognized pages, finished Sass
after, but also need specialized tools into css
to use.
Sass file extension:
Sass
There are two file extension, one scss
, one sass
. Different name suffix corresponding syntax is not the same. Here we use the scss
extension. Including mentioned later Sass
grammar, it is also scss
grammar extension.
Sass basic syntax:
Comment:
Support /* comment */
and // 注释
in two ways.
Nesting:
Sass
Syntax allows nesting. For example, #main
the next there is a class .header
, then we can write the following form:
#main{ background: #ccc; .header{ width: 20px; height: 20px; } }
Write them more intuitive. To see that this .header
is #main
under.
Refers to the parent selector &
( ):
Sometimes, nested sub-selectors, you need to use the parent selector, then this time can &
be expressed. Sample code is as follows:
a{ font-weight: bold; text-decoration: none; &:hover{ color: #888; } }
Define the variable:
Yes, you heard wrong. In Sass
the variables can be defined. For some of the more common values, we can store up by variable later when you want to use it directly on it. Define variables using $
symbols. Sample code is as follows:
$mainWidth: 980px; #main{ width: $mainWidth; }
Operations:
In Sass
support operations. For example, now has a total width of the containers are 900
, to put an average of three boxes inside, then we can set their width through the variable. Sample code is as follows:
$mainWidth: 900px; .box{ width: $mainWidth/3; }
@import syntax:
In css
the @import
only import css
files, and has a great influence on the performance of the site. And Sass
in @import
the full realization of the set is its own mechanism. He can simply copy the code of the specified file to the local import. Sample code is as follows:
@import "init.scss";
@extend syntax:
Sometimes we have a selector, you may need another selector style, then we can extend
be directly assigned selector style join. Sample code is as follows:
.error{ background-color: #fdd; border: 1px solid #f00; } .serious-error{ @extend .error; border-width: 3px; }
@mixin syntax:
Sometimes some style code. We may take a lot of places. Then we can define him as i mixin
. When you need to use a direct reference to it. Sample code is as follows:
@mixin large-text { font: { family: Arial; size: 20px; weight: bold; } color: #ff0000; }
If you want to use the rest of the mixin
time, it can @include
be included. Sample code is as follows:
.page-title { @include large-text; padding: 4px; margin-top: 10px; }
@mixin
You can also use parameters. Sample code is as follows:
@mixin sexy-border($color, $width) { border: { color: $color; width: $width; style: dashed; } }
Then later in include
time, we need to pass a parameter. Sample code is as follows:
p { @include sexy-border(blue, 1px); }
A more detailed tutorial:
A more detailed tutorial can refer to: http: //sass.bootcss.com/docs/sass-reference/.