list-style-image 定位行為跨瀏覽器**極不一致**:垂直對齊基準不同、無法精確控制間距、不支援精確像素定位。
ul { list-style: none; padding-left: 0; }
li {
padding-left: 20px; /* 留出圖標空間 */
background: url(bullet.png) no-repeat 0 center; /* 左中對齊 */
background-size: 12px 12px; /* 控制圖標顯示尺寸 */
line-height: 1.6;
}
優勢:
background-position 支援關鍵字/像素/百分比)background-size)background-position: -24px 0)li {
list-style: url(arrow.gif); /* 保留原生標記 */
}
li a {
position: relative;
top: -5px; /* 微調垂直位置 */
font: 12px/25px SimSun;
}
原理: 針對列表內的連結(或內容包裹元素)施加 position: relative + top 微調,間接帶動標記視覺位置。
缺陷: 僅能微調、不穩定、無法控制圖標尺寸、hover 狀態難處理、語意混淆。
.numbered-list {
counter-reset: item;
list-style: none;
padding-left: 0;
}
.numbered-list li {
position: relative;
padding-left: 2.5em;
margin-bottom: 0.5em;
}
.numbered-list li::before {
counter-increment: item;
content: counter(item) ".";
position: absolute;
left: 0;
top: 0;
width: 2em;
text-align: right;
color: #3498db;
font-weight: bold;
}
li::marker {
content: "▸ "; /* 自訂標記內容 */
color: #3498db; /* 標記顏色 */
font-size: 1.2em; /* 標記大小 */
font-weight: bold;
}
/* 或使用圖片 */
li::marker {
content: url(bullet.svg); /* SVG 矢量不失真 */
}
支援度: Firefox 68+、Chrome 86+、Safari 15+、Edge 86+。
優勢: 語意正確、無需重置 list-style、支援完整樣式控制、可動畫。
| 場景 | 推薦方案 | 理由 |
|---|---|---|
| 一般項目符號、圖標列表 | 背景圖模擬法 | 相容性最好、控制力最強 |
| 快速修復舊專案、不改 HTML | 相對定位法 | 改動最小、見效快 |
| 有序列表、數字/字母編號 | CSS 計數器 | 無圖片依賴、自動編號、可樣式化 |
| 現代專案、追求語意與標準 | ::marker 偽元素 | 標準化、語意純粹、功能強大 |
| 複雜互動(展開/摺疊、拖拽排序) | 自定義組件 | 脫離列表語意限制 |
list-style-image 是「看起來美好、用起來痛苦」的典型屬性。**背景圖模擬法** 是過去十年的最佳實踐;**::marker 偽元素** 是現在與未來的標準答案。根據專案瀏覽器支援矩陣選擇:舊專案用背景圖,新專案直上 ::marker,中間過渡期用計數器或偽元素模擬。