前幾天被朋友問到幾個 CSS 問題,自以為摸索 CSS 也有三年半,算是熟門熟路,結果還是被問得一臉茫然。正好剛進新公司事務不多,於是翻出《CSS 權威指南》重新做一次基礎複習。以下整理幾個我認為容易混淆的知識點(範圍限於本書內容)。
類選擇器的基本寫法:
.your-class{/*...*/}
類選擇器的嵌套選擇(後代選擇):
.first-class .second-class{/*...*/}
多類選擇器:
.first-class.second-class{/*...*/}
一級子元素選擇:
.first-class > .second-class{/*...*/}
用程式碼對照三者差異:
<style> .first-style.second-style{ color: blueviolet} .first-style .third-style{ font-style: italic} .first-style > .fourth-style{ font-weight: bold} </style> <div class="first-style second-style">HELLO</div> <div class="first-style third-style">hello</div> <div class="first-style"><div class="second-style">HELLO</div></div> <div class="first-style"><div class="third-style">hello</div></div> <div class="first-style"><div><div class="third-style">Hello World</div></div></div> <div class="first-style"><div class="fourth-style">Hello World</div></div> <div class="first-style"><div><div class="fourth-style">Hello World</div></div></div>
結論:
· 嵌套選擇選中的是 first-style 之下所有層級中包含 second-style 的後代元素;
· 一級子元素選擇只選 first-style 的直接子代,再往內嵌套的不算;
· 多類選擇器選中的是同時帶有 first-style 與 second-style 兩個類名的元素。
優先級可寫成 0.0.0.0 這樣的四段形式。注意:左邊一段的權重永遠大於右邊,請忽視各段數值本身的絕對大小。例如 0.0.1.0 大於 0.0.0.12。
計算規則:
· 每個 ID 屬性值,加 0.1.0.0;
· 每個類屬性值、屬性選擇或偽類,加 0.0.1.0;
· 每個元素與偽元素,加 0.0.0.1;
· 結合符與通配符對優先級沒有貢獻。
div{ color: black} /* 0.0.0.1 */ div p{ color:black} /* 0.0.0.2 */ .my-div{ color:black} /* 0.0.1.0 */ div.my-div{ color: black} /* 0.0.1.1 */ .my-div .my-p{ color: black} /* 0.0.2.0 */ .my-div p.my-p{ color: black} /* 0.0.2.1 */ div.my-div p.my-p{ color: black} /* 0.0.2.2 */ #div-id{ color: black} /* 0.1.0.0 */
那麼四段中最左邊那一格永遠用不到嗎?並非如此——它保留給行內樣式宣告,特殊性高於其他任何宣告:
<div style="/*...*/"></div> <!--1.0.0.0-->
它寫在樣式宣告之後、分號之前,本身並沒有對應的優先級數值,而是直接跳脫整個優先級體系:
<style> .impt{color: black !important} </style> <div class="impt" style="color:red">hello world</div>
結論:!important 宣告的重要性超過所有其他宣告,連行內樣式也不例外。
正常流:西方語言文本由左至右、由上而下排列,也就是我們熟悉的傳統 HTML 文件流。要讓元素脫離正常流,唯一方式就是讓它成為浮動或定位元素。
非替換元素:內容直接包含在文件中,如 <p></p>。
替換元素:作為其他內容的佔位符,如 <img />、<input />。
塊級元素:在正常流中會在自身前後產生換行,可用 display:block 產生塊級框。
行內元素:不會產生換行,通常是塊級元素的後代,可用 display:inline 產生行內框。
1. 垂直方向會合併。相鄰的上下外邊距會合併,取兩者中較大的一個(也就是重疊)。
<style> .first-div{ margin-bottom:50px;} .second-div{ margin-top:20px;} </style> <div class="first-div">this is div-1</div> <div class="second-div">this is div-2</div>
2. 書寫順序
.your-class{ margin:<top> <right> <bottom> <left> }
記憶方式:從 12 點方向出發,順時針繞一圈。
縮寫規則:缺少左邊用右邊;缺少下邊用上邊;缺少右邊用上邊。
.first-margin{ margin: 50px;} /* 50px 50px 50px 50px */ .second-margin{ margin: 50px 20px ;} /* 50px 20px 50px 20px */ .third-margin{ margin: 50px 30px 10px;} /* 50px 30px 10px 30px */
3. 套用在行內元素上:margin 對行內元素只有水平方向有效,垂直方向不產生影響。
<style> .mar-strong{ margin:20px 0} </style> <div> <strong class="mar-strong">hello world</strong>
順帶一提,padding 套用在行內元素上時同樣是水平有效、垂直無效(在未設定 background 的情況下)。
<style> .mar-strong{ padding:20px 0px; background: red} </style> <p> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa <strong class="mar-strong">hello world</strong> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa </p>
把 padding 或 background 拿掉對照看看,就能驗證這個結論。
background: scroll || fixed || inherit,初始值為 scroll。
<style> .div-bg{ width: 100%; height: 3000px;} .bg-attachment{ background-image: url(img/1.jpg); background-attachment: fixed; background-repeat: no-repeat; background-position: center} </style> <div class="div-bg bg-attachment"></div>
設為 fixed 之後,滾動頁面時背景圖會固定在視窗中不隨內容移動。
· CSS 允許所有元素浮動;
· 浮動元素之間的外邊距不會合併;
<style> .div-float{float: left;margin: 50px} </style> <div class="div-float">HELLO WORLD</div> <div class="div-float">hello world</div>
· 浮動元素一律產生塊級框,不論它本身是什麼元素。
<style> .span{ margin: 50px} .span-float{ float: left;} </style> <span class="span span-float">HELLO WORLD</span> <span class="span span-float">hello world</span>
清除浮動由 clear 屬性完成:clear: left || right || both || none || inherit,初始值 none。left 表示左側不允許有浮動元素,right 表示右側不允許,both 表示兩側都不允許。
<style> .div-mar{ width: 100px;padding: 50px} .div-red{ background: red} .div-yellow{ background: yellow} .div-float-left{ float: left} .div-float-right{ float: right} .div-clear-both{ clear: both} .div-clear-left{ clear: left} .div-clear-right{ clear: right} </style> <div class="div-mar div-red div-float-left">HELLO WORLD</div> <div class="div-mar div-yellow div-float-left div-clear-right">Hello World</div>
position: static || relative || absolute || fixed || inherit,初始值 static。
static:正常生成元素框,不做任何偏移。
relative:元素偏移一段距離,但仍保留原本佔據的空間與形狀。
absolute:元素從文件流中完全移除,並相對其包含塊定位。
fixed:表現類似 absolute,但包含塊是視窗本身。
relative / absolute / fixed 定位的同時,會為其後代元素建立一個包含塊。
什麼是包含塊?在 HTML 中根元素就是 html 元素。
· 根元素的包含塊由使用者代理(瀏覽器)建立;
· 非根元素若 position 為 relative 或 static,包含塊由最近的塊級框、表格儲存格或行內塊祖先框的內容邊界構成;
· 非根元素若 position 為 absolute,包含塊為最近的 position 值不是 static 的祖先元素。
設為 visibility:hidden 時元素不可見,但仍佔據文件流中的位置,會影響布局。
設為 display:none 時元素不顯示,並從文件流中移除,不再佔用任何空間,這也是常見的渲染優化手段。
<style> .div-content:before{ content: "[ "attr(value)" ] "} .div-content:after{content:" hello world"} </style> <div class="div-content" value="H">ello World</div>
content 可以插入字串,也能透過 attr() 讀取元素的屬性值,這在製作裝飾性符號或無障礙補充說明時很實用。
<style> .div-out{width: 400px; height: 200px; background: black;border-bottom: 1px solid white; position: relative;} .div-index-1{ width: 200px; height: 100px; background: red; position: absolute; bottom: -50px; z-index: 1} .div-index-2{ width: 200px; height: 100px; background: yellow; position: absolute; top: -50px; left: 20px; z-index: 1} </style> <div class="div-out"> <div class="div-index-1"></div> </div> <div class="div-out"> <div class="div-index-2"></div> </div>
結論:不受影響。兩組元素分別屬於不同父元素建立的層疊上下文,彼此不會比較層級。
新手筆記,若有錯誤歡迎交流指正。