女友要我教她CSS,因而我就折騰了一週,終於完成了這篇長文... css
而後,而後?而後當我發佈這篇文章的時候,她會感動到哭嗎?html
head
部分body
部分a{}
::before{}
.link{}
[type=radio]{}
:hover{}
#id{}
[type=checkbox] + label{}
:not(.link){}
*{}
例子:計算一個不進位的數字前端
#id.link a[href]
————————————————
計算過程:
#id +100
.link +10
a +1
[href] +0
結果:111
複製代碼
#id .link.active
————————————————
計算過程:
#id +100
.link +10
.active +10
結果:120
複製代碼
那麼,咱們怎麼理解 不進位的數字 這關鍵字眼呢?用一個簡單例子吧:node
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS選擇器</title>
<style>
#test1{
color: red;
}
#test1.test1{
color: blue;
}
.test2{
color: red;
}
div.test2{
color: blue;
}
#test3{
color: red;
}
.c1.c2.c3.c4.c5.c6.c7.c8.c9.c10.c11{
color: blue;
}
</style>
</head>
<body class="body" id="body">
<div id="test1" class="test1">藍色</div>
<div class="test2">藍色</div>
<div id="test3" class="c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11">紅色</div>
</body>
</html>
複製代碼
結果以下圖所示: jquery
不妨發現,最後一個div
,儘管咱們類選擇器有11個,加起來有110,可是依舊沒有 id 選擇器優先級高,正是由於這個不進位的數字,簡單說:
你大爺仍是你大爺!
舉個栗子:webpack
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS選擇器</title>
<style>
.test1{
color: red;
}
.test1{
color: blue;
}
.test2{
color: red!important;
}
.test2{
color: blue;
}
#test3{
color: red;
}
</style>
</head>
<body class="body" id="body">
<div class="test1">藍色</div>
<div class="test2">紅色</div>
<div id="test3" style="color: blue;">藍色</div>
</body>
</html>
複製代碼
運行結果以下圖所示: css3
經典問題:圖片下面有空隙,原理是什麼?怎樣去除? git
原理是由於圖片按照 inline 進行排版,排版的時候會涉及到字體對齊,默認按照baseline
對齊,
baseline
和底線之間是有誤差的,誤差大小是根據字體大小而定,若是是12px字體大小,則圖片空隙就是3px左右。這就是經典圖片3px空隙問題。
解決方式:github
baseline
對齊,不妨將 vertical-align
設爲 bottom
,按照底線對齊。display:block
能夠解決該問題,可是會獨佔一行,以下圖所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>漸變色背景(線性梯度)</title>
<style>
.div1{
height: 90px;
}
.div2{
height: 90px;
/*background: linear-gradient(to right,red,green);*/
/*background: linear-gradient(180deg,red,green);*/
/*background: linear-gradient(135deg,red 0,green 15%,yellow 50%,blue 90%);*/
/*網格線*/
background: linear-gradient(135deg,transparent 0,transparent 49.5%,green 49.5%,green 50.5%,transparent 50.5%,transparent 100%),
linear-gradient(45deg,transparent 0,transparent 49.5%,red 49.5%,red 50.5%,transparent 50.5%,transparent 100%);
background-size: 30px 30px;
}
</style>
</head>
<body>
<div class="div1"></div>
<div class="div2"></div>
</body>
</html>
複製代碼
最後一個例子展現效果: web
background-repeat: no-repeat;
background-size: 30px 30px; //相對於容器偏移
background-position: 30px 30px;
複製代碼
雪碧圖 就是將不少個圖片進行拼接成一個圖片,而後經過 background-position
等屬性進行偏移,在網頁中獲得對應圖片,來達到減小http請求。
base64和性能優化
將圖片進行base64編碼後,就是一個字符文本,缺點之一就是圖片的體積會增大 1/3
左右,而且放入css文件中,也會致使css文件變大。另外,雖然能減小http請求,但增大瞭解碼的開銷。適用於小圖標icon
,例如loading
文件等。最後,在開發環境通常不採用直接將圖片進行 base64
編碼,由於對於協同開發來講,沒法知曉圖片原樣,只有一個文本。
通常狀況下,是在生產環境下,經過打包的方式,將小圖片進行 base64
編碼。
多分辨率適配
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>邊框</title>
<style>
.c1{
width: 400px;
height: 200px;
border: 1px solid red;
/*border: 5px solid red;*/
/*border: 5px dotted red;*/
/*border: 5px dashed red;*/
}
</style>
</head>
<body>
<div class="c1"></div>
</body>
</html>
複製代碼
經典問題:九宮格問題,例以下面圖片,咱們若是想要實現9個不一樣形式,而後中間放內容,若是用本來9個div方法,那麼會很是麻煩,而css3提供了 border
方式能夠解決上述問題。
border-image
處設置
round
以前,是如上效果,後面 30 是用來切出四個角。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>邊框</title>
<style>
.c1{
width: 400px;
height: 200px;
border: 30px solid transparent;
border-image: url(./xx.png) 30 round; /*round將圖片進行整數個拼接*/
}
</style>
</head>
<body>
<div class="c1"></div>
</body>
</html>
複製代碼
在 border-image
處設置 round
以後,是以下效果,基本達到咱們想要的效果。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>邊框</title>
<style>
.c2{
width: 400px;
height: 200px;
border: 30px solid transparent;
border-image: url(./xx.png) 30 round; /*round將圖片進行整數個拼接*/
}
</style>
</head>
<body>
<div class="c2"></div>
</body>
</html>
複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>邊框</title>
<style>
.c3{
width: 0px;
height: 200px;
border-bottom: 30px solid red;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
}
</style>
</head>
<body>
<div class="c3"></div>
</body>
</html>
複製代碼
實現三角形,效果以下:
產生滾動的緣由:當內容比容器多的時候,即容器裝不下內容的時候,就須要滾動。滾動主要包括以下幾個方面:
overflow
設置auto
和 scroll
區別,auto 是內容多於容器時顯示滾動條,反正,不顯示。而 scroll 是一直顯示滾動條
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>滾動</title>
<style>
.div1{
background: red;
height: 200px;
overflow: scroll; /*可選值: hidden visible auto scroll*/
}
</style>
</head>
<body>
<div class="div1">
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
<p>test</p>
</div>
</body>
</html>
複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文字折行</title>
<style>
.div1{
border: 1px solid;
width: 8em;
overflow-wrap: normal;
word-break: normal;
white-space: normal;
}
</style>
</head>
<body>
<div class="div1">
學如逆水行舟,不進則退!Learning is like sailing against the current, retreating if not advancing
</div>
</body>
</html>
複製代碼
展現效果:
經典問題:怎麼讓一個很長的文本不換行?將上述屬性設置爲 nowrap
便可
white-space: nowrap;
複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>font-weight</title>
<style>
.div1{
font-weight: normal; /*400*/
font-weight: bold; /*700*/
font-weight: bolder;
font-weight: lighter;
font-weight: 100;
}
</style>
</head>
<body>
<div class="div1">學如逆水行舟,不進則退!</div>
</body>
</html>
複製代碼
IE6
,那麼就加IE6
的class)background-position
等屬性進行偏移,在網頁中獲得對應圖片,來達到減小http請求,提升頁面加載性能。將圖片進行base64編碼後,就是一個字符文本,缺點之一就是圖片的體積會增大 1/3
左右,而且放入css文件中,也會致使css文件變大。另外,雖然能減小http請求,但增大瞭解碼的開銷。適用於小圖標icon
,例如loading
文件等。最後,在開發環境通常不採用直接將圖片進行 base64
編碼,由於對於協同開發來講,沒法知曉圖片原樣,只有一個文本。
::before{}
,在頁面中會顯示內容)label
來搞定)CSS體系知識的重中之重
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>table佈局</title>
<style>
.table{
margin-top: 20px;
width: 500px;
height: 100px;
display: table;
}
.table-row{
display: table-row;
}
.table-cell{
vertical-align: center;
display: table-cell;
}
.left{
background: red;
vertical-align: middle;
}
.right{
background: blue;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="table">
<div class="table-row">
<div class="left table-cell">left</div>
<div class="right table-cell">right</div>
</div>
</div>
</body>
</html>
複製代碼
展現效果以下圖所示:
display
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>display</title>
<style>
.block{
height: 200px;
background: red;
}
.inline{
display: inline;
background: green;
}
.inline-block{
display: inline-block;
width: 200px;
height: 100px;
background: blue;
}
</style>
</head>
<body>
<div class="block">
block
</div>
<div class="inline">inline</div>
<div class="inline-block">inline-block</div>
</body>
</html>
複製代碼
position
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>position</title>
<style>
.p1{
width: 100px;
height: 100px;
background: red;
}
.p2{
position: relative;
width: 100px;
height: 100px;
left: 20px;
top: -10px;
background: blue;
}
.p3{
position: absolute;
width: 100px;
height: 100px;
left: 80px;
top: 30px;
background: yellow;
}
.p4{
position: fixed;
width: 100px;
height: 100px;
left: 0;
bottom: 10px;
background: green;
}
.p5{
width: 100px;
height: 100px;
background: pink;
}
</style>
</head>
<body>
<div class="p1">position:static</div>
<div class="p2">position:relative</div>
<div class="p3">position:absloute</div>
<div class="p4">position:fixed</div>
<div class="p5">no position</div>
</body>
</html>
複製代碼
展現效果以下:
由上圖可知,當設置position
爲
relative
後,no position(粉色塊) 並無在藍色塊下面,也就是說設置
position
爲
relative
後,佔據空間仍是按照原來的方式計算的,不會由於偏移而改變佈局的計算。
經典問題:
絕對定位 absolute
,會優先查找父級定位爲 absolute
或 relative
的進行定位,若是父級沒有,找上級,那麼最終就會根據 body
進行定位,它和 fixed
同樣,也是脫離了文檔流。
fixed
相對於屏幕進行定位
層疊問題 ,由於默認會根據前後順序顯示,所以會有覆蓋狀況,此時,能夠經過設計 z-index
解決,權重越大,優先顯示。
(目前用的不是很熱的緣由是雖然簡單方便,可是兼容性有點問題)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>flexbox</title>
<style>
.container{
width: 800px;
height: 200px;
display: flex;
border: 1px solid black;
}
.flex{
background: blue;
flex: 1;
margin: 5px;
}
</style>
</head>
<body>
<div class="container">
<div class="flex">flex</div>
<div class="flex">flex</div>
<div class="flex">flex</div>
<div class="flex">flex</div>
<div class="flex">flex</div>
</div>
</body>
</html>
複製代碼
展現效果以下:
由這個很方便的佈局,咱們能夠寫一個 聖盃佈局的例子(即左右固定,中間自適應):<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>聖盃佈局-flexbox</title>
<style>
*{
margin: 0;
}
.container{
min-height: 200px;
display: flex;
}
.left{
width: 200px;
display: flex;
background: red;
}
.center{
background: yellow;
flex: 1;
}
.right{
width: 200px;
display: flex;
background: blue;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="center">center</div>
<div class="right">right</div>
</div>
</body>
</html>
複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>float佈局</title>
<style>
.container{
width: 400px;
background: red;
}
.p1{
width: 200px;
height: 80px;
float: left;
background: pink;
}
</style>
</head>
<body>
<div class="container">
<span class="p1">float元素</span>
<div class="p2">不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字不少文字</div>
</div>
</body>
</html>
複製代碼
展現效果以下:
float 特性對本身的影響:
container
寬度夠的話,就儘可能靠上和靠左,若是不行的話,就往下排了)簡單例子:
float 特性對兄弟的影響:float 特性對父級的影響:
經典問題:
清除浮動的方式
① 當咱們設置爲 float 後,會將元素設爲 BFC,接管本身的寬高,所以咱們也可讓父級元素設置爲 BFC,來接管本身的寬高。
初始狀態:
在父級元素設置overflow: auto/hidden;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>清楚浮動</title>
<style>
.container{
background: red;
width: 400px;
overflow: auto;
margin: 10px;
}
.p1{
background: pink;
float: left;
width: 200px;
height: 50px;
}
</style>
</head>
<body>
<div class="container">
<span>xxxxxx</span>
<span class="p1">float</span>
<span class="p1">float</span>
</div>
<div class="container" style="height: 200px; background: blue;">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
</div>
</body>
</html>
複製代碼
最終狀態效果圖以下:
② 因爲 float 不脫離文檔流(不會影響其它元素佈局),那麼就可使得元素恰好到達下面位置。就能夠經過其它元素將父級撐起來,當父級恰好撐到下方位置時,就能夠將浮動清除掉。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>清楚浮動</title>
<style>
.container{
background: red;
width: 400px;
}
.p1{
background: pink;
float: left;
width: 200px;
height: 50px;
}
.removeFloat::after{
content: 'aaa';
clear: both; /*保證當前元素左右沒有浮動元素*/
display: block;
height: 0;
visibility: hidden;
}
</style>
</head>
<body>
<div class="container removeFloat">
<span>xxxxxx</span>
<span class="p1">float</span>
<span class="p1">float</span>
</div>
<div class="container" style="height: 200px; background: blue;">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
</div>
</body>
</html>
複製代碼
經典清除浮動方式,效果圖以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>聖盃佈局-float</title>
<style>
.container{
min-height: 200px;
}
.left{
float: left;
background: red;
width: 200px;
}
.center{
margin-left: 200px;
margin-right: 200px;
background: yellow;
}
.right{
float: right;
background: blue;
width: 200px;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
<div class="center">center</div>
</div>
</body>
</html>
複製代碼
展現效果以下:
例以下圖,紅藍之間有一個間隙,是由於 inline-block
就像文本同樣,咱們沒辦法讓兩個字牢牢地挨着,因而咱們就有了下文所述解決方式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>inline-block佈局</title>
<style>
.container{
width: 800px;
height: 200px;
font-size: 0;
}
.left{
font-size: 15px;
background: red;
display: inline-block;
width: 200px;
height: 200px;
}
.right{
font-size: 15px;
background: blue;
display: inline-block;
width: 500px;
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
</div>
</body>
</html>
複製代碼
展現效果以下:
查看上述代碼,將父級元素字體大小設置爲0便可解決間隙問題,但子元素須要設置本身的字體大小,否則就不會顯示。所以,作自適應方面會有點麻煩,不像上文 float+margin
那樣簡單
font-size
)來肯定元素大小,但不太精確 / viewport:經過 js或手工 肯定整個界面放到多大 / media query:媒體查詢,根據不一樣的設備來匹配不一樣的樣式)下圖含義就是隻有在寬度小於 640px 時纔會生效,即讓移動端進行適配。
經典問題:
絕對定位 absolute
,會優先查找父級定位爲 absolute
或 relative
的進行定位,若是父級沒有,找上級,那麼最終就會根據 body
進行定位,它和 fixed
同樣,也是脫離了文檔流。
fixed
相對於屏幕(viewport)進行定位
層疊問題 ,由於默認會根據前後順序顯示,所以會有覆蓋狀況,此時,能夠經過設計 z-index
解決,權重越大,優先顯示。
font-size
設爲0)緣由:浮動的元素不會佔據父元素的佈局空間,也就是父級元素不會管浮動元素,有可能浮動元素會超出父元素,從而對其它元素產生影響。
經典問題:
清除浮動的方式
① 當咱們設置爲 float 後,會將元素設爲 BFC,接管本身的寬高,所以咱們也可讓父級元素設置爲 BFC,來接管本身的寬高。
初始狀態:
在父級元素設置overflow: auto/hidden;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>清楚浮動</title>
<style>
.container{
background: red;
width: 400px;
overflow: auto;
margin: 10px;
}
.p1{
background: pink;
float: left;
width: 200px;
height: 50px;
}
</style>
</head>
<body>
<div class="container">
<span>xxxxxx</span>
<span class="p1">float</span>
<span class="p1">float</span>
</div>
<div class="container" style="height: 200px; background: blue;">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
</div>
</body>
</html>
複製代碼
最終狀態效果圖以下:
② 因爲 float 不脫離文檔流(不會影響其它元素佈局),那麼就可使得元素恰好到達下面位置。就能夠經過其它元素將父級撐起來,當父級恰好撐到下方位置時,就能夠將浮動清除掉。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>清楚浮動</title>
<style>
.container{
background: red;
width: 400px;
}
.p1{
background: pink;
float: left;
width: 200px;
height: 50px;
}
.removeFloat::after{
content: 'aaa';
clear: both; /*保證當前元素左右沒有浮動元素*/
display: block;
height: 0;
visibility: hidden;
}
</style>
</head>
<body>
<div class="container removeFloat">
<span>xxxxxx</span>
<span class="p1">float</span>
<span class="p1">float</span>
</div>
<div class="container" style="height: 200px; background: blue;">
<p>第一行</p>
<p>第二行</p>
<p>第三行</p>
</div>
</body>
</html>
複製代碼
經典清除浮動方式,效果圖以下:
主要方法:隱藏 + 折行 + 自適應空間(rem:經過html的字體大小(即 font-size
)來肯定元素大小,但不太精確 / viewport:經過 js或手工 肯定整個界面放到多大 / media query:媒體查詢,根據不一樣的設備來匹配不一樣的樣式)
box-shadow: 5px 5px 10px 0 rgba(0, 0, 0, .2);
複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>border-radius</title>
<style>
.container{
width: 100px;
height: 100px;
background: red;
border-radius: 50%;
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>
複製代碼
展現效果以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D-transform</title>
<style>
.container{
margin: 50px;
padding: 10px;
border: 1px solid red;
width: 200px;
height: 200px;
position: relative;
perspective: 500px;
}
#cube{
width: 200px;
height: 200px;
transform-style: preserve-3d;
transform: translateZ(-100px);
transition: transform 1s;
}
#cube:hover{
transform: translateZ(-100px) rotateX(90deg) rotateY(90deg);
}
#cube div{
width: 200px;
height: 200px;
position: absolute;
line-height: 200px;
font-size: 50px;
text-align: center;
}
.front{
background: rgba(255, 0, 0, .3);
transform: translateZ(100px);
}
.back{
background: rgba(0, 255, 0, .3);
transform: translateZ(-100px) rotateY(180deg);
}
.left{
background: rgba(0, 0, 255, .3);
transform: translateX(-100px) rotateY(-90deg);
}
.right{
background: rgba(255, 255, 0, .3);
transform: translateX(100px) rotateY(90deg);
}
.top{
background: rgba(255, 0, 255, .3);
transform: translateY(-100px) rotateX(-90deg);
}
.bottom{
background: rgba(0, 255, 255, .3);
transform: translateY(100px) rotateX(90deg);
}
</style>
</head>
<body>
<div class="container">
<div id="cube">
<div class="front">1</div>
<div class="back">2</div>
<div class="right">3</div>
<div class="left">4</div>
<div class="top">5</div>
<div class="bottom">6</div>
</div>
</div>
</body>
</html>
複製代碼
展現效果圖以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>transition補間動畫</title>
<style>
.container{
width: 100px;
height: 100px;
background: red;
transition: width 1s, background 2s;
}
.container:hover{
width: 300px;
background: blue;
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>
複製代碼
展現效果以下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>keyframe關鍵幀動畫</title>
<style>
.container{
width: 100px;
height: 100px;
background: red;
animation: run 1s;
animation-direction: reverse;
animation-iteration-count: infinite;
/*animation-fill-mode: forwards;*/
/*animation-play-state: paused;*/
}
@keyframes run {
0%{
width: 100px;
}
100%{
width: 800px;
}
}
</style>
</head>
<body>
<div class="container"></div>
</body>
</html>
複製代碼
展現效果:
例如:一個動物跑動的過程,經過將每一個動做的圖片逐幀顯示,造成動畫的感受。
box-shadow
等)常見的有 less
(基於node寫的,編譯比較快,入門簡單)和 sass
(基於Ruby
寫的)
全局安裝 less
npm install less -g
複製代碼
建立一個
test.less
文件
body{
padding: 0;
margin: 0;
}
.wrapper{
background: white;
.nav{
font-size: 12px;
}
.content{
font-size: 14px;
&:hover{
background: red;
}
}
}
複製代碼
在該文件處調用終端,執行如下命令:
lessc test.less
複製代碼
輸出結果:
執行以下命令,輸出到 test.css
文件中
lessc test.less > test.css
複製代碼
查看 test.css
文件,獲得以下結果:
body {
padding: 0;
margin: 0;
}
.wrapper {
background: white;
}
.wrapper .nav {
font-size: 12px;
}
.wrapper .content {
font-size: 14px;
}
.wrapper .content:hover {
background: red;
}
複製代碼
全局安裝 sass
cnpm install node-sass -g
複製代碼
建立一個 test.scss
文件
body{
padding: 0;
margin: 0;
}
.wrapper{
background: white;
.nav{
font-size: 12px;
}
.content{
font-size: 14px;
&:hover{
background: red;
}
}
}
複製代碼
執行以下命令,查看輸出結果:
node-sass test.scss
複製代碼
執行以下命令,輸出到
test-scss.css
文件中
node-sass test.scss > test-scss.css
複製代碼
查看 test-scss.css
文件,獲得以下結果:
body {
padding: 0;
margin: 0; }
.wrapper {
background: white; }
.wrapper .nav {
font-size: 12px; }
.wrapper .content {
font-size: 14px; }
.wrapper .content:hover {
background: red; }
複製代碼
從以上代碼來看,sass嵌套會保留原來嵌套樣式,對於css觀賞性來講,並非咱們特別想要的,因而咱們能夠執行以下命令,更改輸出樣式:
node-sass --output-style expanded test.scss > test-scss.css
複製代碼
輸出結果就和less嵌套同樣了,讀者能夠本身跑一遍!
建立 variable-less.less
文件
@fontSize: 12px;
@bgColor: red;
body{
padding: 0;
margin: 0;
}
.wrapper{
background: lighten(@bgColor,40%);
.nav{
font-size: @fontSize;
}
.content{
font-size: @fontSize+2px;
&:hover{
background: @bgColor;
}
}
}
複製代碼
建立 variable-less.css
文件,執行以下命令:
lessc variable-less.less > variable-less.css
複製代碼
能夠獲得以下代碼:
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .nav {
font-size: 12px;
}
.wrapper .content {
font-size: 14px;
}
.wrapper .content:hover {
background: red;
}
複製代碼
相似less變量,只須要將 @
改成 $
便可,由於 less
變量命名這樣作,是更貼近CSS。而 sass
是爲了區別CSS。
建立 mixin.less
文件,複製以下代碼:
@fontSize: 12px;
@bgColor: red;
.block(@fontSize){
font-size: @fontSize;
border: 1px solid #ccc;
border-radius: 4px;
}
body{
padding: 0;
margin: 0;
}
.wrapper{
background: lighten(@bgColor,40%);
.nav{
.block(@fontSize);
}
.content{
.block(@fontSize+2px);
&:hover{
background: @bgColor;
}
}
}
複製代碼
建立 mixin-less.css
文件,執行以下代碼:
lessc mixin.less > mixin-less.css
複製代碼
查看mixin-less.css
文件,發現 .block
沒有了,在CSS內部就完成了樣式複用。
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .nav {
font-size: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
.wrapper .content {
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
}
.wrapper .content:hover {
background: red;
}
複製代碼
建立 mixin.scss
文件,複製以下代碼:
$fontSize: 12px;
$bgColor: red;
@mixin block($fontSize){
font-size: $fontSize;
border: 1px solid #ccc;
border-radius: 4px;
}
body{
padding: 0;
margin: 0;
}
.wrapper{
background: lighten($bgColor,40%);
.nav{
@include block($fontSize);
}
.content{
@include block($fontSize+2px);
&:hover{
background: $bgColor;
}
}
}
複製代碼
建立 mixin-sass.css
文件,執行以下代碼:
node-sass --output-style expanded mixin.scss > mixin-sass.css
複製代碼
查看mixin-sass.css
文件,發現 .block
沒有了,在CSS內部就完成了樣式複用。
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .nav {
font-size: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
.wrapper .content {
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
}
.wrapper .content:hover {
background: red;
}
複製代碼
總結:從上文對比來看,sass mixin 除了調用變量符號要換爲 $
外,抽離公共樣式須要使用 @mixin
,此外,調用時還須要使用 @include
。
那麼,mixin還能作什麼呢?
例如,對於佈局方面,咱們在上文提到了清楚浮動的方式,那麼,咱們就能夠將這段樣式進行復用,只須要在須要的元素處引用便可。
上文提到過的,使用過 less mixin 抽離以後,咱們能夠獲得以下代碼,可是呢,又會有一個小問題,就是咱們會有重複樣式代碼。在生產環境下,就會有大量這樣的形式出現,這就會影響到咱們的CSS體積了。
一種比較好的方式,以下,咱們再講公共樣式代碼進行抽離,這顯然會下降咱們的CSS體積。所以,extend就發揮了它的做用
建立 extend-less.less
文件,複製以下代碼:
@fontSize: 12px;
@bgColor: red;
.block{
font-size: @fontSize;
border: 1px solid #ccc;
border-radius: 4px;
}
body{
padding: 0;
margin: 0;
}
.wrapper{
background: lighten(@bgColor,40%);
.nav{
&:extend(.block);
}
.content:extend(.block){
&:hover{
background: @bgColor;
}
}
}
複製代碼
建立 extend-less.css
文件,執行以下代碼:
lessc extend-less.less > extend-less.css
複製代碼
查看 extend-less.css
文件,發現代碼體積相對減小了。
.block,
.wrapper .nav,
.wrapper .content {
font-size: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .content:hover {
background: red;
}
複製代碼
總結:mixin 和 extend 區別, mixin 能處理一些複雜邏輯,好比添加變量,但會致使體積增大問題。而 extend的話,是將選擇器提取出來,更加簡單,不適合處理複雜邏輯,能將體積進行必定減小。
建立 extend-sass.scss
文件,複製以下代碼:
$fontSize: 12px;
$bgColor: red;
.block{
font-size: $fontSize;
border: 1px solid #ccc;
border-radius: 4px;
}
body{
padding: 0;
margin: 0;
}
.wrapper{
background: lighten($bgColor,40%);
.nav{
@extend .block;
}
.content{
@extend .block;
&:hover{
background: $bgColor;
}
}
}
複製代碼
建立 extend-sass.css
文件,執行以下代碼:
node-sass --output-style expanded extend-sass.scss > extend-sass.css
複製代碼
查看 extend-sass.css
文件,發現代碼體積相對減小了。
.block, .wrapper .nav, .wrapper .content {
font-size: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .content:hover {
background: red;
}
複製代碼
建立 loop-less.less
文件,複製以下代碼
.gen-col(@n) when (@n > 0){
.gen-col(@n - 1);
.col-@{n}{
width: 1000px/12*@n;
}
}
.gen-col(12);
複製代碼
建立 loop-less.css
文件,執行以下代碼:
lessc loop-less.less > loop-less.css
複製代碼
查看 loop-less.css
文件,發現有12個樣式
.col-1 {
width: 83.33333333px;
}
.col-2 {
width: 166.66666667px;
}
.col-3 {
width: 250px;
}
.col-4 {
width: 333.33333333px;
}
.col-5 {
width: 416.66666667px;
}
.col-6 {
width: 500px;
}
.col-7 {
width: 583.33333333px;
}
.col-8 {
width: 666.66666667px;
}
.col-9 {
width: 750px;
}
.col-10 {
width: 833.33333333px;
}
.col-11 {
width: 916.66666667px;
}
.col-12 {
width: 1000px;
}
複製代碼
建立 loop-sass.scss
文件,複製以下代碼:
@mixin gen-col($n){
@if $n > 0 {
@include gen-col($n - 1);
.col-#{$n}{
width: 1000px/12*$n;
}
}
}
@include gen-col(12);
複製代碼
建立 loop-sass.css
,執行以下代碼:
node-sass --output-style expanded loop-sass.scss > loop-sass.css
複製代碼
查看 loop-sass.css
文件,發現有12個樣式
.col-1 {
width: 83.33333px;
}
.col-2 {
width: 166.66667px;
}
.col-3 {
width: 250px;
}
.col-4 {
width: 333.33333px;
}
.col-5 {
width: 416.66667px;
}
.col-6 {
width: 500px;
}
.col-7 {
width: 583.33333px;
}
.col-8 {
width: 666.66667px;
}
.col-9 {
width: 750px;
}
.col-10 {
width: 833.33333px;
}
.col-11 {
width: 916.66667px;
}
.col-12 {
width: 1000px;
}
複製代碼
上述版本代碼或許是挺複雜的,好在 sass
提供了for循環,見以下代碼:
@for $i from 1 through 12 {
.col-#{$i} {
width: 1000px/12*$i;
}
}
複製代碼
建立 import-less.less
文件,複製以下代碼:
@import "./mixin";
@import "./variable-less";
複製代碼
建立 import-less.css
文件,執行以下代碼:
lessc import-less.less > import-less.css
複製代碼
查看 import-less.less
文件
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .nav {
font-size: 12px;
border: 1px solid #ccc;
border-radius: 4px;
}
.wrapper .content {
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
}
.wrapper .content:hover {
background: red;
}
body {
padding: 0;
margin: 0;
}
.wrapper {
background: #ffcccc;
}
.wrapper .nav {
font-size: 12px;
}
.wrapper .content {
font-size: 14px;
}
.wrapper .content:hover {
background: red;
}
複製代碼
語法上沒有什麼變化,注意符號是 $
用於組件交互
基於jquery寫的,同時,依賴第三方庫 Popper.js和 bootstrap.js
如上圖,第二個js文件包含了 Popper.js
使用方式:
很是精華的部分
關注幾個事情:組織、優化、構建、維護
常見:
全局安裝 postcss-cli
npm install postcss-cli -g
複製代碼
在 postcss.config.js
文件進行配置
PostCSS 支持的構建工具
話說,我有女友嗎?我有嗎?原來我沒有...
扯開女友話題,終於完成了這篇史詩級大做,時隔幾天時間。更多筆記內容還請關注 小獅子前端筆記倉庫 (不要白嫖,給個 star ✿✿ヽ(°▽°)ノ✿ 就是對我最大的支持啦~)學如逆水行舟,不進則退
複製代碼