CSS 技巧的價值在於用少量程式碼解決常見問題。以下整理常用做法,涵蓋文字、表單、圖片、版面與相容性。
- 用 ::selection 與 ::-moz-selection 改變選取文字的背景與顏色。
- html { overflow-y: scroll; } 可避免 Firefox 滾動條跳動。
- page-break-before: always 可控制列印分頁。
- !important 可暫時提高樣式優先權。
- text-indent 搭配背景圖,可用圖片替換文字。
- min-height 搭配舊 IE 的 height,可模擬最小高度。
- 利用 target 屬性與偽元素,可標示新視窗連結。
- 調整 ol 的字體與顏色,可美化序號。
- p:first-letter 可做出首字下沉。
- opacity 與 filter 可處理透明度。
- line-height 可讓單行文字垂直居中。
- margin:0 auto 可讓固定寬度區塊水平居中。
- textarea { overflow:auto; } 可移除 IE 垂直滾動條。
- a:active 與 a:focus 的 outline:none 可移除虛線框。
- position:relative 可解決部分 IE 元素消失問題。
- 依副檔名設定背景圖,可為不同連結加上圖示。
- cursor:pointer 可讓元素呈現手型。
- text-transform: capitalize 可將單字首字母大寫。
- font-variant: small-caps 可做出小型大寫字母。
- input:focus 可高亮輸入框。
- a img { border:none; } 可移除圖片邊框。
- 用 label 搭配輸入框,可做出無表格表單。