CSS programming framework and css Programming
CSS
Develop a webpage style. It is not a programming language and cannot be programmed with it. This is very painful for programmers and there is no language specification,
According to Daniel Ruan Yifeng, CSS is basically a designer tool, not a programmer tool. In the eyes of programmers, CSS is a very troublesome thing. It has no variables and no conditional statements. It is a simple description of a row, which is quite troublesome to write.
SASS
SASS is a specialized programming language for CSS. It is a CSS development tool. It designs webpage styles through programming languages and then compiles them into normal CSS files.
It provides many convenient methods to modularize and conquer CSS code. Better CSS coding experience. Combined with automated webpack build tools of gulp for compilation and file merging. It greatly saves the designer time and makes CSS development easy and maintainable.
Installation and configuration
Step 1: Install Ruby
SASS depends on the Ruby environment and must be installed first.
During installation,SelectAdd Ruby executables to your PATHThis optionTo add environment variables. Otherwise, the system will prompt that the ruby environment cannot be found when you use the compilation software later.
Step 2: Install sass using the ruby command gem
After ruby is installed, run it in the cmd command line, as shown in
Installation command
1 gem install sass
Command Behavior for upgrading sass
1 gem update sass
SASSCompile
SASS compilation methods include:
1. Integrate and compile/kaola through gulp (not described in this article)
2. Compile through command line (this article will not be detailed)
Sass scss directory address: output directory address
For exampleSass scss/main. scss: style/main.css
The SASS command line tool can automatically monitor directories, so that files under our directories have changed. SASS helps us to automatically compile,
SetSass scss/main. scssSASS source code compilationStyle/main.cssFile.
Directory address monitored by sass -- watch: automatically compiled directory address
Run the -- watch command to enable the sass tool to automatically monitor directories and compile directories.
Sass -- watch watchdir: outputdir -- style nested | compact | compressed | expended
You can use the -- style nested (nested-default) | compact (compression type) | compressed (compression) | expended (Extension) command to specify the format for generating css styles for sass.
SASS provides four compilation formats
Nested: the css code with nested indentation, which is the default value.
Expanded: expanded css code without indentation.
Compact: css code in simple format.
Compressed: the compressed css code.
3. Compile with webstorm development tools
Setp1: Configure the environment
Webstorm-> File-> Setting-> Tool-> File Watchers-> "+"-> SCSS-> OK
Setp2: Create a New SCSS File
The result is as follows: The SCSS file automatically generates the CSS file and the MAP associated file.
SASSSyntax structure
1. Use nested syntax to define variables
$ + Variable name: Value
1 $ color: # ffff; 2 $ border: 1px solid red (multi-value );
2. nested syntax usage
With the benefit of nested syntax, we can remove the public duplicates in the style. When using nested syntax,
If the: hover pseudo-class selector is used, spaces are reserved in the nested syntax. We define a pseudo class for an element so that there cannot be spaces between the element and the pseudo class. (Ie: a: hover ).
&: Used to reference the parent class name (for example: &-title {}=>. nav. nav-title {})
At this time, we must use the "&: hover" symbol to directly use the parent class name without spaces. You can use nesting in attributes:
1. circle {2 background-color: red; 3 background-origin: border-box; 4 background-repeat: no-repeat; 5 background-size: cover; 6} 7. circle2 {8 background: {// note that you need to add ":" color: #000; 9 origin: border-box; 10 repeat: no-repeat; 11 size: cover; 12} 13}
3. @ mixin syntax
Mixin is a pre-defined style block, which can be reused anywhere else.
Syntax structure:
1 @ mixin name ($ parameter .., $ parameter) {2 ....... 3} 4 // use @ include to call mixin 5 @ include name ($ parameter ,...) {6 ....... 7}
4. @ extend inheritance syntax
We use inheritance to reduce repeated definitions.
1 .mm {2 background-color: red;3 a {4 text-align: center;5 }6 }7 .mm2 {8 @extend .mm; background-origin: border-box;9 }
After the child class inherits from the parent class, the Child class can inherit references related to the parent class: nesting of ie:.
5. placeholder % placeholder
1% icon {2 transition: background-color background. 2 s; 3 margin: 0. 5em; 4} 5. error-icon {6 @ extend % icon; 7/* The style specified by the error icon... */8} 9. info-icon {@ extend % icon; 10/* The style specified by the information icon... */11}
6. @ import introduce partial style syntax
SASS extends the @ import function,
SASS allows us to introduce multiple files in a CSS file and compile them into one file;
For some reason, we can put a large css file,
It can be divided into multiple partials (partials must begin with ie: _ header. scss ),
We only need to add
1 @import "header"
At this time, the underline and extension names of header. scss do not need to be added! @ Import "header ";
7. Interpolation inserts a value into another value.
# {$ Variable name}: string used to reference the definition
1 $version:"0.0.1";2 /* hello test comments #{$version} */3 $nns : "txt";4 .nav-#{$nns} {5 background-size: cover;6 }
8. SASS control language name
1 $theme :"ddd"; 2 .testif { 3 @if($theme == light) { 4 background-color: lightblue; 5 } @else if($theme == red){ 6 background-color: red; 7 } @else { 8 background-color: black; 9 }10 }11 @for $i from 1 through 5 {12 .col-#{$i} {13 width:10% *$i;14 }15 }16 @each $var in <list/map>17 $list: zhangsan lisi wangwu; $map:(red:#dddd) -> map-get($map,key)18 @each $name in $list {19 .photo-#{$name}{20 background: url("#{$name}.jpg");21 }22 }
9. SASS UDF
1 $ ccs :( light: # ffffff, dark: #000000); // define function 2 @ function getColor ($ key) {3 @ return map_get ($ ccs, $ key ); 4} 5 @ mixin aa () {// call @ function 6 color: getColor (light); 7} 8 // use mixin 9. testff {10 @ include aa () 11}
Please leave a message to inform me of your comments !!
(To be continued ~~)