CSS预处理——LESS
LESS是什么?
less是一門CSS預(yù)處理語言。由于CSS本身并不是程序式語言,不方便維護(hù)和擴(kuò)展,沒有變量、函數(shù)、作用域等概念。而LESS在CSS的基礎(chǔ)語法之上,引入了變量、Mixin混入、運(yùn)算以及函數(shù)等功能,大大簡(jiǎn)化了CSS的編寫,降低了CSS的維護(hù)成本。
本質(zhì)上,LESS包含一套自定義的語法及一個(gè)解析器,寫好的LESS文件會(huì)通過解析器編譯生成CSS文件。LESS并沒有建材CSS原有的特性,更不是用來取代CSS的,而是在現(xiàn)有的語法基礎(chǔ)上,為CSS加入程序式語言的功能特性。所以,標(biāo)準(zhǔn)的CSS文件直接改成.less格式,LESS編譯器可以完全識(shí)別。
使用
1.標(biāo)簽引入。
link標(biāo)簽rel屬性值一定要為stylesheet/less
<link rel="stylesheet/less" href="style.less"> <script src="less.min.js"></script>2.命令行使用npm安裝
npm install -g less 編譯: lessc style.less style.css 編譯并壓縮文件: lessc --clean-css styles.less styles.min.css3.前端服務(wù)熱調(diào)試(webpack)
需要配置webpack,webpack會(huì)幫你編譯less文件,可以在js中Import進(jìn).less文件,像使用css那樣使用less
4.在js中調(diào)用編譯器調(diào)試
var less = require('less'); less.render('.myclass { width: (1 + 1) }', function (e, output) {console.log(output.css); // 輸出 .myclass {width:2} });語法規(guī)則
變量
這里的變量指的是常量,只能定義一次。以@開頭定義,使用時(shí)也要鍵入@
1.值變量
@color: #5B83AD; @bgColor: @color + #111; @width: 300px; #wrap {color: @color;width: @width-20;height: @width-20*5;margin: (@width-20)*5;background: @bgColor; }生成后的CSS:
#wrap {color: #999;width: 50%;background: #6c94be; }2.選擇器變量
讓選擇器變成動(dòng)態(tài)的變量,使用時(shí)必須將變量名用花括號(hào)括起來
@mySelector: #wrap; @Wrap: wrap; @{mySelector}{ //變量名 必須使用大括號(hào)包裹c(diǎn)olor: #999;width: 50%; } .@{Wrap}{color:#ccc; } #@{Wrap}{color:#666; }生成的CSS:
#wrap{color: #999;width: 50%; } .wrap{color:#ccc; } #wrap{color:#666; }3.屬性變量
將某CSS屬性名當(dāng)做變量,與選擇器變量一樣,使用時(shí)需要{}
@borderStyle: border-style; @Solid: solid; #wrap{@{borderStyle}: @Solid;//變量名 必須使用大括號(hào)包裹 }編譯后:
#wrap{border-style:solid; }4.url變量
@images: "../img"; //需要加引號(hào) body {background: url("@{images}/dog.png"); //變量名 必須使用大括號(hào)包裹 }編譯后:
body {background: url("../img/dog.png"); }5.聲明規(guī)則集(作用類似于混入)
聲明:@name:{屬性:值};
使用:@name();
編譯后:
#main{background:red; } #con{width: 200px;height: 200px;border: solid 1px red; }6.變量的作用域
就近原則,與大多數(shù)編程語言的原則一樣
@var: @a; @a: 100%; #wrap {width: @var;@a: 9%; }編譯后:
#wrap {width: 9%; }嵌套
&符:代表父選擇器的名字
.boring {display: inline-block;&::after{content:"Hello Less";}.class {width: 100%;}&>.text{height: 10px;} }編譯后:
.boring{display: inline-block; } .boring::after{content: "Hello Less"; } .boring .class{width: 100%; } .boring>.text{height: 10px; }混入(Mixin)
?混入就是將一組CSS屬性整體包含入另一個(gè)css類中去。提高復(fù)用性
1.無參數(shù)混入
.bordered {&:hover {color: red;}border-top: dotted 1px black;border-bottom: solid 2px black; } .card {background: #f6f6f6;.bordered; // .bordered()兩種寫法等價(jià) }編譯后:
.bordered {border-top: dotted 1px black;border-bottom: solid 2px black; } .bordered:hover {color: red; } .card {background: #f6f6f6;border-top: dotted 1px black;border-bottom: solid 2px black; } .card:hover {color: red; }2.不輸出Mixin
如果只是想創(chuàng)建一個(gè)mixin,并不想單獨(dú)輸出,可以這樣用
.bordered(){&:hover {color: red;}border-top: dotted 1px black;border-bottom: solid 2px black; } .card {background: #f6f6f6;.bordered; // .bordered()兩種寫法等價(jià) }編譯后:
.card {background: #f6f6f6;border-top: dotted 1px black;border-bottom: solid 2px black; } .card:hover {color: red; }3.有參數(shù)混入
注意的點(diǎn):① 默認(rèn)參數(shù)? ② 命名參數(shù)? ③ @arguments? ④ @rest
.border-radius(@radius) {border-radius: @radius; } .border(@a:10px, @b:50px, @c:30px, @color:#000) {border:solid 1px @color;box-shadow: @arguments; // 指代的是 全部參數(shù) } .bgColor(@color,@rest...){width: @rest; // @rest參數(shù)可以獲得后面違背獲取的值。background: @color; } #main {.border(0px,5px,30px,red);.border-radius(5px); } #main1 {.border(@c: 20px,@color: red); } #main2 {.border(10px); } #main3 {.border; }4.匹配模式
和面向?qū)ο蟮亩鄳B(tài)很相似。當(dāng)調(diào)用傳參的時(shí)候,會(huì)根據(jù)參數(shù)進(jìn)行匹配,找到匹配程度更高的,如果匹配程度相同,將全部選擇,并存在樣式覆蓋。
.mixin(@color) {color-1: @color; } .mixin(@color; @padding: 2) {color-2: @color;padding-2: @padding; } .mixin(@color; @padding; @margin: 2) {color-3: @color;padding-3: @padding;margin: @margin @margin @margin @margin; } div {.mixin(#008000); }編譯后:
div {color-1: #008000; // "color-1"這種寫法對(duì)CSS來說是不合法的,在這里只是展示哪些屬性被用上了color-2: #008000;padding-2: 2; }Demo2:
/* Less */ .triangle(top,@width:20px,@color:#000){border-color:transparent transparent @color transparent ; } .triangle(right,@width:20px,@color:#000){border-color:transparent @color transparent transparent ; }.triangle(bottom,@width:20px,@color:#000){border-color:@color transparent transparent transparent ; } .triangle(left,@width:20px,@color:#000){border-color:transparent transparent transparent @color; } .triangle(@_,@width:20px,@color:#000){ // @_的作用是,無論選擇的是哪個(gè)函數(shù),一定運(yùn)行此函數(shù)border-style: solid;border-width: @width; } #main{.triangle(left, 50px, #999) } /* 生成的 CSS */ #main{border-color:transparent transparent transparent #999;border-style: solid;border-width: 50px; }5.方法的命名空間
不能單獨(dú)使用命名空間的方法,必須先引入命名空間,才能使用其中的方法。
#card(){background: #723232;.d(@w:300px){width: @w;#a(@h:300px){height: @h;//可以使用上一層傳進(jìn)來的方法width: @w;}} } #wrap{#card > .d > #a(100px); // 父元素不能加 括號(hào) } #main{#card .d(); } #con{//不得單獨(dú)使用命名空間的方法//.d() 如果前面沒有引入命名空間 #card ,將會(huì)報(bào)錯(cuò)#card; // 等價(jià)于 #card();.d(20px); //必須先引入 #card }編譯后:
#wrap{height:100px;width:300px; } #main{width:300px; } #con{width:20px; }6.條件篩選
沒有邏輯運(yùn)算符與或非,但是有when not ,(逗號(hào))分別相當(dāng)于 && ! ||
比較運(yùn)算符有:?> >= = =< <。=代表的是等于。
#card{// and 運(yùn)算符 ,相當(dāng)于 與運(yùn)算 &&,必須條件全部符合才會(huì)執(zhí)行.border(@width,@color,@style) when (@width>100px) and(@color=#999){border:@style @color @width;}// not 運(yùn)算符,相當(dāng)于 非運(yùn)算 !,條件為 不符合才會(huì)執(zhí)行.background(@color) when not (@color>=#222){background:@color;}// , 逗號(hào)分隔符:相當(dāng)于 或運(yùn)算 ||,只要有一個(gè)符合條件就會(huì)執(zhí)行.font(@size:20px) when (@size>50px) , (@size<100px){font-size: @size;} } #main{#card>.border(200px,#999,solid);#card .background(#111);#card > .font(40px); }編譯后:
#main{border:solid #999 200px;background:#111;font-size:40px; }7.使用!important
.border{border: solid 1px red;margin: 50px; } #main{.border() !important; }編譯后:
#main {border: solid 1px red !important;margin: 50px !important; }8.循環(huán)
less并沒有提供for循環(huán)功能,但是可以借助when實(shí)現(xiàn)。
.loop(@counter) when (@counter > 0) {.loop((@counter - 1)); // next iterationwidth: (10px * @counter); // code for each iteration } div {.loop(5); // launch the loop }編譯后:
div {width: 10px;width: 20px;width: 30px;width: 40px;width: 50px; }使用循環(huán)可以做成CSS網(wǎng)格類的示例:
.generate-columns(4);.generate-columns(@n, @i: 1) when (@i =< @n) {.column-@{i} {width: (@i * 100% / @n);}.generate-columns(@n, (@i + 1)); }編譯后:
.column-1 {width: 25%; } .column-2 {width: 50%; } .column-3 {width: 75%; } .column-4 {width: 100%; }9.屬性拼接
CSS中有的屬性值是空格隔開,有的是逗號(hào)隔開。
①逗號(hào)隔開,在LESS中使用 +?
.boxShadow() {box-shadow+: inset 0 0 10px #555; } .main {.boxShadow();box-shadow+: 0 0 20px black; } /* 生成后的 CSS */ .main {box-shadow: inset 0 0 10px #555, 0 0 20px black; }②空格隔開,在LESS中使用 +_?
/* Less */ .Animation() {transform+_: scale(2); } .main {.Animation();transform+_: rotate(15deg); }/* 生成的 CSS */ .main {transform: scale(2) rotate(15deg); }繼承
.animation{transition: all .3s ease-out;.hide{transform:scale(0);} } #main{&:extend(.animation); } #con{&:extend(.animation .hide); }?編譯后:
.animation,#main{transition: all .3s ease-out; } .animation .hide , #con{transform:scale(0); }all全局搜索替換
#main{width: 200px; } #main {&:after {content:"Less is good!";} } #wrap:extend(#main all) {}編譯后:
#main,#wrap{width: 200px; } #main:after, #wrap:after {content: "Less is good!"; }函數(shù)
1.類型函數(shù)
- isnumber 判定是否是數(shù)字
- iscolor ? 判斷值是否是顏色
- isurl? ? ? ? ?判斷是否是url
2.顏色操作
- saturate? ? 增加一定數(shù)值的顏色飽和度
- lighteen? ? 增加一定數(shù)值的顏色亮度
- darken? ? ? 降低顏色亮度
- fade? ? ? ? ? 給顏色設(shè)置一定數(shù)值的透明度
3.數(shù)學(xué)函數(shù)
- ceil? ? ? ? ? ?向上取整
- floor? ? ? ? ?向下取整
- round? ? ? ?四舍五入
- percentage? 將浮點(diǎn)數(shù)值轉(zhuǎn)換為百分比字符串
- abs? ? ? ? ? ?計(jì)算數(shù)值的絕對(duì)值
- pow? ? ? ? ? 計(jì)算一個(gè)數(shù)的乘方
更多函數(shù)參見官方文檔:
http://lesscss.cn/functions/
其他
1.注釋
/* CSS注釋,會(huì)出現(xiàn)在CSS文件中 */
// LESS注釋,不會(huì)被編譯在CSS文件中?
2.避免編譯
在CSS屬性被引號(hào)包起來,且前面加上~符號(hào)
/* Less */ #main{width:~'calc(300px-30px)'; } /* 生成后的 CSS */ #main{width:calc(300px-30px); }3.使用JS
/* Less */ @content:`"aaa".toUpperCase()`; #randomColor{@randomColor: ~"rgb(`Math.round(Math.random() * 256)`,`Math.round(Math.random() * 256)`,`Math.round(Math.random() * 256)`)"; } #wrap{width: ~"`Math.round(Math.random() * 100)`px";&:after{content:@content;}height: ~"`window.innerHeight`px";alert:~"`alert(1)`";#randomColor();background-color: @randomColor; } /* 生成后的 CSS */ #wrap{width: 隨機(jī)值(0~100)px;height: 743px;//由電腦而異background: 隨機(jī)顏色; } #wrap::after{content:"AAA"; }此乃JS in CSS
轉(zhuǎn)載于:https://www.cnblogs.com/V587Chinese/p/11419587.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的CSS预处理——LESS的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 下载电驴资源
- 下一篇: html相对定位向上偏移,使用CSS的相