萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 列表標記樣式完全控制:list-style-type 20+ 取值、自定義圖標、計數器進階與無障礙實踐

list-style-type 完整取值速查表

CSS1 基礎關鍵字(全支持)

關鍵字渲染適用場景
disc● 實心圓無序列表默認
circle○ 空心圓二級無序列表
square■ 實心方塊三級無序列表
decimal1, 2, 3…有序列表默認
decimal-leading-zero01, 02, 03…固定寬度編號
lower-romani, ii, iii, iv…附錄、法律條文
upper-romanI, II, III, IV…大綱一級
lower-alpha / lower-latina, b, c, d…子條款
upper-alpha / upper-latinA, B, C, D…大綱次級
none無標記導航、面包屑、重置

CSS2 國際化關鍵字(按瀏覽器支持度排序)

關鍵字文字系統示例支持度
cjk-ideographic中日韓表意數字一、二、三、四Chrome/Edge/Firefox/Safari ✅
hiragana日文平假名あ、い、う、え主流 ✅
katakana日文片假名ア、イ、ウ、エ主流 ✅
hiragana-iroha伊呂波順平假名い、ろ、は、に主流 ✅
katakana-iroha伊呂波順片假名イ、ロ、ハ、ニ主流 ✅
lower-greek希臘小寫字母α, β, γ, δ主流 ✅
armenian亞美尼亞數字Ա, Բ, ԳFirefox/Safari ✅, Chrome ⚠️
georgian喬治亞數字ა, ბ, გFirefox/Safari ✅, Chrome ⚠️
hebrew希伯來數字א, ב, גFirefox/Safari ✅, Chrome ⚠️

自定義標記三大方案

1. list-style-image — 簡單但不可控

ul { list-style-image: url(bullet.svg); }

缺點:無法縮放定位、高密度屏模糊、無法動畫。

2. ::marker 偽元素 — 現代標準(CSS::marker Level 3)

/* 修改標記顏色、大小、內容 */
li::marker {
  color: #007bff;
  font-size: 1.2em;
  content: "▸ ";  /* 自定義字符 */
}

/* 數字編號加括號 */
ol li::marker { content: counter(list-item) ") "; }

/* 針對特定列表 */
.nav li::marker { content: ""; } /* 隱藏導航標記 */

支持:Chrome 86+, Firefox 68+, Safari 15+, Edge 86+。

3. CSS Counter + ::before — 完全可控、兼容性最佳

.numbered {
  counter-reset: section;  /* 重置計數器 */
}
.numbered li {
  position: relative;
  padding-left: 2em;
  list-style: none;
}
.numbered li::before {
  counter-increment: section;
  content: counter(section) ".";
  position: absolute;
  left: 0;
  color: #007bff;
  font-weight: 600;
}

/* 多級嵌套編號 1.1, 1.2 */
.nested { counter-reset: subsection; }
.nested li { counter-increment: subsection; }
.nested li::before {
  content: counters(section, ".") "." counters(subsection, ".") " ";
}

list-style-position — 標記定位

ul { list-style-position: outside; } /* 默認:標記在內邊距外,文字不環繞 */
ul { list-style-position: inside; }  /* 標記在內容流內,文字環繞對齊 */

list-style 綜合簡寫

ul { list-style: square inside url(bullet.svg); }
/* 順序:type || position || image */

無障礙與語義化檢查清單

  • ✅ 使用 <ul>/<ol>/<li> 語義標簽,而非 <div> 模擬。
  • ✅ 隱藏標記時保留語義:list-style: none 而非 display: none。
  • ✅ 嵌套列表正確結構:<ul><li>...<ul><li>...</ul></li></ul>。
  • ✅ 複雜編號用 counter 而非硬編碼數字。
  • ✅ ::marker 樣式不影響螢幕閱讀器播報。

實戰:面包屑導航、分頁、時間軸

/* 面包屑 — 無標記、行內分隔符 */
.breadcrumb { display: flex; flex-wrap: wrap; gap: .5rem; list-style: none; padding: 0; }
.breadcrumb li+li::before { content: "/"; color: #999; }
.breadcrumb li[aria-current="page"] { color: #333; font-weight: 600; }

/* 分頁 — 數字+箭頭 */
.pagination { display: flex; gap: .25rem; list-style: none; }
.pagination a { padding: .25rem .5rem; border: 1px solid #ddd; border-radius: 4px; }
.pagination [aria-current="page"] { background: #007bff; color: #fff; border-color: #007bff; }

/* 時間軸 — 計數器+偽元素連線 */
.timeline { position: relative; padding-left: 2rem; list-style: none; }
.timeline::before { content: ""; position: absolute; left: .75rem; top: 0; bottom: 0; width: 2px; background: #ddd; }
.timeline li { position: relative; padding-bottom: 2rem; }
.timeline li::before { 
  counter-increment: step; 
  content: counter(step); 
  position: absolute; left: -2rem; width: 1.5rem; height: 1.5rem; 
  border-radius: 50%; background: #007bff; color: #fff; 
  display: flex; align-items: center; justify-content: center;
}

列表樣式控制已從屬性配置進化為偽元素/計數器編程。掌握 ::marker 與 counter,讓列表不再只是「項目符號」,而是信息架構的視覺化表達。

copyright © 萬盛學電腦網 all rights reserved