CSS3 讓過去需要切圖、疊層、Hack 才能實現的圓角、陰影、半透明效果,變成幾行標準屬性。本文系統梳理三大屬性的語法、瀏覽器前綴歷史、現代最佳寫法與實戰組合技巧。
border-radius.box { border-radius: 1em; } /* 四角同半徑 */
.box { border-radius: 1em 2em 3em 4em; } /* 四角分別設定:左上 右上 右下 左下 */
.box { border-radius: 50%; } /* 圓形/橢圓 */
.box { border-radius: 10px / 20px; } /* 水平/垂直半徑不同(橢圓角) */
.box {
-webkit-border-radius: 1em; /* Safari 3-4, iOS 1-3, Android ≤1.6 */
-moz-border-radius: 1em; /* Firefox 1-3.6 */
border-radius: 1em; /* 標準,所有現代瀏覽器 */
}
✅ 現代項目**直接寫標準屬性**,Autoprefixer 會按 browserslist 自動補齊(其實已無需補)。
box-shadowbox-shadow: h-offset v-offset blur spread color inset;
/* 參數含義 */
h-offset : 水平偏移(正右負左)
v-offset : 垂直偏移(正下負上)
blur : 模糊半徑(≥0,0 為銳利邊緣)
spread : 擴散半徑(正擴大、負縮小,可做外發光/內縮邊框)
color : 陰影顏色(建議 rgba() 控制透明度)
inset : 可選,改為內陰影
.card {
box-shadow:
0 2px 8px rgba(0,0,0,0.08), /* 柔和外陰影 */
0 0 0 1px rgba(0,0,0,0.04); /* 1px 邊框線(替代 border) */
}
.btn:active {
box-shadow: inset 0 2px 4px rgba(0,0,0,0.2); /* 按下內陰影 */
}
.glow {
box-shadow: 0 0 20px 4px rgba(66,153,225,0.6); /* 外發光 */
}
rgba() / hsla() / #RRGGBBAAopacity?opacity 作用於**整個元素及其所有子孫**,導致文字、圖片一起變透明。rgba() 僅影響**單一顏色值**(背景、邊框、陰影、文字顏色獨立控制)。
| 格式 | 範例 | 支援度 | 備註 |
|---|---|---|---|
rgba() | rgba(0,0,0,0.8) | IE9+ / 全現代 | 最常用,直觀 |
hsla() | hsla(210, 50%, 40%, 0.8) | IE9+ / 全現代 | 色相/飽和度/亮度更易調整 |
#RRGGBBAA | #000000CC | 現代瀏覽器(IE 不支援) | 8 位十六進制,簡潔 |
color-mod() | color-mod(#000 alpha(80%)) | 僅實驗階段 | CSS Color Module Level 5 |
.glass-card {
background: rgba(255,255,255,0.7);
backdrop-filter: blur(12px); /* 背景模糊,需 HTTPS */
-webkit-backdrop-filter: blur(12px);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0,0,0,0.1);
border: 1px solid rgba(255,255,255,0.3);
}
border-radius、box-shadow、rgba()@supports 做漸進增強@supports (backdrop-filter: blur(1px)) {
.glass-card { /* 毛玻璃增強 */ }
}
三大屬性是現代 CSS 視覺表達的基石,熟練組合它們,能用極少代碼構建豐富、高效能、無圖片依賴的 UI 介面。