list-style: <type> || <position> || <image>;
/* 順序任意,建議:type position image */
ul { list-style: square inside url(bullet.svg); }
/* 等效展開 */
ul {
list-style-type: square; /* 標記類型 */
list-style-position: inside; /* 標記位置 */
list-style-image: url(bullet.svg); /* 自定義圖片標記 */
}
| 分類 | 關鍵字 | 視覺效果 | 適用場景 |
|---|---|---|---|
| 經典 | disc | ● 實心圓 | 無序列表默認 |
circle | ○ 空心圓 | 次級列表 | |
square | ■ 實心方塊 | 強調項目 | |
| 數字 | decimal | 1, 2, 3... | 有序列表默認 |
decimal-leading-zero | 01, 02, 03... | 對齊需求 | |
lower-roman | i, ii, iii... | 附錄、條款 | |
upper-roman | I, II, III... | 正式文檔 | |
| 字母 | lower-alpha / lower-latin | a, b, c... | 選項列表 |
upper-alpha / upper-latin | A, B, C... | 大綱層級 | |
lower-greek | α, β, γ... | 學術/科技 | |
cjk-ideographic | 一, 二, 三... | 中文文檔 | |
| 特殊 | none | 無標記 | 導航、面包屑、自定義計數器 |
| 國際化 | armenian, georgian, hebrew, hiragana, katakana | 對應語系 | 多語言站點 |
ul.outside { list-style-position: outside; } /* 默認:標記在內容盒外,文字環繞不對齊 */
ul.inside { list-style-position: inside; } /* 標記在內容盒內,文字按標記對齊 */
/* 視覺差異示意 */
.outside: ● 文字內容
● 文字內容
.inside: ● 文字內容
● 文字內容
關鍵點:inside 會使標記參與文本流佈局,配合 padding-left: 0 可實現標記與文字齊平。
ul.custom-bullet {
list-style-image: url('bullet.svg'); /* SVG 矢量無損縮放 */
/* 回退:圖片載入失敗時使用 type */
list-style-type: disc;
}
/* 進階:多種狀態圖片 */
ul.interactive li { list-style-image: url(bullet-default.svg); }
ul.interactive li:hover { list-style-image: url(bullet-hover.svg); }
ul.interactive li.completed { list-style-image: url(bullet-check.svg); }
最佳實踐:優先用 ::marker 偽元素(現代標準),list-style-image 為回退方案。
/* 完全控制標記樣式,不受 list-style 限制 */
li::marker {
content: "▶ "; /* 自定義內容 */
color: #007bff; /* 獨立顏色 */
font-size: 1.2em; /* 獨立大小 */
font-weight: bold;
}
/* 不同層級不同標記 */
ol > li::marker { content: counter(item) ". "; }
ol ol > li::marker { content: counter(subitem, lower-alpha) ") "; }
/* 條件樣式 */
li:checked::marker { content: "✓ "; color: green; }
li::marker { content: "○ "; color: #999; }
優勢:不需要 list-style: none 重置、支援動畫/變換、可用 counter() 實現複雜編號。
ol.numbered {
counter-reset: section; /* 根計數器 */
list-style: none; /* 隱藏默認標記 */
}
ol.numbered > li {
counter-increment: section; /* 遞增 */
}
ol.numbered > li::before {
content: counters(section, ".") " "; /* 多層級:1. 1.1 1.1.1 */
font-weight: bold;
color: #666;
margin-right: 0.5rem;
}
/* 章節標題自動編號 */
h2 { counter-reset: subsection; }
h2::before {
counter-increment: section;
content: counter(section) ". ";
}
h3::before {
counter-increment: subsection;
content: counter(section) "." counter(subsection) " ";
}
<ul> 無序列表、<ol> 有序列表、<dl> 定義列表 —— 語義優於視覺list-style: none 不移除語義,僅視覺隱藏role="listbox", aria-selected/* 移動端:簡化標記節省空間 */
@media (max-width: 480px) {
ul { list-style-type: disc; padding-left: 1.5rem; }
ol { list-style-type: decimal; padding-left: 1.5rem; }
}
/* 桌面端:豐富視覺層級 */
@media (min-width: 768px) {
.article-content ul { list-style: "🔹 "; } /* Unicode 直接作標記 */
.article-content ol { list-style: upper-roman; }
}
list-style 三屬性覆蓋 90% 需求,現代開發推薦遷移至 ::marker + 計數器系統,獲得完全的樣式控制權與語義保留。記住:列表語義(ul/ol/dl)永遠優先於視覺表現。