Sass learning path (5) -- variables, sass learning path variables
1. Define variables:
The keyword for defining variables in Sass is '$' (after all, programmers are short of money), and assign values using colons (:). For example:
- $ Width: 200px; // defines a variable named width with a value of 200px.
2. Common variables and default variables:
Common variables are the variables declared in the above way in braces, which can be used globally.
The default variable must be added after the declared variable! Default:
- $ LineHeight: 1.5! Default;
The default variable can be understood as an initial value for the variable. If the variable does not have a value when it is used, the default value is used. Defining common variables before or after the default variables overwrites the value of the default variables.
- $ LineHeight: 2;
- $ LineHeight: 1.5! Default;
- . Line {
- Line-height: $ lineHeight;
- }
- // The result is:
- . Line {
- Line-height: 2;
- }
You can also think of the default variable as the variable with the lowest priority. The priority order is :! Default <Common variable <! Important
3. Local and global variables
Global variables: variables defined outside selector, function, and hybrid macro.
Local variable: it is actually the content above.
Here is an example:
- // SCSS
- $ Color: orange! Default; // defines global variables
- . Block {
- Color: $ color; // call global variables
- }
- Em {
- $ Color: red; // defines local variables
- A {
- Color: $ color; // call a local variable
- }
- }
- Span {
- Color: $ color; // call global variables
- }
The compiled result is as follows:
- // CSS
- . Block {
- Color: orange;
- }
- Em {
- Color: red;
- }
- Span {
- Color: orange;
- }
It can be seen that the priority of the local variables defined inside the selector is higher.