CSS 之所以強大,在於它能控制的屬性既多且細。本文以分類方式整理 Dreamweaver 中常用的 CSS 屬性,並標示哪些可繼承、哪些不繼承,方便你查表時一目了然。
說明:√ 代表屬性會繼承,× 代表不繼承;方括號 [ ] 標示適用的元素類型,未標出者表示適用於所有元素。
這組屬性大多會繼承,包含 font-family(字型)、font-size(字級,可用 9、10、12、14 等數值或 xx-small 到 xx-large、smaller、larger 等關鍵字)、color(顏色)、font-style(normal、italic、oblique)、line-height(行高)、font-weight(粗細,normal 到 900)、text-transform(capitalize、uppercase、lowercase、none)、font-variant(normal、small-caps)等。其中 text-decoration(none、underline、overline、line-through、blink)不繼承,font 為字體的綜合設定。
這組屬性皆不繼承,包含 background-color(背景色)、background(綜合設定)、background-image(背景圖)、background-attachment(fixed、scroll)、background-repeat(no-repeat、repeat、repeat-x、repeat-y)、background-position(背景位置)等。
word-spacing(字距)與 letter-spacing(字距)會繼承;vertical-align(垂直對齊,如 baseline、sub、super、top、middle、bottom)、text-align(水平對齊,如 left、right、center、justify)、text-indent(首行縮排)、white-space(空白處理,normal、pre、nowrap)、display(inline、block、list-item 等)則不繼承。
這組皆不繼承,包含 border(綜合設定)、border-top/right/bottom/left、border-color、border-style(none、solid、dashed、dotted、double、groove、ridge、inset、outset)、border-width(thin、medium、thick)等,能精細控制每個邊的樣式。
這組皆不繼承,包含 width、height(可用 auto、px、pt、em、% 等單位)、float(left、right、none)、clear(left、right、both、none)、margin 及其四向、padding 及其四向等,是控制盒模型與布局的核心。
list-style-type(disc、circle、square、decimal、lower-roman 等)、list-style-image、list-style-position(inside、outside)皆不繼承,且僅適用於 display 值為 list-item 的元素。position(absolute、relative、static)與 visibility 則負責元素的定位與可見性控制。
把這些屬性分類記牢,日後撰寫或除錯 CSS 時便能事半功倍,也能更從容地判斷某段樣式為何會「莫名其妙」地套用到子元素上——答案往往就藏在「是否繼承」這條線索裡。