善用 CSS 縮寫屬性能大幅減少程式碼量,提升可讀性與維護性。以下整理七大類最常用的縮寫規則與順序。
/* 縮寫 */
font: italic small-caps bold 12px/1.5em Arial, Verdana;
/* 等效全寫 */
font-style: italic;
font-variant: small-caps;
font-weight: bold;
font-size: 12px;
line-height: 1.5em;
font-family: Arial, Verdana;
順序:font-style | font-variant | font-weight | font-size | line-height | font-family
注意:font-size 與 line-height 必須以 / 連接,不可分開書寫。
background: #fff url(bg.gif) no-repeat fixed left top;
/* 等效 */
background-color: #fff;
background-image: url(bg.gif);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: left top;
順序:color | image | repeat | attachment | position
margin: 1px 0 2em -20px;
/* 上 | 右 | 下 | 左(順時針) */
top right bottom lefttop right/left bottom(左 = 右)top/bottom right/leftpadding 規則完全相同。
border: 1px solid #000;
/* 寬度 | 樣式 | 顏色 */
單邊框同理:border-top: 1px solid #000;
list-style: square outside url(bullet.gif);
/* type | position | image */
「上右下左」順時針,適用於 margin、padding、border-width、border-color 等所有四邊屬性。
#FFF = #FFFFFF = white#06C = #0066CC只要每兩位數值相同即可縮寫為三位十六進制。