萬盛學電腦網

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

CSS 常用縮寫屬性完整速查表:盒模型、背景、字體、邊框、列表一網打盡

顏色值縮寫

十六進制每兩位相同可縮寫:#000000 → #000、#336699 → #369、#f2f2f2 無法縮寫。CSS Color 4 新增 #RGBA、#RRGGBBAA 8 位寫法。

盒模型縮寫

Margin / Padding(順時針:上右下左)

margin: 1px 2px 3px 4px;  /* 上右下左 */
margin: 1px 2px;            /* 上下 | 左右 */
margin: 1px 2px 3px;        /* 上 | 左右 | 下 */
margin: 0;                  /* 四方同值,0 可省略單位 */

Border

border: 1px solid #000;  /* width style color,順序可互換但建議固定 */
border-left: 2px dashed red; /* 單邊縮寫 */

Background 縮寫(順序建議固定)

background: #f00 url(bg.gif) no-repeat fixed 0 0;
/* color | image | repeat | attachment | position-x position-y */
/* 現代擴展:size、origin、clip、gradient 皆可納入 */
background: linear-gradient(45deg, #f00, #0f0) center/cover no-repeat border-box;

Font 縮寫(嚴格順序,缺一不可)

font: italic small-caps bold 1em/1.4 "Lucida Grande", sans-serif;
/* style | variant | weight | size/line-height | family */
/* size 與 family 為必填,其餘可省略採預設值 */

陷阱:font: 1em "Serif" 會重置 line-height、weight、style 為預設值。

List-style 縮寫

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

Flex / Grid 現代縮寫

flex: 1 1 auto;        /* grow | shrink | basis */
grid: auto-flow dense / 1fr 2fr; /* template-rows / template-columns */

避坑指南

  • margin: 0 auto 水平置中需配合 width 或 max-width
  • background 縮寫會重置未聲明屬性為預設值(如 repeat: repeat)
  • font 縮寫同理重置所有字體相關屬性
  • 團隊協作建議:僅在熟悉預設值行為時使用縮寫,否則長手寫法更安全明確
copyright © 萬盛學電腦網 all rights reserved