/* 完整寫法 */
color: #000000;
background: #336699;
/* 縮寫(每兩位相同可縮為一位) */
color: #000;
background: #369;
適用於 16 進位色碼,且每對字符相同時(如 #ff00cc → #f0c)。
/* 四個值:上 右 下左 (順時針) */
margin: 10px 20px 15px 5px;
/* 三個值:上 左右 下 */
margin: 10px 20px 15px;
/* 兩個值:上下 左右 */
margin: 10px 20px;
/* 單一值:四邊相同 */
margin: 15px;
記憶口訣:「上右下左」順時針 🔄。
/* 分開寫法 */
border-width: 2px;
border-style: solid;
border-color: #333;
/* 縮寫:寬度 樣式 顏色(順序不固定,建議統一) */
border: 2px solid #333;
/* 單邊縮寫 */
border-top: 1px dashed red;
border-left: 3px double #000;
/* 分開寫法 */
background-color: #f0f0f0;
background-image: url(bg.png);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
/* 縮寫:顏色 圖片 重複 固定 位置 */
background: #f0f0f0 url(bg.png) no-repeat fixed center top;
/* 現代寫法(CSS3 多背景) */
background:
linear-gradient(45deg, #fff, #eee) no-repeat center,
url(pattern.svg) repeat 0 0;
默認值補充:省略的屬性會回退至默認值(如 repeat、scroll、0% 0%)。
/* 必須包含 font-size 與 font-family,否則無效 */
font: italic small-caps bold 1rem/1.5 "Microsoft JhengHei", sans-serif;
/* 順序:style variant weight size/line-height family */
⚠️ 注意:line-height 必須緊跟在 font-size 後並用 / 分隔。
/* 分開寫法 */
list-style-type: square;
list-style-position: inside;
list-style-image: url(bullet.svg);
/* 縮寫 */
list-style: square inside url(bullet.svg);
/* 移除預設樣式 */
list-style: none;
:root {
--spacing-unit: 8px;
--border-base: 1px solid #ddd;
}
.card {
margin: calc(var(--spacing-unit) * 2) var(--spacing-unit);
padding: var(--spacing-unit);
border: var(--border-base);
}
/* ❌ 不利於後續單獨修改 */
.button { background: #3498db url(icon.svg) no-repeat 10px center; }
/* ✅ 拆分關鍵屬性,保留彈性 */
.button {
background-color: #3498db;
background-image: url(icon.svg);
background-repeat: no-repeat;
background-position: 10px center;
}
/* border-image 縮寫 */
border: 10px solid transparent;
border-image: url(frame.png) 30 stretch;
/* 等同於 */
border-image-source: url(frame.png);
border-image-slice: 30;
border-image-repeat: stretch;
所有現代瀏覽器(Chrome 1+、Firefox 1+、Safari 1+、Edge 12+、IE 8+)完整支援上述縮寫語法。IE 6/7 對部分縮寫(如 background 多背景)不支援,但基本單屬性縮寫無問題。
熟練掌握 CSS 縮寫屬性是前端工程師的基本功。遵循「順時針」、「必填項優先」、「保留彈性」三大原則,能讓樣式表更精簡、語義更清晰、團隊協作更高效。建議在專案中建立統一的縮寫編碼規範,並配合 PostCSS 自動化檢查。