萬盛學電腦網

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

CSS 列表樣式屬性 list-style 完整指南:從基礎到進階定制

list-style 縮寫屬性結構

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); /* 自定義圖片標記 */
}

三大子屬性深度解析

list-style-type:標記類型大全

分類關鍵字視覺效果適用場景
經典disc● 實心圓無序列表默認
circle○ 空心圓次級列表
square■ 實心方塊強調項目
數字decimal1, 2, 3...有序列表默認
decimal-leading-zero01, 02, 03...對齊需求
lower-romani, ii, iii...附錄、條款
upper-romanI, II, III...正式文檔
字母lower-alpha / lower-latina, b, c...選項列表
upper-alpha / upper-latinA, B, C...大綱層級
lower-greekα, β, γ...學術/科技
cjk-ideographic一, 二, 三...中文文檔
特殊none無標記導航、面包屑、自定義計數器
國際化armenian, georgian, hebrew, hiragana, katakana對應語系多語言站點

list-style-position:標記定位機制

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

/* 視覺差異示意 */
.outside:  ●  文字內容
           ●  文字內容
.inside:   ● 文字內容
           ● 文字內容

關鍵點:inside 會使標記參與文本流佈局,配合 padding-left: 0 可實現標記與文字齊平。

list-style-image:自定義圖片標記

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 為回退方案。

現代替代方案:::marker 偽元素 (CSS Pseudo-Elements 4)

/* 完全控制標記樣式,不受 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> 定義列表 —— 語義優於視覺
  • 螢幕閱讀器依賴標籤語義而非 CSS 樣式
  • list-style: none 不移除語義,僅視覺隱藏
  • 互動列表(待辦、選單)需配合 ARIA: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)永遠優先於視覺表現。

copyright © 萬盛學電腦網 all rights reserved