萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 常見屬性縮寫與全寫對照表:寫出更精簡的樣式碼

善用 CSS 縮寫屬性能大幅減少程式碼量,提升可讀性與維護性。以下整理七大類最常用的縮寫規則與順序。

1. font 字體綜合設定

/* 縮寫 */
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 必須以 / 連接,不可分開書寫。

2. background 背景綜合設定

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

3. margin / padding 邊距與內距

margin: 1px 0 2em -20px;
/* 上 | 右 | 下 | 左(順時針) */
  • 四值:top right bottom left
  • 三值:top right/left bottom(左 = 右)
  • 兩值:top/bottom right/left
  • 一值:四邊相同

padding 規則完全相同。

4. border 邊框

border: 1px solid #000;
/* 寬度 | 樣式 | 顏色 */

單邊框同理:border-top: 1px solid #000;

5. list-style 列表樣式

list-style: square outside url(bullet.gif);
/* type | position | image */

6. 四邊值順序口訣

「上右下左」順時針,適用於 margin、padding、border-width、border-color 等所有四邊屬性。

7. 顏色縮寫

  • #FFF = #FFFFFF = white
  • #06C = #0066CC

只要每兩位數值相同即可縮寫為三位十六進制。

實務建議

  • 團隊專案建立統一縮寫規範,確保程式碼風格一致。
  • 除非為了極致壓縮,否則保留適度空白與換行利於閱讀。
  • 使用建構工具(如 PostCSS、cssnano)自動處理生產環境壓縮。
copyright © 萬盛學電腦網 all rights reserved