萬盛學電腦網

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

CSS List 樣式屬性簡寫與進階技巧

List 相關屬性

屬性值範例說明
list-style-typedisc / circle / square / decimal / lower-roman / none項目符號樣式
list-style-positionoutside (預設) / inside符號相對內容位置
list-style-imageurl(bullet.png) / none自訂圖片符號

簡寫屬性 list-style

/* 順序任意,建議 type → position → image */
ul {
  list-style: square inside url(bullet.png);
  /* 等同 */
  list-style-type: square;
  list-style-position: inside;
  list-style-image: url(bullet.png);
}

重置預設樣式 (常用)

ul, ol {
  list-style: none;      /* 移除符號 */
  margin: 0; padding: 0; /* 移除縮進 */
}

進階技巧

1. 計數器自定義編號 (CSS Counters)

.numbered {
  list-style: none;
  counter-reset: item;
}
.numbered li::before {
  counter-increment: item;
  content: counter(item) ". ";
  font-weight: bold; color: #1976d2; margin-right: .5em;
}

支援巢狀編號:counter-reset: section; counter-increment: section; content: counters(section, ".") " ";

2. 圖片符號對齊微調

li {
  list-style-image: url(bullet.svg);
  /* 圖片預設垂直置中於行盒,若需精確對齊改用背景圖 */
  background: url(bullet.svg) no-repeat left center / 1em;
  padding-left: 1.5em;
  list-style: none;
}

3. 響應式列表樣式切換

@media (max-width: 600px) {
  .nav-list { display:flex; flex-wrap:wrap; gap:.5rem; list-style:none; padding:0; }
  .nav-list li { flex:1 0 auto; }
}

瀏覽器支援

  • 所有屬性 IE6+ 完整支援
  • CSS Counters:IE8+、所有現代瀏覽器
  • ::marker 偽元素 (直接樣式化符號):Chrome 86+、Firefox 68+、Safari 15+、Edge 86+
copyright © 萬盛學電腦網 all rights reserved