RGBA / HSLA:帶透明度的色彩
.box { background: rgba(0, 0, 0, 0.5); } /* 50% 黑半透明 */
.text { color: hsla(200, 50%, 50%, 0.8); } /* HSL + Alpha */
- 與 `opacity` 差異:`opacity` 影響**整個元素及子孫**,`rgba` 僅影響**單一顏色屬性** (背景、邊框、文字、陰影)。
- IE9+ 全支援,IE8 需漸變濾鏡回退。
Text-shadow:文字陰影/描邊/霓虹
/* 基礎語法:x-offset y-offset blur-radius color */
.shadow-1 { text-shadow: 1px 1px 2px rgba(0,0,0,.5); } /* 陰影 */
.shadow-2 { text-shadow: 0 0 5px #fff, 0 0 10px #0ff; } /* 霓虹發光 */
.shadow-3 { text-shadow: 1px 1px 0 #fff, -1px -1px 0 #000; } /* 凸起/凹陷 */
- 可堆疊多組 (逗號分隔),按聲明順序疊加 (第一組在最上層)。
- 負值:`x` 負 → 陰影在右;`y` 負 → 陰影在上。
- `blur=0` 最銳利,漸大越模糊。
Box-shadow:盒子陰影/內陰影/多層疊加
/* 基礎:x y blur spread color */
.shadow { box-shadow: 2px 4px 6px rgba(0,0,0,.3); }
/* 內陰影 */
.inset { box-shadow: inset 0 2px 4px rgba(0,0,0,.2); }
/* 多層疊加:卡片立體感 */
.card {
box-shadow:
0 1px 3px rgba(0,0,0,.12),
0 1px 2px rgba(0,0,0,.24);
}
.card:hover {
box-shadow:
0 14px 28px rgba(0,0,0,.25),
0 10px 10px rgba(0,0,0,.22);
}
- `spread` (擴散半徑) 正值擴大陰影、負值縮小。
- `inset` 關鍵字產生內陰影 (如按鈕按下、輸入框凹陷)。
Border-radius:圓角、圓形、不規則形狀
/* 統一圓角 */
.round { border-radius: 8px; }
/* 四角不同值 (順序:左上、右上、右下、左下) */
.asym { border-radius: 20px 0 20px 0; }
/* 八值語法:水平半徑 / 垂直半徑 */
.organic { border-radius: 50% 20% 30% 40% / 60% 30% 70% 40%; }
/* 圓形 (正方形容器) */
.circle { width: 100px; height: 100px; border-radius: 50%; }
/* 胶囊/標籤 */
.pill { border-radius: 9999px; } /* 或 50% 配合 height */
- WebKit/舊 Firefox 需前綴 `-webkit-`、`-moz-` (Autoprefixer 自動處理)。
- 百分比基於**該邊長度**,非正方形時 50% 為橢圓而非圓。
綜合實戰:Material Design 卡片
.md-card {
background: #fff; border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,.08), 0 4px 8px rgba(0,0,0,.06);
transition: box-shadow .2s, transform .2s;
}
.md-card:hover { box-shadow: 0 8px 16px rgba(0,0,0,.12), 0 4px 8px rgba(0,0,0,.08); transform: translateY(-2px); }
.md-card h3 { color: rgba(0,0,0,.87); text-shadow: 0 1px 0 rgba(255,255,255,.5); }