一量没、 Css預(yù)編譯
參考網(wǎng)址:https://segmentfault.com/a/1190000012360995?utm_source=tag-newest
常見(jiàn)的css預(yù)編譯器有三種:less 、 sass 隙袁、 stylus绒障。
Bootstrop使用的是less。
二竖哩、 Less
Less是一門(mén)css預(yù)處理語(yǔ)言糖赔,他擴(kuò)展了css語(yǔ)言,增加了變量、Mixin欢瞪、函數(shù)等特性活烙,使css更容易維護(hù)和擴(kuò)展。他不是一個(gè)直接使用的語(yǔ)言遣鼓,而是一個(gè)生成css的語(yǔ)言啸盏。Less可以運(yùn)行在Node或?yàn)g覽器端。
Css計(jì)算:cacl()
三骑祟、 使用方法
1.Node環(huán)境的使用方法
2.瀏覽器環(huán)境
1) 引入less文件回懦,類(lèi)似于css文件,單類(lèi)型不一樣次企。
<Link rel=”stylesheet/less” type=”text/css” href=”style.less”/>
2) 引入解析less的less.js
<script src=”less.js” type =”text/javascript”></script>
<script src="cdnjs.cloudflare.com/ajax/libs/less.js/3.8.1/less.min.js" ></script>
下載地址:https://raw.github.com/less/less.js/v2.5.3/dist/less.min.js
四怯晕、 快速起步
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet/less" href="1.less">
<!-- <script src="less.js"></script> -->
<script src="cdnjs.cloudflare.com/ajax/libs/less.js/3.8.1/less.min.js" ></script>
</head>
<body>
<div></div>
</body>
</html>
1.變量
1) 普通變量
less 以@開(kāi)頭定義變量,并且使用時(shí)直接鍵入@名稱(chēng)缸棵。注意:作為屬性值的變量不能添加大括號(hào)("{}")
@color:#333;
Div{
Background:@color舟茶;
}
Less其變量只能定義一次,不能重復(fù)定義堵第,否則后面的會(huì)類(lèi)似與js的變量提升吧凉,覆蓋前面的變量。
使用變量設(shè)置css踏志,也方便代碼的維護(hù)阀捅。
2) 選擇器變量
選擇器變量
讓選擇器變成一個(gè)動(dòng)態(tài)的。
作為選擇器的變量在使用的時(shí)候需要添加大括號(hào)("{}")针余,變量的前面可以添加選擇操作符饲鄙。
/* Less */
@mySelector: #wrap;
@Wrap: wrap;
@{mySelector}{ //變量名 必須使用大括號(hào)包裹
color: #999;
width: 50%;
}
.@{Wrap}{
color:#ccc;
}
#@{Wrap}{
color:#666;
}
/* 生成的 CSS */
#wrap{
color: #999;
width: 50%;
}
.wrap{
color:#ccc;
}
#wrap{
color:#666;
}
3) 屬性變量
可以讓我在書(shū)寫(xiě)的時(shí)候少寫(xiě)很多內(nèi)容,屬性變量使用的時(shí)候也需要添加大括號(hào)("{}")圆雁。
/* Less */
@borderStyle: border-style;
@Soild:solid;
#wrap{
@{borderStyle}: @Soild;//變量名 必須使用大括號(hào)包裹
}
/* 生成的 CSS */
#wrap{
border-style:solid;
}
4) Url變量
項(xiàng)目結(jié)構(gòu)改變時(shí)忍级,可以方便我們修改,目的是為了方便項(xiàng)目的維護(hù)伪朽。
/* Less */
@images: "../img";//需要加引號(hào)
body {
background: url("@{images}/dog.png");//變量名 必須使用大括號(hào)包裹
}
/* 生成的 CSS */
body {
background: url("../img/dog.png");
}
5) 申明變量
格式:
定義:@name:{
Key:value轴咱;
Key:value;
}
使用:@name()驱负;
案例:
/* Less */
@background: {background:red;};
#main{
@background();
}
@Rules:{
width: 200px;
height: 200px;
border: solid 1px red;
};
#con{
@Rules();
}
/* 生成的 CSS */
#main{
background:red;
}
#con{
width: 200px;
height: 200px;
border: solid 1px red;
}
6) 變量運(yùn)算
規(guī)則:
加減法時(shí),以第一個(gè)數(shù)據(jù)的單位為基準(zhǔn)患雇。
乘除法時(shí)跃脊,注意單位一定要統(tǒng)一。
案例:
/* Less */
@width:300px;
@color:#222;
#wrap{
width:@width-20;
height:@width-20*5;
margin:(@width-20)*5;
color:@color*2;
background-color:@color + #111;
}
/* 生成的 CSS */
#wrap{
width:280px;
height:200px;
margin:1400px;
color:#444;
background-color:#333;
}
7) 變量的作用域
就近原則苛吱,不是指代碼的位置酪术,而是指代碼的層級(jí)結(jié)構(gòu)。
如果是同一級(jí)后面的生效,類(lèi)似于提升绘雁。
不同級(jí)的變量橡疼,距離最近的生效。距離是指定義時(shí)的位置庐舟。
8) 用變量的定義變量
解析的順序是從后向前主城解析欣除。
/* Less */
@fnord: "I am fnord.";
@var: "fnord";
#wrap::after{
content: @@var; //將@var替換為其值 content:@fnord;
}
/* 生成的 CSS */
#wrap::after{
content: "I am fnord.";
}
2.嵌套
1) &符號(hào),表示的是上1級(jí)選擇器的名字挪略。
/* Less */
#header{
&:after{ //注意:不能省略&历帚,如果省略會(huì)給每一個(gè)子元素添加一個(gè)after。
content:"Less is more!";
}
.title{
font-weight:bold;
}
&_content{//理解方式:直接把 & 替換成 #header
margin:20px;
}
}
/* 生成的 CSS */
#header::after{
content:"Less is more!";
}
#header .title{ //嵌套了
font-weight:bold;
}
#header_content{//沒(méi)有嵌套杠娱!
margin:20px;
}
2) 嵌套覆蓋原有樣式
~~
div{ width:200px; height:200px; background:red; .show{ display: none; } } .show{ display: block; //只有在div使用的時(shí)候被覆蓋 }
~~
3.媒體查詢(xún)
之前媒體查詢(xún)的格式是:先分屏然后在每一個(gè)媒體查詢(xún)中設(shè)置樣式挽牢。
@media only screen and {max-width:1200px;}{
Div{}
}
@media only screen and {min-width:992px;}{
Div{}
}
@media only screen and {min-width:768px}{
Div{}
}
less提供給我們更加方便的方式:
/* Less */
#main{
//something...
@media screen{
@media (max-width:768px){
width:100px;
}
}
@media tv {
width:2000px;
}
}
/* 生成的 CSS */
@media screen and (max-width:768px){
#main{
width:100px;
}
}
@media tv{
#main{
width:2000px;
}
}
- 唯一缺點(diǎn):每一個(gè)元素都會(huì)編譯出自己@media聲明摊求,并不會(huì)合并
4.方法
1) 無(wú)參數(shù)方法 方法如聲明的集合禽拔,使用值直接鍵入名稱(chēng)即可。
要點(diǎn):“.”與“#”都可以作為方法前綴室叉《闷埽可以不適用小括號(hào),但是為了避免css格式混淆太惠,建議加上小括號(hào)“()”磨淌。
.card(){
//something....
}
#wrap{
.card();
}
2) 具體參數(shù)方法
Less可以傳遞參數(shù)
width:200px;
height:200px;
.setSize(@width_size,@height_size){
width:@width_size;
height:@height_size;
}
形成css:
div{
.setSize(200px,300px);
}
3) 默認(rèn)參數(shù)方法
Less 可以使用默認(rèn)參數(shù),如果沒(méi)有傳參數(shù)凿渊,那么將使用默認(rèn)參數(shù)梁只。
@arguments 猶如 JS 中的 arguments 指代的是全部參數(shù)。 傳的參數(shù)中 必須帶著單位埃脏。
/* Less */
.border(@a:10px,@b:50px,@c:30px,@color:#000){
border:solid 1px @color;
box-shadow: @arguments;//指代的是 全部參數(shù)
}
#main{
.border(0px,5px,30px,red);//必須帶著單位
}
#wrap{
.border(0px);
}
#content{
.border;//等價(jià)于 .border()
}
/* 生成的 CSS */
#main{
border:solid 1px red;
box-shadow:0px,5px,30px,red;
}
#wrap{
border:solid 1px #000;
box-shadow: 0px 50px 30px #000;
}
#content{
border:solid 1px #000;
box-shadow: 10px 50px 30px #000;
}
4) 不定參
不確定參數(shù)的個(gè)數(shù)
案例:
/* Less */
.boxShadow(...){
box-shadow: @arguments;
}
.textShadow(@a,...){
text-shadow: @arguments;
}
#main{
.boxShadow(1px,4px,30px,red);
.textShadow(1px,4px,30px,red);
}
/* 生成后的 CSS */
#main{
box-shadow: 1px 4px 30px red;
text-shadow: 1px 4px 30px red;
}
5) 方法的匹配模式
類(lèi)似于多態(tài)搪锣。有點(diǎn)類(lèi)似于switch case
同一個(gè)方法名的多個(gè)方法,由于傳入的參數(shù)不同而實(shí)現(xiàn)不同的功能彩掐。
案例:
/* 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){
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;
}
6) 方法的命名空間
有一個(gè)嵌套關(guān)系
/* Less */
#card(){
background: #723232;
.d(@w:300px){
width: @w;
#a(@h:300px){
height: @h;//可以使用上一層傳進(jìn)來(lái)的方法
width: @w;
}
}
}
#wrap{
#card > .d > #a(100px); // 父元素不能加 括號(hào)
}
#main{
#card .d();
}
#con{
//不得單獨(dú)使用命名空間的方法
//.d() 如果前面沒(méi)有引入命名空間 #card 构舟,將會(huì)報(bào)錯(cuò)
#card; // 等價(jià)于 #card();
.d(20px); //必須先引入 #card
}
/* 生成的 CSS */
#wrap{
height:100px;
width:300px;
}
#main{
width:300px;
}
#con{
width:20px;
}
案例2
.div{
background:red;
.size(@width){
width:@width;
height:200px;
border:1px solid #ccc;
.a(@height){
height:@height;
width:@width;
}
}
}
a{
display: block;
.div>.size(200px);
.a(200px);
}
在 CSS 中>
選擇器,選擇的是兒子元素堵幽,就是必須與父元素有直接血源的元素狗超。 - 在引入命令空間時(shí),如使用 >
選擇器朴下,父元素不能加括號(hào)努咐。 - 不得單獨(dú)使用命名空間的方法必須先引入命名空間,才能使用其中方法殴胧。 - 子方法可以使用上一層傳進(jìn)來(lái)的方法
7) 條件語(yǔ)句
Less沒(méi)有if / else 但是less具有一個(gè)when渗稍,and佩迟,not與“,”語(yǔ)法竿屹。
/* Less */
#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);
}
/* 生成后的 CSS */
#main{
border:solid #999 200px;
background:#111;
font-size:40px;
}
8) 條件運(yùn)算符
比較運(yùn)算有:> >= = =< <
=代表是等于
除去關(guān)鍵字true以外的值其他都會(huì)被默認(rèn)為fales
9) 循環(huán)語(yǔ)法
Less并沒(méi)有提供一個(gè)for等循環(huán)的方法但是可以使用遞歸的方法實(shí)現(xiàn)扼雏。
案例:
/* Less */
.generate-columns(4);
.generate-columns(@n, @i: 1) when (@i =< @n) {
.column-@{i} {
width: (@i * 100% / @n);
}
.generate-columns(@n, (@i + 1));
}
/* 生成后的 CSS */
.column-1 {
width: 25%;
}
.column-2 {
width: 50%;
}
.column-3 {
width: 75%;
}
.column-4 {
width: 100%;
}
10) 方法中的important
方法使用了important坚嗜,相當(dāng)于這個(gè)方法中的每一個(gè)屬性都設(shè)置了一遍important,不允許覆蓋诗充。
/* Less */
.border{
border: solid 1px red;
margin: 50px;
}
#main{
.border() !important;
}
/* 生成后的 CSS */
#main {
border: solid 1px red !important;
margin: 50px !important;
}
5.Less注釋
Less的注釋語(yǔ)法類(lèi)似于js
//單行注釋
/* ---*/多行注釋
6.屬性的拼接語(yǔ)法
+代表的是逗號(hào)苍蔬,+_代表的是空格。
1) 空格
案例:
/* Less */
.Animation() {
transform+_: scale(2);
}
.main {
.Animation();
transform+_: rotate(15deg);
}
/* 生成的 CSS */
.main {
transform: scale(2) rotate(15deg);
}
2) 逗號(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;
}
7.繼承
Exterd是less的一個(gè)偽類(lèi)蝴蜓。它可以繼承所匹配聲明中的全部樣式碟绑。
案例:
/* Less */
.animation{
transition: all .3s ease-out;
.hide{
transform:scale(0);
}
}
#main{
&:extend(.animation);
}
#con{
&:extend(.animation .hide);
}
/* 生成后的 CSS */
.animation,#main{
transition: all .3s ease-out;
}
.animation .hide , #con{
transform:scale(0);
}
1)All全局搜索替換
用以匹配方法匹配到的所有的聲明。
案例:
/* Less */
#main{
width: 200px;
}
#main {
&:after {
content:"Less is good!";
}
}
#wrap:extend(#main all) {}
/* 生成的 CSS */
#main,#wrap{
width: 200px;
}
#main:after, #wrap:after {
content: "Less is good!";
}
2)擴(kuò)展的注意事項(xiàng)
選擇器和擴(kuò)展之間 是允許有空格的:pre:hover :extend(div pre).
可以有多個(gè)擴(kuò)展: pre:hover:extend(div pre):extend(.bucket tr) - 注意這與 pre:hover:extend(div pre, .bucket tr)一樣茎匠。
擴(kuò)展必須在最后 : pre:hover:extend(div pre).nth-child(odd)格仲。
如果一個(gè)規(guī)則集包含多個(gè)選擇器,所有選擇器都可以使用extend關(guān)鍵字诵冒。
8.導(dǎo)入
在less文件中可以引入其他的less文件凯肋。使用關(guān)鍵字import。
1)導(dǎo)入less文件可以省略后綴
Import ”index.less”;
Import “index”
2)@import可以放置在任何地方汽馋。
案例:
mian{
Font-size:15px;
}
@import ”7.less”
3)Reference
Less中最強(qiáng)的特性
引入一個(gè)less文件但是不會(huì)編譯它侮东。
@import(reference)“bootstrap.Less”;
#wrap:extend(.navbar all){}
使用@import(reference)導(dǎo)入外部文件豹芯,只能在less中使用悄雅,注意是為了繼承和方法,最終編譯的時(shí)候里面的內(nèi)容不會(huì)產(chǎn)生less
4)Once
@import語(yǔ)句的默認(rèn)行為铁蹈。這表明相同的文件只會(huì)被導(dǎo)入一次宽闲,而隨后的導(dǎo)入文件在的重要代碼都不會(huì)解拆。
@import (once) "foo.less";@import (once) "foo.less"; // this statement will be ignored
5)Multiple
使用@import (multiple)允許導(dǎo)入多個(gè)同名文件握牧。
案例:
* Less */
// file: foo.less
.a {
color: green;
}
// file: main.less
@import (multiple) "foo.less";
@import (multiple) "foo.less";
/* 生成后的 CSS */
.a {
color: green;
}
.a {
color: green;
}
9.避免編譯
基本語(yǔ)法:
~’值
案例
/* Less */
#main{
width:~'calc(300px-30px)';
}
/* 生成后的 CSS */
#main{
width:calc(300px-30px);
}
10.less中可以使用js
Less本身是使用較少實(shí)現(xiàn)的容诬,所以在less中就可以使用js。
Js的代碼寫(xiě)在字符串模板里 ‘ 這里 ’沿腰。
案例:
/* 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 */
// 彈出 1
#wrap{
width: 隨機(jī)值(0~100)px;
height: 743px;//由電腦而異
background: 隨機(jī)顏色;
}
#wrap::after{
content:"AAA";
}
五览徒、less的函數(shù)
1.判斷類(lèi)型
Isnumber():判斷是否為數(shù)字
Iscolor():判斷是否為顏色
Isurl():判斷是否為路徑
2.顏色操作
Saturate > 增加一定數(shù)值的顏色飽和度。
Lighten > 增加一定數(shù)值的顏色亮度矫俺。
Darken > 降低一定數(shù)值的顏色亮度吱殉。
Fade > 給顏色設(shè)定一定數(shù)值的透明度。
Mix > 根據(jù)比例混合兩種顏色厘托。
3.?dāng)?shù)字函數(shù)
Ceil > 向上取整友雳。
Floor > 向下取整。
Percentage > 將浮點(diǎn)數(shù)轉(zhuǎn)換為百分比字符串铅匹。
Round > 四舍五入押赊。
Sqrt > 計(jì)算一個(gè)數(shù)的平方根。
Abs > 計(jì)算數(shù)字的絕對(duì)值包斑,原樣保持單位流礁。
Pow > 計(jì)算一個(gè)數(shù)的乘方。
Less函數(shù)參考網(wǎng)址參考http://lesscss.cn/functions/
Less函數(shù)參考手冊(cè)網(wǎng)址:https://www.w3cschool.cn/less/namespaces_accessors.html