css 预处理工具,能够将其对应的DSL(领域特定语言)编译为 css
compass 将 sass 编译为 css
)如今可用 node-sass
来编译 sass/scss
文件css
node-sass
时,会去GitHub 下载一个 .node
的文件而这个文件托管在墙外的服务器上,因此失败了 node-sass安装失败解决方案 .sass
或 .scss
为文件后缀名称(如今通常都是用 scss)less.js
在浏览器运行时中解析 less 代码npm install -g less &lessc styles.less styles.css
(能够加参数控制编译后的css排版及压缩).less
为文件后缀名称npm install stylus -g & stylus src/
(能够加参数控制编译后的css排版及压缩).styl
为文件后缀安装 编译器、对应 loader
,而后再 module.rules 配置其具体规则
变量符 @ $ 无变量符号直接变量名
前端
less @size: 10px; .box { width: @size; } scss $red: #c00; strong { color: $red; } stylus red = #c00 strong color: red css 的变量规范 /* global scope */ :root { --red: #c00; } strong { color: var(--red); }
* 变量做用域:less 惰性加载,sass,stylus 就近加载
嵌套语法一致,用 & 引用父集
-- 嵌套不建议超过 4 层node
* less 不支持跳出嵌套 * sass `@at-root` ``` @at-root 支持参数,跳出不一样的嵌套 without: all,表示全部 without: rule,表示常规css,rule是默认值 without: media,表示media without: support,@support如今使用还不普遍 // child1 将跳出 parent 的嵌套 .parent1{ color:#f00; @at-root .child1 { width:200px; } } } ```
插值webpack
less @prefix: ui; .@{prefix}-button { color: #333; } sass $prefix: ui .#{$prefix}-button { color: #333; } stylus prefix = ui .{prefix}-button color #333
混入(mixin):预处理器最精髓的功能,样式层面上的抽象(至关于copy代码片断)c++
@mixin
,使用时 @include
if else for each while
, less 使用 mixin when 处理
PostCSS经常使用插件 (用这些插件集合其实已经能够代替 三大 css 预处理器)git
目前 Postcss 在通常项目中的用途github
autoprefixer
插件,为css 属性增长前缀postcss.config.js
// webpack.config.js { test: /\.less$/, loader: ExtractTextWebpackPlugin.extract([ {loader: 'css-loader', options: { minimize: true }}, 'postcss-loader', // 要在预处理器处理完以后,在使用 postcss-loader 'less-loader', ]), } // postcss.config.js module.exports = { plugins: [ require('autoprefixer')({ 'browsers': ['> 1%', 'last 2 versions'] }) ] }