CSS初學(xué)感覺很簡單,但隨著學(xué)習(xí)的深入才感覺CSS的水由多深,平常總會遇到各種坑,先總結(jié)一些經(jīng)常遇到的坑
大小寫不敏感
雖然我們平時在寫CSS的時候都是用小寫,但其實CSS并不是大小寫敏感的
.test{ background-COLOR:#a00; width:100px; height: 100px;}
雖然把background-color寫為了background-COLOR,但仍然會生效,之所以寫成小寫是因為xhtml標(biāo)準(zhǔn)的關(guān)系,但是即使不是xhtml還是寫成小寫比較好,美觀、易讀而且可以應(yīng)對可能的轉(zhuǎn)換需求
選擇器優(yōu)先級
當(dāng)兩個規(guī)則都作用到了同一個html元素上時,如果定義的屬性有沖突,那么應(yīng)該用誰的值的,CSS有一套優(yōu)先級的定義。
不同級別
在屬性后面使用 !important 會覆蓋頁面內(nèi)任何位置定義的元素樣式。
作為style屬性寫在元素內(nèi)的樣式
同一級別
同一級別中后寫的會覆蓋先寫的樣式
上面的級別還是很容易看懂的,但是有時候有些規(guī)則是多個級別的組合,像這樣
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> div.test{ background-COLOR:#a00; width:100px; height: 100px; } .test.test2{ background-COLOR:#0e0; width:100px; height: 100px; } </style></head><body> <div class="test test2"></div></body></html>
到底div是應(yīng)用那條規(guī)則呢,有個簡單的計算方法(經(jīng)園友提示,權(quán)值實際并不是按十進(jìn)制,用數(shù)字表示只是說明思想,一萬個class也不如一個id權(quán)值高)
我們可以把選擇器中規(guī)則對應(yīng)做加法,比較權(quán)值,如果權(quán)值相同那就后面的覆蓋前面的了,div.class的權(quán)值是1+10=11,而.test1 .test2的權(quán)值是10+10=20,所以div會應(yīng)用.test1 .test2變成綠色
行內(nèi)(inline)元素的一些屬性
并不是所有的屬性對行內(nèi)元素都能夠生效
<div style="background-color: #a44;"> <span style="padding:4px; margin:8px; height: 500px; width:1000px; background-color:#0e0;">123456789123456789</span> </div> <div style="background-color: #a44;"> <span style="padding:4px; margin:8px; height: 500px; width:1000px; background-color:#0a0;">123456789</span> </div>
通過例子可以看出,我們對span設(shè)置的width和height屬性并沒有生效,margin-top和margin-bottom無效,padding-top和padding-bottom會改變元素范圍(背景區(qū)域變大了),但并沒有影響下面元素位置
一些互斥的元素
font-size單位
我們在寫字體的尺寸的時候常用的單位有
這些字體分別有什么含義?
:checked 選擇器范圍
我們知道:checked會選擇被選中的checkbox和radio,看個例子
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> :checked{ margin: 10px; } </style></head><body> <input id="t1" type="checkbox" checked/> <input id="t3" type="radio" checked/> <select> <option id="t2">test</option> <option id="t4">test2</option> </select></body></html>
對于前兩個margin變成10px我們不奇怪,但是當(dāng)我們看select的option的時候會發(fā)現(xiàn)被選中的option的margin業(yè)變成了10px,沒有被選中的option則沒有變化!
是的:checked也會選擇被選中的option
并不是所有圖片都會被加載
我們知道寫在頁面上的img標(biāo)簽,無論顯示與否,圖片都會被加載(所以試圖通過對圖片display:none來達(dá)到節(jié)省網(wǎng)絡(luò)流量的做法就省省吧。。。),我們也經(jīng)常使用backgroung-image等css屬性為頁面添加圖片,這些圖片是不是一定會被加載呢,看個例子
<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> .useless{ background-image: url(images/0.jpg); } .hidden{ background-image: url(images/1.jpg); } .none{ background-image: url(images/2.jpg); } .parentHidden{ background-image: url(images/3.jpg); } .parentNone{ background-image: url(images/4.jpg); } </style></head><body> <div class="hidden"></div> <div class="none"></div> <div style="visibility:hidden;"> <div class="parentHidden"></div> </div> <div style="display:none;"> <div class="parentNone"></div> </div> <div style="display:none"> </div></body></html>
看一下網(wǎng)絡(luò)監(jiān)視情況(怎么柳巖的照片變小后感覺怪怪的。。。)
我們可以發(fā)現(xiàn)圖片0和4沒有被下載,0是沒有用到的CSS,4是父容器的display被設(shè)為none的情況,這兩種情況下的CSS引用的圖片是不會被加載的,而父容器設(shè)置visibility屬性為hidden仍然會加載圖片,不要搞混了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答