官方文檔:http://www.w3school.com.cn/cssref/index.aspcss
基礎簡單過下,事先說明下:諸如引入、註釋、案例就不一一演示了,有個工做1年左右的人均可以熟練掌握,因此基礎部分就簡單概括一下W3C的經常使用屬性列表了html
官方文檔:http://www.w3school.com.cn/cssref/index.asp#font or http://www.w3school.com.cn/cssref/index.asp#text前端
color
:文字的顏色
color:red;
、color: #000;
、color: rgb(0,0,0);
font-size
:文字的大小
font-size:12px;
font-family
:文字的字體
font-family:'Microsoft Yahei';
font-style
:字體是否傾斜
font-style:normal;
i
、em
這些會致使字體傾斜的標籤設置font-weight
:文字是否加粗
font-weight:bold;
、字體不加粗:font-weight:normal;
**line-height**
:文字的行高
line-height:24px;
2~6彙總設置
:font
:同時設置文字的幾個屬性
font:是否加粗 字號/行高 字體;
font:normal 12px/36px 'Microsoft Yahei';
text-align
:文字水平對齊方式
text-align:center
list-style-type
:設置列表項標記符號
list-style-type:none;
text-decoration
:文字下劃線
text-decoration:none;
a
的下劃線text-indent
:文字首行縮進
text-indent:2em;
text-transform
:控制文本的大小寫
none
:定義帶有小寫字母和大寫字母的標準的文本(默認)capitalize
:單詞中首字母大寫uppercase
:單詞大寫lowercase
:單詞小寫官方文檔:http://www.w3school.com.cn/cssref/index.asp#backgroundhtml5
background
:彙總2~6
background: 顏色 url(背景圖) no-repeat 定位 fixed
background:url(雪碧圖) no-repeat left -3.025rem;
background-color
:設置元素的背景顏色background-image
:設置元素的背景圖像background-repeat
:設置是否及如何重複背景圖像background-position
:設置背景圖像的開始位置
background-position: center center;
background-attachment
:設置背景圖像是否固定或者隨着頁面的其他部分滾動普通盒子寬 | 高
= width | height
+ padding
+ border
css3
官方文檔:http://www.w3school.com.cn/cssref/index.asp#dimensiongit
width
:寬度height
:高度max-width
:最大寬度max-height
:最大高度min-height
:最小高度min-width
:最小寬度官方文檔:http://www.w3school.com.cn/cssref/index.asp#bordergithub
border
:2~4彙總
border:width style color;
border:10px solid red;
border-width
:設置四條邊框的寬度border-style
:設置四條邊框的樣式
solid
:實線、dashed
:虛線、dotted
:點線border-color
:設置四條邊框的顏色PS:指定邊框樣式:border-[top|left|right|bottom]:width style color;
bootstrap
border-top: #00c1de .2rem solid;
官方文檔:http://www.w3school.com.cn/cssref/index.asp#positioning小程序
浮動自己沒什麼知識點,清除浮動卻是能夠說下:segmentfault
float: none;
:不浮動(默認值)float: left;
:左浮動float: right;
:右浮動clear: both;
:清除全部浮動
[left | right | both |none]
之前清除浮動都是在浮動div下添加一個div.clear的空白div,如今基本上都是用淘寶的僞元素大法
:
PS:有利於SEO
/* 清除浮動的樣式 */ .clearfix::before, .clearfix::after { content: ''; display: table; } .clearfix::after { clear: both; } /* 兼容IE */ .clearfix { zoom: 1; }
overflow:元素溢出
清除浮動
、解決margin-top塌陷問題
PS:若是隻只對一個方向可使用overflow-x
| overflow-y
main{position:absolute;left:0;right:0;top:2.5rem;bottom:2.5rem;overflow-x:hidden;overflow-y:auto;}
position
:定位
absolute
:絕對定位fixed
:固定定位relative
:相對定位static
:沒有定位(默認值)position
使用)
top
:距離上方的距離left
:距離左方的距離right
:距離右方的距離bottom
:距離下方的距離z-index
PS:絕對定位和固定定位的塊元素和行內元素會自動轉化爲行內塊
元素
來個常用的案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>定位案例</title> <link rel="stylesheet" href="https://at.alicdn.com/t/font_1148101_h5fe3ssorlk.css"> <style> header { width: 100px; margin: 0 auto; text-align: center; } /* 字體圖標 */ header a.icon-guoxue { /* 去除下劃線 */ text-decoration: none; color: #1a8236; font-size: 100px; /* 父類元素設置相對|絕對定位 */ position: relative; } header span { position: absolute; top: 15px; right: -5px; /* 絕對定位和固定定位的塊元素和行內元素會自動轉化爲行內塊元素 */ width: 30px; height: 30px; font-size: 16px; /* 居中顯示 */ line-height: 30px; color: white; background-color: red; /* 變成圓 */ border-radius: 50%; } </style> </head> <body> <!-- a.iconfont.icon-guoxue>span{99} --> <header> <a href="#" class="iconfont icon-guoxue"><span>99</span></a> </header> </body> </html>
效果:
官方文檔:http://www.w3school.com.cn/cssref/index.asp#padding
padding
:彙總屬性
padding:上 右 下 左;
padding: 上 左右 下;
padding-bottom
:設置元素的下內邊距padding-left
:設置元素的左內邊距padding-right
:設置元素的右內邊距padding-top
:設置元素的上內邊距官方文檔:http://www.w3school.com.cn/cssref/index.asp#margin
margin
:彙總屬性
margin:x auto;
margin:上 右 下 左;
、三個值:margin: 上 左右 下;
margin-bottom
:設置元素的下外邊距margin-left
:設置元素的左外邊距margin-right
:設置元素的右外邊距margin-top
:設置元素的上外邊距PS:有時候有margin-top
塌陷和外邊距合併
的問題,so ==> 儘可能用padding
重現:在兩個盒子嵌套時候,內部的盒子設置的margin-top會加到外邊的盒子上,致使內部的盒子margin-top設置失敗
解決:外部盒子設置一個邊框,而且在外部盒子裏設置overflow:hidden
,最後使用僞元素.clearfix:before{content: '';display:table;}
width
、height
、margin
和padding
用代碼表示一下:
display:inline
:內聯元素(默認)
display:block
:塊級元素
display:inline-block
:行內塊元素隱藏:display:none
此元素不會顯示也不佔空間,visibility:hidden;
元素是否可見且佔空間
PS:加過渡動畫的時候,visibility有動畫效果,而display沒有
之前是隻要不去換行就沒事了(去掉內聯元素之間的換行),如今get到一個新skill:
cursor:pointer;
white-space: nowrap;
(normal
是默認值)
border-collapse:collapse;
(如今基本上用不到了)選擇器參考手冊:http://www.w3school.com.cn/cssref/css_selectors.asp
p{color:red;}
#footer{color:#a7a8a9;}
.copyright {text-align: center;}
空格間隔
)
.copyright a{color: #a7a8a9;}
:設置copyright類下的a標籤顏色em,i {font-style: normal;}
:去除em和i的斜體樣式僞類|僞元素
選擇器:兩個:就是僞元素
nav li:hover {color: #000;}
.clearfix::after {content:'';display:table;clear:both;}
:清除浮動:僞類
(一種選擇器)eg::hover
、a:active
、a:visited
::僞元素
(經過CSS實現的頁面元素)eg:::before
、::after
/* 經常使用僞類 */ a:link {color: #FF0000} /* 未訪問的連接 */ a:visited {color: #00FF00} /* 已訪問的連接 */ a:hover {color: #FF00FF} /* 鼠標移動到連接上 */ a:active {color: #0000FF} /* 選定的連接 */
參考文檔:http://www.w3school.com.cn/css/css_syntax_attribute_selector.asp
之前就有的:表單
用的比較多些
[屬性名] {}
:匹配對應的屬性便可[屬性名=值] {}
:匹配屬性名是某值[屬性名|=值]
(屬性名以某值開頭)和[屬性名~=值]
(屬性名包含某值)CSS3新增:結合爬蟲
有大用
[屬性名^=值] {}
:以值開頭
a[src^="https"]
:選擇src屬性以https
開頭的每一個<a>
元素[屬性名$=值] {}
:以值結束
a[src$=".pdf"]
:選擇src屬性以.pdf
結尾的全部<a>
元素[屬性名*=值] {}
:包含某值
a[src*="baidu.com"]
:選擇src屬性包含baidu.com
子串的每一個<a>
元素來個簡單案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>屬性選擇器</title> <style> input[type="password"] { background-color: yellow; } // name以user開頭的表單(eg:用戶相關字段) input[name^="user"] { background-color: green; } </style> </head> <body> <input type="hidden" name="id"><br/> <input type="text" name="user_name"><br/> <input type="text" name="user_age"><br/> <input type="password" name="pass"> </body> </html>
效果:
結構性僞類選擇器:n
能夠是數值
也能夠是表達式
(eg:2n+1
)
:nth-child(n) {}
:選中父元素中正數第n個子元素
:first-child {}
:選中父元素中第一個子元素:nth-last-child(n) {}
:選中父元素中倒數第n個子元素
:last-child {}
:選中父元素中最後一個子元素:nth-of-type(n){}
:選中父元素中特定
正數第n個子元素
:nth-last-child(n) {}
:選中父元素特定
元素中倒數第n個子元素這個看一個必須來個案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>結構性僞類選擇器</title> <style> /* 本意先找到bloginfo類下的第一個div,對它第二個span設置樣式 */ /* 瀏覽器會這麼找:先去bloginfo類下的第一個div中找第二個元素,而後看看是否是span,若是是則生效 */ /* PS:這邊父類下第二個元素不是span,因此不生效 */ .bloginfo div:first-child span:nth-child(2) { color: red; } /* 若是須要達到這個效果能夠這麼用: */ .bloginfo div:first-child span:nth-child(4) { color: red; } /* 本意先找到bloginfo類下的第二個div,對它第二個span設置樣式 */ /* 瀏覽器會這麼找:先去bloginfo類下的第二個div中找全部的span,而後給第二個span設置樣式 */ .bloginfo div:last-child span:nth-of-type(2) { color: green; } </style> </head> <body> <div class="bloginfo"> <div> <span>逆天</span> <i>this is test</i> <time>2019-04-14</time> <span>【Web】</span> </div> <!-- 和上面同樣 --> <div> <span>逆天</span> <i>this is test</i> <time>2019-04-14</time> <span>【Web】</span> </div> </div> </body> </html>
輸出圖示:
:target
:被錨連接指向的時候會觸發該選擇器::selection
:當被鼠標選中的時候的樣式::first-line
:選中第一行::first-letter
:選中第一個字符這個官方文檔我卻是沒找到,可是流傳比較多的是這麼個順序(已驗)
!important
(權重值:10000
)加在樣式屬性值後
display: block!important;
內聯
樣式(權重值:1000
)
style="xx"
ID
選擇器(權重值:100
)
#content{xx}
類
,僞類
和屬性
選擇器(權重值:10
)
.clearfix
、:hover
、input[type="text"]
標籤
選擇器和僞元素
選擇器(權重值:1)
div
、p
、:before
通用
選擇器(*)、子
選擇器(>)、相鄰
選擇器(+)、同胞
選擇器(~)的(權重值:0)
PS:這個平時用不到,可是對於修改模版樣式不生效的時候常常會用到
你有沒有發現有些樣式修改了並不生效?這時候CSS權重的重要性就來了
opacity
:設置元素總體透明度rgba
:設置帶透明色的顏色
來個區分案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>透明背景</title> <style> div { width: 200px; height: 100px; color: green; border: 1px solid black; } div:nth-child(1) { background-color: black; /* 設置整個div的透明色 */ opacity: 0.2; } div:nth-child(2) { /* 只設置背景色的透明度 */ background-color: rgba(0, 0, 0, 0.2); } </style> </head> <body> <div> <h2>我是一個測試文字</h2> </div> <div> <h2>我是一個測試文字</h2> </div> </body> </html>
效果:
官方案例:http://www.w3school.com.cn/tiy/c.asp?f=css_background-size
background-size
:規定背景圖片的尺寸
background-size:30% 40%;
:能夠是數值或百分比
contain
:等比縮放,直至寬或者高和盒子相同
cover
:等比縮放,並填充慢容器
看個案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>背景尺寸</title> <style> div { width: 100px; height: 100px; background: url(http://github.lesschina.com/html5/images/logo.gif) no-repeat; border: 1px solid black; } div:nth-child(1) { /* 寬 高 */ background-size: 100% 50%; } div:nth-child(2) { /* 等比縮放,直至寬或者高和盒子相同 */ background-size: contain; } div:nth-child(3) { /* 等比縮放,並填充慢容器 */ background-size: cover; } </style> </head> <body> <div></div> <div></div> <div></div> </body> </html>
效果圖:
CSS3能夠設置兩個背景圖:background-image:url(xxx),url(xxx);
(疊加顯示)
看個案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>兩個背景圖</title> <style> div { width: 260px; height: 462px; /* 三句合爲一句寫 */ background: url(https://github.lesschina.com/html5/images/logo.gif) no-repeat center center, url(https://github.lesschina.com/html5/images/banner/b02.jpg) no-repeat center; /* background-image: url(https://github.lesschina.com/html5/images/logo.gif), url(https://github.lesschina.com/html5/images/banner/b02.jpg); */ /* background-repeat: no-repeat; */ /* 垂直居中 + 水平居中 */ /* background-position: center center; */ border: 1px solid black; } </style> </head> <body> <div></div> </body> </html>
效果:
background-origin
:設置背景圖片的定位區域:
padding-box
:之內邊距(padding
)左上角爲起點進行定位(默認值)border-box
:以邊框(border
)左上角爲起點進行定位content-box
:之內容區域(content
)左上角爲起點background-clip
:規定背景的繪製區域
border-box
:背景圖片在整個容器中顯示padding-box
:背景圖片在內邊距+正文區域顯示content-box
:背景圖片只在正文區域顯示border-radius
:邊框圓角border-image
:邊框圖片(eg:border-image:url(border.png) 20 round;
)
border-image-source: url("xx");
border-image-slice: 數值;
不須要帶單位
border-image-repeat
: 設置邊框圖片的平鋪方式(如何重複圖像邊框)
stretch
:拉伸(默認)round
:環繞的方式重複顯示repeat
:重複顯示
border-image-width: 數值;
box-shadow: 水平陰影的位置 垂直陰影的位置 [模糊距離] [陰影的尺寸] [陰影的顏色] [內|外陰影];
text-shadow
:設置文本陰影box-shadow: 0px 0px 10px #fff;
(水平陰影位置,垂直陰影位置,模糊距離,顏色)linear-gradient
to [ right | top | bottom | left ]
or 0deg:從下向上漸變],90deg:從左向右
linear-gradient(to left top, blue, red);
radial-gradient
background-image
這個比較簡單,簡單說下經常使用的兩種便可:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>圓角案例</title> <style> /* 橢圓矩形 */ div:first-child { width: 200px; height: 200px; background: black; border-radius: 10px; } /* 設置圖片大小,並顯示爲圓 */ div img { /* 半徑是長度的50%==>圓 */ border-radius: 50%; width: 200px; height: 200px; } div:last-child i { display: block; width: 200px; height: 200px; padding: 5px; border-radius: 50%; /* 設置一個漸變色 */ background: linear-gradient(to top right, rgba(0, 153, 255, .9), rgba(42, 228, 197, .7)); } </style> </head> <body> <div></div><br /> <div><img src="https://github.lesschina.com/html5/images/avatar.jpg"></div><br /> <div><i><img src="https://github.lesschina.com/html5/images/avatar.jpg"></i></div> </body> </html>
圖示:
我先用PS來簡單擴充一下裁切的概念:(類比Padding和margin的設置方法,而後不算單位)
看個簡單案例:(平時設置網站風格的時候用的比較多,之前是設置背景圖片,如今省事也省帶寬了)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>邊框圖片</title> <style> div { width: 300px; height: 300px; border: 50px solid black; font-family: '鍾齊流江毛筆草體'; font-size: 30px; line-height: 300px; /* 豎排顯示(從左往右方向) */ writing-mode: vertical-lr; text-align: center; float: left; } div:nth-child(2) { /* 設置邊框圖片 */ border-image-source: url(../images/1.png); } div:nth-child(3) { border-image: url(../images/1.png) 54 80 62 68; /* border-image-source: url(../images/1.png); */ /* 設置邊框圖片的裁切 */ /* border-image-slice: 54 80 62 68; */ } </style> </head> <body> <div>微信公衆號 逸鵬說道</div> <div>微信公衆號 逸鵬說道</div> <div>微信公衆號 逸鵬說道</div> </body> </html>
圖示:
平鋪方式簡單說下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>邊框圖片2</title> <style> div { width: 250px; height: 250px; margin-right: 20px; border: 50px solid black; font-size: 25px; line-height: 300px; /* 豎排顯示(從左往右方向) */ writing-mode: vertical-lr; text-align: center; float: left; } div:first-child { border-image: url(../images/2.png); } div:nth-child(2) { /* 裁切20就能夠把四個角的愛心完美扣下了 */ border-image: url(../images/2.png) 20; } div:nth-child(3) { /* 設置邊框圖片的平鋪方式 */ border-image: url(../images/2.png) 20 repeat; } div:last-child { /* 推薦方式 */ border-image: url(../images/2.png) 20 round; } </style> </head> <body> <div>只設置了邊框圖片</div> <div>設置邊框圖片和裁切</div> <div>邊框平鋪方式爲重複</div> <div>邊框平鋪方式爲環繞</div> </body> </html>
圖示:
課後拓展:
個人理解:若是一個元素設置了transition
,那麼它屬性改變的時候就會呈現出動畫狀態
transition: property duration timing-function delay
transition-property
:設置過渡的屬性
transition-duration
:設置過渡的時間
transition-timing-function
:設置過渡的運動方式
linear
(勻速). ease
(緩衝運動)transition-delay
:設置動畫的延遲
PS:通常都這麼用:transition: all 500ms ease;
看個例子:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>過渡動畫</title> <style> div { width: 300px; height: 300px; margin: 0 auto; background-color: black; /* 設置了transition,那div任何屬性的改動都會有過渡動畫 */ transition: all 1s ease; } div:hover { background-color: yellow; } </style> </head> <body> <div></div> </body> </html>
圖示:
只有一個屬性是沒有過渡動畫的:display:none
==》 display:block
看個區分案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>特殊案例</title> <style> div { width: 300px; height: 300px; margin: 0 auto; background-color: black; /* 設置了transition,那div任何屬性的改動都會有過渡動畫 */ transition: all 500ms ease; } /* 第一個盒子使用visibility的方式顯示和隱藏 */ div:first-child { visibility: hidden; } body:hover div:first-child { visibility: visible; background-color: yellow; } /* 第二個盒子使用display的方式顯示和隱藏 */ div:last-child { display: none; } body:hover div:last-child { display: block; background-color: yellow; } </style> </head> <body> <div>visibility</div> <div>display</div> </body> </html>
圖示:(visibility
和display
的區別不用說了吧?無非是一個佔空間一個不佔)
經驗:若是多個變換一塊兒用的,那麼順序爲:從後往前執行
**
transform
必定要加初始值**(否則在邊角的時候有小問題)
transform: translate(x軸,y軸);
transform: translate(100px,100px);
transform: scale(x,y);
transform: scale(0.5,1);
transform: rotate(60deg);
deg
是角度transform: skew(x軸,y軸);
transform: skew(30deg,30deg);
看個案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>2d變換</title> <style> div { width: 200px; height: 200px; float: left; border: 1px solid black; background-color: yellow; /* 添加一個過渡動畫 */ transition: all 1s ease; } /* x軸平移50px,y軸平移100px */ div:first-child:hover { background-color: green; /* 逗號別忘記了 */ transform: translate(50px, 100px); } /* x軸放大1.1倍,y軸放大2倍 */ div:nth-child(2):hover { transform: scale(1.1, 2) } /* 旋轉90度 */ div:last-child:hover { transform: rotate(90deg) } </style> </head> <body> <div></div> <div></div> <div></div> </body> </html>
圖示:(PS:CSS3樣式都不會改變盒模型,這個案例就能夠看出了)
這兩個是視覺更好體驗的參數:(推薦
)
transform-style:preserve-3d
:容許子元素呈現3d視圖perspective
:設置透視距離
transform: perspective(800px);
其餘參數:
tranform-origin
:設置變形的中心點backface-visibility
:設置盒子背面是否可見演示案例:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>3d變換</title> <style> .grandfather { width: 300px; height: 300px; margin: 300px auto; background-color: rgba(255, 0, 0, 0.3); /* 容許子元素呈現3d視圖 */ transform-style: preserve-3d; } .parent { width: 100%; height: 100%; background-color: rgba(0, 255, 0, 0.3); /* 設置初始值 */ transform: perspective(0) translateZ(0); /* 容許子元素呈現3d視圖 */ transform-style: preserve-3d; /* 設置一個補間動畫 */ transition: all 1s ease; } .child { width: 100%; height: 50%; background-color: rgba(0, 255, 255, 0.3); /* 設置初始值 */ transform: perspective(0) translateZ(0); /* 設置一個補間動畫 */ transition: all 1s ease; } .parent:hover { /* z軸移動200px(鋪面而來) */ transform: perspective(800px) translateZ(200px); } .child:hover { /* z軸移動200px(鋪面而來) */ transform: perspective(800px) translateZ(100px); } </style> </head> <body> <div class="grandfather"> <div class="parent"> <div class="child"></div> </div> </div> </body> </html>
圖示:
PS:更多複雜功能,若是感興趣能夠看看WebGL
animation基本上不太用,複雜的動畫我通常使用:Animate.css
,簡單的動畫transition
+transform
就夠了,若是是滾動動畫之類的我通常用scrollreveal.js
animation:動畫名稱 動畫時間 動畫曲線 延遲時間 播放次數 是否反向運動 動畫停留在哪一幀;
animation: move_search 1s linear 0.5s forwards;
(動畫名稱 動畫時間 動畫曲線 延遲時間 停留在最後一幀)@keyframes
:定義關鍵幀動畫animation-name
:動畫名稱animation-duration
:動畫時間animation-timing-function
:動畫曲線
linear
(勻速)、ease
(緩衝)、steps
(作幀動畫的步數
)animation-delay
:動畫延遲animation-iteration-count
:動畫播放次數
animation-direction
:動畫結束後是否反向還原
animation-play-state
:動畫狀態
animation-fill-mode
:動畫先後的狀態
PS:若是設置動畫集使用的是百分比,那麼這個百分比是指相對整個動畫的執行時間
簡單看個案例就收工吧:
此次先看效果:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>animate動畫</title> <style> body { width: 100%; height: 100%; background-color: rgb(22, 186, 157); } header { width: 50%; margin: 100px auto; } .search { height: 50px; border: 1px solid white; /* 水平陰影位置,垂直陰影位置,模糊距離,顏色 */ box-shadow: 0px 0px 10px #fff; /* 爲動畫準備的相對定位 */ position: relative; /* animation:動畫名稱 動畫時間 動畫曲線 停留在最後一幀 */ animation: move_search 1s linear forwards; } /* 最簡單的移動,也能夠百分百來設置進度 */ @keyframes move_search { from { left: -200%; } to { left: 0; } } .search div { color: white; padding-left: 10px; /* 爲了居中 */ line-height: 50px; /* 爲了動畫而設置 */ width: 0; height: 50px; overflow: hidden; /* animation:動畫名稱 動畫時間 8幀顯示完 停留在最後一幀 延遲1s顯示 */ animation: show_font 1.5s steps(8) forwards 1s; } @keyframes show_font { 0% { width: 0; } 50% { width: 75px; } 100% { width: 130px; } } </style> </head> <body> <header> <div class="search"> <div>公衆號:逸鵬說道</div> </div> </header> </body> </html>
這個基本上用的前端框架都包含了(eg:bootstrap
),這邊簡單看下便可
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>彈性</title> <style media="screen"> ul { display: flex; } ul li { list-style: none; border: 1px solid black; background-color: yellow; } ul li:first-child { /* 權重爲1 */ flex: 1; } ul li:nth-child(2) { /* 權重爲2 */ flex: 2; /* 通常都配合min-寬高來使用 */ min-width: 100px; } ul li:last-child { /* 權重爲2 */ flex: 2; } </style> </head> <body> <ul> <li>1</li> <li>2</li> <li>3</li> </ul> </body> </html>
我的理解:規定一個最大或者最小的邊界值,超過這個值就對指定元素設置指定樣式
不難,看下demo裏面的code片斷就結束吧:
/* 屏幕小於等於1200px使用以下CSS(非寬屏) */ @media screen and (max-width: 1200px) { main { width: 95%; } /* 文章寬屏顯示 */ main article, .copyright { width: 100%; } /* 側邊欄隱藏 */ main aside { display: none; } /* 頁腳寬屏顯示 */ .copyright { width: 100%; min-width: auto; } }
注意一下:響應式看起來加載的東西少了(eg:移動端)但其實和PC端請求頁面大小同樣(該加載的仍是要加載,只是隱藏而已)
最後再提一下移動端的meta
,通常都這麼設置:寬度爲屏幕寬度,且不容許用戶縮放
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
Autoprefixer
自動給添加不一樣瀏覽器CSS3前綴有些CSS3在谷歌瀏覽器、火狐、IE的老版本中可能不是標準名稱,這時候能夠經過Autoprefixer
快速來兼容舊瀏覽器,以前寫過文章,能夠看看:
cssrem
自動轉換px到rem這個移動佈局用的比較多,使用的時候有一點須要注意一下:需先設置默認root字體大小!(通常設置爲20px)
PS:移動佈局通常按照這個尺寸的雙倍來設計:
開始以前先說下我準備的案例:https://github.com/lotapp/h5blog/
此次是真費了一番功法去準備了一個博客的案例,從
設計
(佈局是仿幾個模版的)到PC頁面
、再到響應式
移動端佈局(原本是準備直接出移動頁面的,後來一想,算了完全點吧,含iPad
和iPhone
)、再到移動端
H5頁面、最後再到小程序
頁面(在H5的基礎上簡單改了下)算是都弄了個demo
最後貼下效果圖(前端真的沒啥,忘了W3C查一下,我這邊整理一下也是爲了之後方便)
PC端演示:https://github.lesschina.com/html5/
這個動態演示的gif太大,你們本身訪問吧,這邊就貼一個靜態圖
ipad佈局:GIF渲染的時候有點小問題,能夠直接訪問查看效果
iPhone:GIF渲染的時候有點小問題,能夠直接訪問查看效果
移動端演示:http://github.lesschina.com/html5/mobil/
微信端演示:https://github.com/lotapp/H5Blog/tree/master/wechat
注意下圖片有時候會有黑邊框的問題,設置一下display:block
就能夠了
PS:本質就是行內塊間隙問題(後面會說的)
小程序方面注意點:
navigator
)是塊級元素,而a標籤原本是行內元素image
)在css中設置width:100%
是有點小問題的,還須要設置mode="widthFix"
(或者直接設置這個)其餘的沒什麼好說的了,一切盡在代碼中了~ https://github.com/lotapp/H5Blog/tree/master/mobil
(完)