以下整理二十五條在實際專案中反覆被驗證的 CSS 細節,多數都是那種「不知道也能寫,知道後少踩很多坑」的類型。
ul 標籤在 Mozilla 中預設帶有 padding,在 IE 中只有 margin 有值,所以定義列表時兩者都要處理。!important 分流:selector { 屬性: B !important; 屬性: A }。這是權宜之計,不是長久之計。background-image 實作,而非 list-style-image,控制力更好。background-color 作為圖片載入失敗時的後備。<h1><span>標題內容</span></h1> 保留結構、隱藏 span 內容。<script> 沒有 language 屬性,正確寫法是 <script type="text/javascript">。table { border-collapse: collapse; } 搭配 td { border: #000 solid 1px; },在 IE5~IE7 與新版 Firefox 皆通過測試。left: 50%,把它放進一個相對定位的容器再用負 margin 位移會更穩。overflow:hidden; text-overflow:ellipsis;。IE6 以上有效,Firefox 早期版本只會隱藏而不顯示省略號。<!--[if !IE]>...<![endif]-->。@font-face { font-family: name; src: url(url); }。name 是字型名稱(任何合法的 font-family 值),url 指向 OpenType 字型檔。a { color: red; } 時,等同替四種狀態都設了樣式;之後只需補上 a:hover 就能做出懸停效果。但如果你只寫了 a:link,一定要記得把其他三種狀態補齊。p { padding: 1px 2px 3px 4px },後來只需調整上 5px、下 6px,寫成 p.style1 { padding-top:5px; padding-right:6px; } 反而更好——不必重複宣告整組值,也不必回頭查原本的左右內距是多少。/* 中文段落兩端對齊 */
text-align: justify; text-justify: inter-ideograph;
/* 固定寬度單行截斷(IE5+,Firefox 僅隱藏) */
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
/* 固定寬度中文詞折行(IE5+,Firefox 不支援) */
table-layout: fixed; word-break: break-all;
/* 滑過顯示說明提示 */
<acronym title="要提示的文字" style="cursor:help;">文字</acronym>
/* 圖片半透明(IE 私有屬性) */
.halfalpha { background-color: #000000; filter: Alpha(Opacity=50); }
/* 圖片懸停變亮 */
.pictures img { filter: alpha(opacity=45); }
.pictures a:hover img { filter: alpha(opacity=90); }
最後一項是 Flash 透明:在 </object> 前加上 <param name="wmode" value="transparent">;針對 Firefox 則需在 <embed> 上補 wmode="transparent"。
這批技巧中有一部分屬於特定瀏覽器的私有實作,用在今日專案時請當作漸進增強,並以標準屬性作為主要方案。