以下依主題整理日常開發中反覆遇到的 CSS 難題與解法。
html, body { height: 100%; margin: 0; }
#test { height: 100%; }
html, body { height: 100%; margin: 0; }
#test {
position: absolute;
top: 10px; right: 10px; bottom: 10px; left: 10px;
}
body { text-align: center; }
#test { width: 960px; margin: 0 auto; text-align: left; }
把 body 的 text-align 設為 center,可以讓連 IE5.5 都能正確居中;再用 margin: 0 auto 完成標準模式下的居中,並在容器內把文字對齊還原為 left。
#test {
position: absolute;
top: 50%; left: 50%;
width: 200px; height: 200px;
margin: -100px 0 0 -100px;
}
原理是先移到中心,再用負 margin 補回自身尺寸的一半。
#test {
display: table-cell;
width: 200px; height: 200px;
text-align: center; vertical-align: middle;
*display: block; *position: relative; /* IE6/7 hack */
}
#test p { *position: absolute; *top: 50%; *left: 50%; margin: 0; }
#test p img { *position: relative; *top: -50%; *left: -50%; vertical-align: middle; }
現代方案可直接用 flex:display:flex; align-items:center; justify-content:center;。
#test { height: 25px; line-height: 25px; }
讓行高與容器高度相等即可。
絕大多數情況是子元素浮動後脫離了文件流,父容器高度塌陷所致。除錯時第一時間就該檢查是否有未清除的浮動。
/* 方法一:在浮動元素的下一個兄弟元素上清除 */
#test { clear: both; }
/* 方法二:在父元素上觸發 BFC */
#test { display: block; zoom: 1; overflow: hidden; }
/* 方法三:偽元素清除(推薦) */
#test { zoom: 1; }
#test::after {
content: '';
display: block;
clear: both;
visibility: hidden;
height: 0;
}
.box1 { margin: 10px 0; }
.box2 { margin: 20px 0; }
實際間距是 20px,而非 30px,這稱為外邊距合併(margin collapse),取兩者中的最大值。父子元素之間同樣會發生。幾點注意事項:
visible 的 overflow 的區塊元素,不會與其子元素合併。img { display: block; } /* 方法一 */
img { vertical-align: top; } /* 方法二 */
#test { font-size: 0; line-height: 0; } /* 方法三,設在父元素上 */
原因是圖片預設為行內元素,會依基線對齊而在下方留出預留空間。
/* 方法一:容器與內容並列,用負 margin 疊上去 */
.outer { width: 200px; height: 200px; background: #000; filter: alpha(opacity=20); opacity: .2; }
.inner { width: 200px; height: 200px; margin-top: -200px; }
/* 方法二:現代瀏覽器直接用 rgba,舊 IE 讓子元素相對定位 */
.outer {
width: 200px; height: 200px;
background: rgba(0, 0, 0, .2);
background: #000\9;
filter: alpha(opacity=20)\9;
}
.outer .inner { position: relative\9; }
關鍵觀念:opacity 會連帶影響所有子元素,因此若只想讓背景半透明,優先用 rgba() 指定背景色。
/* 方法一:合併相鄰邊框 */
#test { border-collapse: collapse; border: 1px solid #ddd; }
#test th, #test td { border: 1px solid #ddd; }
/* 方法二:用間距加背景色模擬 */
#test { border-spacing: 1px; background: #ddd; }
#test tr { background: #fff; }
IE7 及更早版本不支援 border-spacing,可用 table 的 cellspacing 屬性替代。
body { line-height: 1.5; }
注意不要加上單位,寫成倍數才能讓行高隨字級自動縮放。
#test { width: 150px; white-space: nowrap; }
#test {
width: 150px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
三個屬性缺一不可:先強制單行,再截斷溢出內容,最後指定截斷處顯示省略號。
#test { width: 150px; word-wrap: break-word; }
標準模式與怪異模式(Quirks mode)下的寬度計算不同:
標準模式:element width = width + padding + border 怪異模式:element width = width
如今可用 CSS3 的 box-sizing: border-box 主動指定想要哪一種計算方式。
.test {
width: 200px; height: 50px;
text-indent: -9999px;
background: #eee url(icon.png) no-repeat;
}
結構簡潔,但缺點明顯:負值隨情境而異不易抽成通用樣式;元素為連結時非 IE 瀏覽器的虛線框會不完整;設為行內或行內區塊時跨瀏覽器差異較大。
.test { width: 200px; height: 50px; background: #eee url(icon.png) no-repeat; }
.test span { visibility: hidden; } /* 或 display: none */
相容性最好、最容易抽成通用類別,代價是 HTML 結構多一層。
.test {
overflow: hidden;
width: 200px; height: 0; padding-top: 50px;
background: #eee url(icon.png) no-repeat;
}
結構簡潔,但 padding 數值不易通用,且要兼顧 IE5.5 及更早版本還得加 hack。
.test {
overflow: hidden;
width: 200px; height: 50px;
font-size: 0; line-height: 0;
color: rgba(0, 0, 0, 0);
background: #eee url(icon.png) no-repeat;
}
結構簡單、易於複用,是目前最實用的做法。不過在 SEO 與無障礙角度上,更推薦使用 CSS 的 background 搭配 HTML5 的 <img> + alt,或善用 clip-path 這類標準手法。
a { outline: none; }
a:link { color: #03c; }
a:visited { color: #666; }
a:hover { color: #f30; }
a:active { color: #c30; }
順序必須是 LoVe HAte(link-visited-hover-active),這個口訣正好幫你記住順序不能顛倒。
#test { cursor: pointer; }
寫成 hand 只有 IE 與 Opera 支援,且屬非標準值。
input { vertical-align: middle; }
span { display: block; width: 200px; height: 100px; }
只要把行內元素轉為區塊級或行內區塊級即可,除了 display,float 與 position 同樣能達到效果。
html {
scrollbar-3dlight-color: #999;
scrollbar-darkshadow-color: #999;
scrollbar-highlight-color: #fff;
scrollbar-shadow-color: #eee;
scrollbar-arrow-color: #000;
scrollbar-face-color: #ddd;
scrollbar-track-color: #eee;
scrollbar-base-color: #ddd;
}
把原本寫在 body 上的捲軸樣式移到 html 選擇器即可。
別用 list-style-image,改用 background-image 搭配 background-position 定位,控制力更精確。
.a { color: #f00; }
.b { background: #eee; }
.c { background: #ccc; }
<div class="a b c">測試</div>
多個 class 之間以空格分隔,且只有 class 支援這種寫法,id 不行。
在撰寫當時的測試環境(IE6–IE10、Firefox 6.0、Chrome 13.0、Safari 5.1、Opera 11.51)中觀察到:
:before/:after 加在 <input> 上,除 Opera 外皆無效果,而 Opera 的表現也與預期不同。:before/:after 無法定義 transition 與 animation,只有 Firefox 支援。原因是偽元素作用在取代型元素(如 input、img)上並不符合規範。若效果是必要需求,請改用真實元素,或把樣式掛在 input 的外層容器上。