What is theCSS
The preprocessor
The CSS preprocessor defines a new language. Its basic idea is to use a special programming language to design web pages and then compile them into normal CSS files. Developers only need to use this language to code, while reducing the tedious process of CSS code writing, it makes your CSS more concise, more adaptable, more readable, more hierarchical, easier to maintain code and many other benefits.
CSS preprocessors style Web pages in a specialized programming language and then compile them into normal CSS files for use by projects. The CSS preprocessor adds programming features to the CSS, regardless of browser compatibility.
Types of CSS preprocessors
The three most popular preprocessors are Sass, Less, and Stylus.
SASS
: Born in 2007, the earliest and most matureCSS preprocessor
withThe ruby community
The support andcompass
This is the most powerfulCSS framework
.Sass
Use the default.sass
Extension.
Sass now has two sets of syntax rules: one still uses indentation as a separator to distinguish blocks of code; Another set of rules (affected by LESS) uses curly braces ({}) as delimiters like the CSS. The latter syntax is also known as SCSS (SCSS uses the.scss extension by default) and is supported in versions after Sass3.
LESS
: Appeared in 2009, bySASS
The influence is larger but also usedCSS
Syntax for most developers and designers easier to use inThe ruby community
Outside supporters far outnumberSASS
Its disadvantage is that compared withSASS
The programmable features are not enough, but the advantages are simplicity and compatibilityCSS
And the other way aroundSASS
evolvedSCSS
The Times, the famousTwitter Bootstrap
Is to useLESS
Do the underlying language,Less
Use the default.Less
Extension.
According to Wikipedia, LESS is Alexis Sellier’s open source project inspired by Sass. At the time, SASS used indentation as a separator to distinguish blocks of code, rather than the curly braces ({}) commonly used in CSS. To make CSS easier for existing users, Alexis developed LESS and provided CSS-like writing capabilities.
Stylus
Born in 2010Node.js
Community, mainly used to giveNode
The projectCSS
Pre-processing support, which has a certain constituency within this community, is not at all popular in a broad senseSASS
andLESS
.Stylus
Use the default.styl
Extension.
Stylus is described as a revolutionary new language that provides an efficient, dynamic, and usable expression to generate CSS for use by browsers. The Stylus supports both indentation and CSS regular style writing rules.
Preprocessor syntax generally includes:
- variable
- nested
- Mixed Mixins
- operation
- Import (Import)
- function
grammar
CSS precompiled language syntax is very simple, if your editor is good, File Watching will automatically find the syntax error.
The syntax of Sass3, Scss and Less is very similar. The old Sass and Stylus are special. Stylus has a more flexible syntax and more powerful functions.
For example, 1. Sass
/* Old sass syntax */
h1
color: # 000
font-size: 24px
Copy the code
2, SCSS
/ * * / SCSS grammar
h1{
color: # 000;
font-size: 24px;
}
Copy the code
3, LESS
/ * * / less grammar
h1{
color: # 000;
font-size: 24px;
}
Copy the code
4, Stylus
* Style of Stylus syntax */
h1{
color: # 000;
font-size: 24px;
}
h1
color: # 000
font-size: 24px
h1
color #fff
font-size 24px
Copy the code
variable
/* the Scss variable must be $start */
$mainColor: # 000;
h1{
color: $mainColor;
}
/* 2, Less variable names start with the @ sign */
@mainColor: #000;
h1{
color: @mainColor;
}
Do not start the variable name in Stylus with @; = assign */ to the Stylus
mainColor = # 000;
h1{
color: mainColor;
}
Copy the code
nested
If you need to refer to the same parent element in CSS, you need to write the parent element over and over again. With the CSS preprocessor, there are many fewer words, and the parent relationship is clear.
Instead of introducing the old SASS notation, I will introduce the SCSS notation.
The same generation
nav{
color: # 000;
font-size: 16px;
}
nav a{
color: #0000cc;
font-size: 18px;
}
nav a.first{
color:red
}
Copy the code
/*Scss, less, Stylus nesting, pseudo-class nesting all use &*/
nav {
color: # 000;
font-size: 16px;
a {
color: #0000cc;
font-size: 18px;
&.first {
color: red; }}}/* Nesting of the Stylus can also be written like */
nav
color # 000
font-size 16px
a
color #0000cc
font-size 18px
&.first
color red
Copy the code
Mixins hybrid
Mixins are one of the most powerful features of the CSS preprocessor language. In simple terms, Mixins can be used repeatedly by many selectors by pulling out styles as individually defined modules.
If you’re writing a CSS style, you’ll have to repeat it multiple times. In CSS preprocessor language, you can define a Mixin for these common CSS styles and then call your Mixin directly where your CSS needs to use those styles. This is a very useful feature, Mixins are treated as a recognized selector, and you can also define variables or default parameters in Mixins.
The CSS precompilation tool allows you to apply existing class and ID styles to a different selector. Such as:
/*Scss, less */
.circle{
border-radius: 100%;
background: #ccc;
}
.small-circle{
width: 50px;
height: 50px;
.circle
}
.big-circle{
width: 100px;
height: 100px;
.circle
}
/*Stylus Mixin hybrid syntax */
circle{
border-radius: 100%;
background: #ccc;
}
.small-circle{
width: 50px;
height: 50px;
circle
}
.big-circle{
width: 100px;
height: 100px;
circle
}
Copy the code
1. Mixing can transfer parameters
/* this is SCSS example 1. Change to less, just change $to @2. To change Stylus, just change circle to circle */
.circle($radius){
border-radius: $radius;
background: #ccc;
}
.small-circle{
width: 50px;
height: 50px;
.circle(5px)}Copy the code
2, can be mixed with default values
/* this is SCSS example 1. Change to less, just change $to @2. To change Stylus, just change circle to circle, : to = */
.circle($radius:5px){
border-radius: $radius;
background: #ccc;
}
.small-circle{
width: 50px;
height: 50px;
.circle
}
/* This is a sample Stylus */
circle($radius=5px) {border-radius: $radius;
background: #ccc;
}
.small-circle{
width: 50px;
height: 50px;
circle
}
Copy the code
operation
/* This is a Scss sample */
$test:300px;
.test_01{
width: $test + 20px; //height: $test*2 ;
color: #ccc - 10;
}
/* This is the Less sample */
@test:300px;
.test_01{
width: @test + 20px; //height: @test*2 ;
color: #ccc - 10;
}
/* This is a sample Stylus */
test=300px
.test_01{
width: test + 20px; //height: test*2 ;
color: #ccc - 10;
}
Copy the code
Scope (Scope
)
CSS preprocessor variables in language and other languages, can realize the value of reuse, it also exists the life cycle, which is the Scope (variable Scope, developers used to call it Scope), the simple point is the concept of local variable or a global variable, find the variable sequence is to find in local definition first, if you cannot find, Then look up the superior definition, all the way to global. Let’s use a simple example to explain the scope usage of these three CSS preprocessors.
Sass
The scope of the
The scope of Sass is the worst among the three preprocessors, so there is no global variable in Sass. Look at the following code:
/ * Sass style * /
$color: black;
.scoped {
$bg: blue;
$color: white;
color: $color;
background-color:$bg;
}
.unscoped {
color:$color;
}
Copy the code
First look at the translated CSS style:
.scoped {
color:white;/* is white */
background-color:blue;
}
.unscoped {
color:white;/* White (no global variable concept) */
}
Copy the code
The example clearly shows that when defining a variable in Sass style, there is no concept of a global variable in calling a variable. Therefore, when defining the same variable name in Sass, you must be careful when calling it, otherwise you will introduce errors in your style.
LESS
The scope of the
The scope in LESS is very much the same as the scope in other programming languages. It first looks for a locally defined variable, and if it doesn’t find one, it bubbles down to the root. In the same example above, let’s see what it does under LESS.
/ * LESS style * /
@color: black;
.scoped {
@bg: blue;
@color: white;
color: @color;
background-color:@bg;
}
.unscoped {
color:@color;
}
Copy the code
Translated CSS styles:
.scoped {
color:white;/* White (local variable called) */
background-color:blue;
}
.unscoped {
color:black;/* Black (global variable called) */
}
Copy the code
Stylus
The scope of the
Stylus, though a late starter, has the same scoped properties as LESS, supporting both global and local variables. It bubbles up to the root.
function
Sass, Less, and Stylus all offer a wealth of built-in functions. Stylus allows for custom functions, so look them up when you use them.
The import
The import syntax is the same for several CSS precompiled languages. Css3’s @import looks the same, but cSS3’s @import causes asynchronous loading. It’s not what we expected.
The CSS precompiler @import solves this problem by combining multiple style files into one, essentially making it easier for the front end to manage code without loading it asynchronously.
@import 'reset.scss'// SCSS import style @import"reset"; // Less Import By default, less files are imported@import "reset.css"// Stylus Import The default import is a CSS fileCopy the code
Looping statements
Sass (Scss), Stylus supports circular statements
Branch statements
Sass (Scss), Stylus supports conditional statements
Develop reading
- Sass website
- LESS website
- The Stylus’s official website