想要單選框(Radio Button)擁有「圓圈內陰影、左上半圓陰影」的立體質感,主要有兩種主流實現路徑。
隱藏原生 <input type="radio">,用 ::before/::after 偽元素繪製外框與內圓,配合 box-shadow、radial-gradient 產生立體感。
.radio-3d {
position: relative;
width: 20px; height: 20px;
appearance: none;
border: 2px solid #999;
border-radius: 50%;
background: radial-gradient(circle at 30% 30%, #fff 0%, #e0e0e0 100%);
box-shadow: inset 0 2px 4px rgba(0,0,0,.2), 0 1px 0 #fff;
cursor: pointer;
transition: all .2s;
}
.radio-3d:checked {
border-color: #007bff;
background: radial-gradient(circle at 30% 30%, #6ab0ff 0%, #0056d2 100%);
box-shadow: inset 0 2px 6px rgba(0,0,0,.3), 0 0 0 2px #fff, 0 2px 4px rgba(0,123,255,.4);
}
.radio-3d:focus { outline: none; box-shadow: 0 0 0 3px rgba(0,123,255,.4); }
準備兩張圖:radio_off.png、radio_on.png (含陰影立體效果),用 CSS 背景圖切換。
.radio-img {
width: 22px; height: 22px;
appearance: none;
background: url(radio_off.png) no-repeat center;
background-size: contain;
}
.radio-img:checked { background-image: url(radio_on.png); }
參考連結中的 alt_on.gif, alt_off.gif 即為此類圖片,JS 僅用於舊 IE 不支援 :checked 時的類別切換,現代專案可完全移除 JS。
| 維度 | 純 CSS | 圖片替換 |
|---|---|---|
| 維護性 | 修改顏色/大小只改數值 | 需重製圖片 |
| 效能 | 零請求,GPU 加速 | 額外圖片請求 |
| 相容性 | IE9+ (需 prefix) | 全瀏覽器 |
| 縮放清晰度 | 向量級,任意縮放銳利 | 固定解析度 |
@supports 降級。<input> 的語意與無障礙存取 (ARIA),僅用 CSS 隱藏視覺呈現。