萬盛學電腦網

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

CSS 列表樣式屬性 list-style 完全指南:類型、圖片、位置、簡寫與現代重置最佳實踐

list-style 簡寫屬性結構

list-style:   ;
/* 順序任意,缺省值:disc outside none */

三大子屬性詳細解析

1. list-style-type — 標記類型

分類關鍵字效果適用列表
無序disc實心圓 ● (默認)ul
circle空心圓 ○ul
square實心方塊 ■ul
none無標記ul/ol
有序decimal阿拉伯數字 1,2,3...ol
lower-roman小寫羅馬 i, ii, iii...ol
upper-roman大寫羅馬 I, II, III...ol
lower-alpha小寫字母 a, b, c...ol
upper-alpha大寫字母 A, B, C...ol
cjk-ideographic中文數字 一、二、三...ol
hiragana/katakana日文平/片假名ol

2. list-style-image — 自定義圖片標記

ul.custom-bullets {
  list-style-image: url('bullet.svg'); /* SVG 矢量無失真 */
  /* 回退:圖片失效時顯示 list-style-type */
}
li::marker { /* 現代方式:直接控制標記盒模型 */
  content: url('bullet.svg');
  font-size: 1.2em;
  color: #0066cc;
}

💡 **現代首選 ::marker 偽元素**:支援顏色、字號、動畫、SVG,完全脫離 list-style-image 限制。

3. list-style-position — 標記位置

值行為視覺效果
outside (默認)標記在內容盒外側,文字不環繞標記對齊標記懸空於左邊距外
inside標記在內容盒內側,文字環繞對齊標記併入首行文本流

⚠️ margin-left: 0 會導致 outside 標記不可見,需保留足夠 padding-left (通常 ≥ 1.5rem)。

現代重置與最佳實踐

Reset 階段:統一歸零

/* reset.css */
ul, ol, menu {
  list-style: none;
  padding: 0;
  margin: 0;
}

組件層:語義化重建

/*** 文章正文列表 ***/
.prose ul { list-style: disc outside; padding-left: 1.5rem; }
.prose ol { list-style: decimal outside; padding-left: 1.5rem; }

/*** 導航/選單 — 無標記 ***/
.nav { list-style: none; display: flex; gap: 1rem; }

/*** 自定義圖標列表 — ::marker 現代寫法 ***/
.feature-list li::marker {
  content: "✨";
  color: #f59e0b;
  font-size: 1.2em;
}

進階:CSS Counters 自定義編號

.numbered-section {
  counter-reset: section;
}
.numbered-section h2::before {
  counter-increment: section;
  content: counter(section) ". ";
  font-weight: bold;
  color: #0066cc;
}

適用於文檔章節編號、法律條文、多級嵌套編號 (counter-reset: subsection 於 h3)。

總結

list-style 是**語義化列表的標準控制接口**。現代開發:**Reset 歸零 → 組件層語義化重建 → ::marker 進階定製 → CSS Counters 複雜編號**。拒絕直接在業務組件寫 ul { list-style: ... },建立可復用的列表組件體系。

copyright © 萬盛學電腦網 all rights reserved