集腋成裘-03-css基礎-02
1.1 三種寫法
內嵌式:樣式只作用於當前文件,沒有真正實現結構表現分離
外鏈式:作用範圍是當前站點,真正實現了內容與表現分離
行內樣式:僅限於當前標簽,結構混在一起
1.2 標簽分類
1.2.1 塊元素
代表標簽:Div;p;ul;li
特點:獨占一行;可設置寬高;子元素的寬高默認為父元素的寬高
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> div{ width:200px; height:200px; background-color: blue } p{ background-color: red; } </style> </head> <body> <div> div元素是塊元素 <p>p元素的默認寬高等於父級的寬度</p> </div> <p>p元素也是默認瀏覽器寬度</p> </body> </html>
1.2.2 行內樣式
代表標簽:span;a;strong;
特點:一行顯示;不可設置寬高;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> span{ width:200px; height:200px; background行內元素-color: blue } a{ width:200px; height:200px; background-color: red; } </style> </head> <body> <span> 我是span標簽中的元素<a href="#">a標簽</a> </span> <span> 不能設置寬高 </span> <a href="#"> 不能設置寬高 </a> </body> </html>
1.2.3 行內塊
代表標簽:input;img;
特點:一行顯示;可設置寬高;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> input{ width:200px; height:200px; background-color: blue } img{ width:200px; height:200px; background-color: red; vertical-align: top; } </style> </head> <body> <input type="text" name="" value="我是input標簽中的元素"> </span> <img src="1.png" alt=""> </body> </html>行內塊
1.3 互相轉換
1.3.1 塊=>行內
display:inline;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> div{ width:200px; height:200px; background-color: blue; } p{ background-color: red; display:inline; } </style> </head> <body> <div> div元素是塊元素 <p>p元素的默認寬高等於父級的寬度</p> </div> <p>p元素也是默認瀏覽器寬度</p> </body> </html>塊轉化為行內
1.3.2 行內=>塊
display: block;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> span{ width:200px; height:200px; background-color: blue; display: block; } a{ width:200px; height:200px; background-color: red; } </style> </head> <body> <span> 我是span標簽中的元素<a href="#">a標簽</a> </span> <span> 不能設置寬高 </span> <a href="#"> 不能設置寬高 </a> </body> </html>行內轉化為塊
1.3.3 =>行內塊
display:inline-block;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> div{ width:400px; height:400px; background-color: blue; } p{ width:200px; height:200px; background-color: red; display:inline-block; } </style> </head> <body> <div> div元素是塊元素 <p>p元素的默認寬高等於父級的寬度</p> </div> <p>p元素也是默認瀏覽器寬度</p> </body> </html>塊轉化為行內塊
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> span{ width:400px; height:400px; background-color: blue; display:inline-block; } a{ width:200px; height:200px; background-color: red; display:inline-block; } </style> </head> <body> <span> 我是span標簽中的元素<a href="#">a標簽</a> </span> <span> 不能設置寬高 </span> <a href="#"> 不能設置寬高 </a> </body> </html>行內轉化為行內塊
1.4 樣式表的屬性
1.4.1 層疊性 :當元素之間的樣式發生沖突時,按照瀏覽器的解析順序,後續的樣式會覆蓋掉原有的樣式.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .div1{ color:red; } .div2{ color:blue; } </style> </head> <body> <div class="div1 div2"> 我是div中元素 </div> </body> </html>層疊性
1.4.2 繼承性:包含關系,子元素中的某些元素會繼承父元素的樣式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> p{ font-style: italic; } div{ font:italic 700 16px/40px 微軟雅黑 ; color:red; } </style> </head> <body> <div> 我是另一個div中元素 <p>我是div中的p標簽我是紅色</p> </div> </body> </html>繼承性
1.4.3 優先級
默認(繼承)<標簽<類<id<行內<!important
0 < 10<100<1000<1000+
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #p2{ color:green; } #p{ color:pink; } .p{ color:orange; } p{ color:blue; } div{ color: red; } .p2{ color:orange !important; } </style> </head> <body> <div > 我默認是黑色; <p>我是默認是繼承div的黑色</p> <p>標簽權重大於繼承</p> <p class="p">類的權重大於標簽</p> <p class="p" id="p">id權重大於類</p> <p class="p" id="p2" style="color:yellow">行內權重大於id</p> </div> <div class="father"> <p class="p2">最厲害得是!important</p> </div> </body> </html>優先級
權重的疊加
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .father,.p2{ color:red; } div,.p{ color:orange; } </style> </head> <body> <div class="father"> <p class="p2">紅色表示連個類的疊加</p> </div> </body> </html>權重的疊加
1.5 鏈接偽類
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> a:link{ color:red; text-decoration: none; } /*訪問之後的狀態*/ a:visited{ color:yellow; } /*鼠標放上的狀態*/ a:hover{ color:orange; } /*激活狀態*/ a:active{ color:pink; } </style> </head> <body> <div class="content"> <a href="#">鏈接</a> </div> </body> </html>鏈接偽類
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .nav{ height: 60px; background-color: #aaa; text-align: center; } a:link{ color:red; text-decoration: none; } /*訪問之後的狀態*/ a:visited{ color:yellow; } /*鼠標放上的狀態*/ a:hover{ background-color: #eee; color: #F14400; } /*激活狀態*/ a:active{ color:pink; } a{ display: inline-block; width:100px; height: 60px; } a.public{ font-weight: 700; color: #F14400; } </style> </head> <body> <div class="nav"> <a href="#" class="public">天貓</a> <a href="#" class="public">聚劃算</a> <a href="#" class="public">超市</a> <a href="#" class="public">頭條</a> <a href="#">阿裏旅行</a> <a href="#">電器城</a> <a href="#">淘搶購</a> <a href="#">蘇寧易購</a> <a href="#">智能生活</a> </div> </body> </html>小練習
1.6 背景屬性
/*背景顏色*/
background-color:red;
/*背景圖片*/
background-image:url("");
/*背景平鋪*/
background-repeat:repeat | no-repeat | repeat-x | repeat-y;
/*背景定位*/
background-position: left | right | center | top | bottom
/*背景滾動*/
Background -attachment scroll | fixed;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .box{ height:500px; /*背景顏色*/ background-color:red; /*背景圖片*/ background-image:url("1.png"); /*背景平鋪*/ background-repeat:no-repeat; /*背景定位*/ background-position:center; /*背景滾動*/ background-attachment: fixed; } </style> </head> <body> <div class="box"> </div> <div Style="height:1000px;background-color:blue"> </div> </body> </html>背景顏色
1.6.2 背景屬性的連寫
background: red url("1.png") no-repeat center scroll;
1.6.3 練習
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> input{ width:200px; height: 20px; background: url("search.png") no-repeat right center; } </style> </head> <body> <input type="text" value="請輸入關鍵字"> </body> </html>練習1
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> li{ list-style: none; background: url("li.gif") no-repeat left center; text-indent: 1em; } </style> </head> <body> <div class="nav"> <ul> <li><a href="#">阿裏旅行</a></li> <li><a href="#">電器城</a></li> <li><a href="#">淘搶購</a></li> <li><a href="#">蘇寧易購</a></li> <li><a href="#">智能生活</a></li> </ul> </div> </body> </html>練習2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> a{ display: inline-block; width: 67px; height: 32px; background: url("shoppingCar.png"); } a:hover{ background: url("shoppingCar.png") bottom; } </style> </head> <body> <div class="nav"> <a href="#"></a> </div> </body> </html>練習3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> a{ display: inline-block; width: 120px; height: 58px; text-align: center; text-decoration: none; color:white; line-height: 50px; } .one{ background: url("images/bg1.png"); } .one:hover{ background: url("images/bg5.png"); } .two{ background: url("images/bg2.png"); } .three{ background: url("images/bg3.png"); } .four{ background: url("images/bg4.png"); } </style> </head> <body> <a href="#" class="one">五彩導航</a> <a href="#" class="two">五彩導航</a> <a href="#" class="three">五彩導航</a> <a href="#" class="four">五彩導航</a> </body> </html>練習3
集腋成裘-03-css基礎-02