萬盛學電腦網

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

CSS 屬性縮寫與全寫完整對照表:font、background、margin、border、list-style 四邊順序規則

六大類屬性縮寫對照總表

1. font 字體縮寫

/* 縮寫 - 必須包含 font-size 與 font-family */
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;

/* 順序:style | variant | weight | size/line-height | family */

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;

/* 全寫對照 */
margin-top: 1px;
margin-right: 0;
margin-bottom: 2em;
margin-left: -20px;

/* 順序:top | right | bottom | left (順時針) */
/* padding 規則完全相同 */

4. border 邊框縮寫

/* 縮寫 - 四邊同樣式 */
border: 1px solid #000;

/* 全寫對照 */
border-width: 1px;
border-style: solid;
border-color: #000;

/* 單邊縮寫 */
border-top: 1px solid #000;
/* 展開:border-top-width | border-top-style | border-top-color */

5. list-style 列表縮寫

/* 縮寫 */
list-style: square outside url(bullet.gif);

/* 全寫對照 */
list-style-type: square;
list-style-position: outside;
list-style-image: url(bullet.gif);

/* 順序:type | position | image */

6. 四邊屬性通用規則(順時針:上右下左)

參數數語法展開結果
1v1四邊皆 v1
2v1 v2上下 v1、左右 v2
3v1 v2 v3上 v1、左右 v2、下 v3
4v1 v2 v3 v4上 v1、右 v2、下 v3、左 v4

適用屬性: margin, padding, border-width, border-color, border-radius 等所有四邊屬性。

7. 顏色縮寫

完整縮寫條件
#FFFFFF#FFF每兩位相同
#0066CC#06C每兩位相同
#336699#369每兩位相同
#ABCDEF不可縮寫每兩位不同

實戰避坑指南

❌ 錯誤:font-size 與 line-height 分開寫

/* 錯誤 */
font: bold 12px Arial, 1.5em sans-serif;

/* 正確:必須用斜槓連接 */
font: bold 12px/1.5em Arial, sans-serif;

❌ 錯誤:background-position 寫反

/* 錯誤:水平 垂直 顛倒 */
background: #fff url(img.jpg) no-repeat top left;

/* 正確:水平 垂直 */
background: #fff url(img.jpg) no-repeat left top;

❌ 錯誤:border-radius 縮寫順序誤解

/* 四個值:左上、右上、右下、左下 */
border-radius: 4px 8px 12px 16px;

/* 八個值(含 /):水平半徑 / 垂直半徑 */
border-radius: 4px 8px 12px 16px / 2px 4px 6px 8px;

自動化工具建議

  • Stylelint:規則 shorthand-property-no-redundant-values 檢測冗餘縮寫
  • PostCSS:postcss-merge-longhand 自動合併、postcss-expand 自動展開
  • IDE 片段:配置常用縮寫 Snippet 提升輸入效率
copyright © 萬盛學電腦網 all rights reserved