-
Variables - define commonly used variables -
Calculation - Calculation of realized value -
Mixed - code reuse or inheritance -
Functions - powerful functions to manipulate properties and values
Install Less
npm install less -g
yarn add less -g
lessc styles.less > styles.css
Combined with webpack2
yarn add less less-loader css-loader style-loader --dev
Style Tag Inline
module.exports = { module: { rules: [ { test: /\. (less|css)$/, use:[ 'style-loader','css-loader','less-loader'], }, ], }, };
Style tag outreach
yarn add extract-text-webpack-plugin --dev
const ExtractTextPlugin = require('extract-text-webpack-plugin'); module.exports = { module: { rules: [ { test: /\. (less|css)$/, use: ExtractTextPlugin.extract({ use:[ 'css-loader','less-loader'], fallback: 'style-loader', }), }, ], }, plugins: [ new ExtractTextPlugin({ filename: 'index.css', disable: false, allChunks: true, }), ], };
Support CSS Modules
module: { rules: [ { test: /\. (less|css)$/, use: ExtractTextPlugin.extract({ use:[ { loader: 'css-loader', options:{ modules:true, importLoaders:1, localIdentName:'[local]_[hash:base64:5]', } }, { loader:'less-loader', }, ], fallback: 'style-loader', }), }, ], },
Less use learning
Variables
@background-color: #eee; @text-color: #333; p{ background-color: @background-color; color: @text-color; padding: 15px; }
p{ background-color: #eee; color: #333; padding: 15px; }
Mixins
#circle{ background-color: #4CAF50; border-radius: 100%; } #small-circle{ width: 50px; height: 50px; #circle }
#circle { background-color: #4CAF50; border-radius: 100%; } #small-circle { width: 50px; height: 50px; background-color: #4CAF50; border-radius: 100%; }
#circle(){ background-color: #4CAF50; border-radius: 100%; } #small-circle{ width: 50px; height: 50px; #circle }
#small-circle { width: 50px; height: 50px; background-color: #4CAF50; border-radius: 100%; }
Nesting and Scope
ul{ background-color: #03A9F4; padding: 10px; list-style: none; li{ background-color: #fff; border-radius: 3px; margin: 10px 0; } }
ul { background-color: #03A9F4; padding: 10px; list-style: none; } ul li { background-color: #fff; border-radius: 3px; margin: 10px 0; }
@text-color: #000000; ul{ @text-color: #fff; background-color: #03A9F4; padding: 10px; list-style: none; li{ color: @text-color; border-radius: 3px; margin: 10px 0; } }
ul { background-color: #03A9F4; padding: 10px; list-style: none; } ul li { color: #fff; border-radius: 3px; margin: 10px 0; }
Operations
@div-width: 100px; @color: #03A9F4; #left{ width: @div-width; background-color: @color - 100; } #right{ width: @div-width * 2; background-color: @color; }
#left { width: 100px; background-color: #004590; } #right { width: 200px; background-color: #03a9f4; }
Functions
.img{ width:image-width("../img/less-logo.png"); height:image-height("../img/less-logo.png"); margin:image-size("../img/less-logo.png"); }
.img { width: 300px; height: 100px; margin: 300px 100px; }
@list: red, blue, green, yellow; .list{ color:extract(@list, 1); border-color:extract(@list, 2); background-volor:extract(@list, 3); }
.list { color: red; border-color: blue; background-volor: green; }
Others
.a{ &:hover{ color:red; } } .p, .a, .ul, .li { border-top: 2px dotted #366; && { border-top: 0; } & + & { border-top: 0; } }
.a:hover{ color:red; } .p, .a, .ul, .li { border-top: 2px dotted #366; } .p.p, .p.a, .p.ul, .p.li, .a.p, .a.a, .a.ul, .a.li, .ul.p, .ul.a, .ul.ul, .ul.li, .li.p, .li.a, .li.ul, .li.li { border-top: 0; } .p + .p, .p + .a, .p + .ul, .p + .li, .a + .p, .a + .a, .a + .ul, .a + .li, .ul + .p, .ul + .a, .ul + .ul, .ul + .li, .li + .p, .li + .a, .li + .ul, .li + .li { border-top: 0; }
@background-color: #eee; @text-color: #333; p{ background-color: @background-color; color: @text-color; padding: 15px; }
@import "other.less";
p{ background-color: #eee; color: #333; padding: 15px; }
epilogue
reference resources
-
webpack.js.org/loaders/css-loader -
webpack.js.org/loaders/style-loader -
webpack.js.org/loaders/less-loader -
lesscss.org