萬盛學電腦網

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

CSS list-style 完整屬性拆解:type、image、position、shorthand 實戰速查

三大子屬性與簡寫

/* 簡寫順序:type | position | image */
list-style: square inside url("bullet.png");
/* 等同於 */
list-style-type: square;
list-style-position: inside;
list-style-image: url("bullet.png");

list-style-type 所有關鍵字

關鍵字效果CSS 級別
disc實心圓(預設)CSS1
circle空心圓CSS1
square實心方塊CSS1
decimal阿拉伯數字 1,2,3…CSS1
lower-roman小寫羅馬 i,ii,iii…CSS1
upper-roman大寫羅馬 I,II,III…CSS1
lower-alpha小寫字母 a,b,c…CSS1
upper-alpha大寫字母 A,B,C…CSS1
none無標記CSS1
cjk-ideographic中文數字 一、二、三…CSS2
hiragana日文平假名 あ、い、う…CSS2
katakana日文片假名 ア、イ、ウ…CSS2

list-style-position 差異圖解

/* outside(預設) */
li { list-style-position: outside; }
  ▸ 項目內容
    內容換行對齊文字起始

/* inside */
li { list-style-position: inside; }
  ▸ 項目內容
  內容換行對齊標記起始

list-style-image 進階技巧

  • 圖片無法載入時自動 fallback 到 list-style-type
  • 建議使用 ::marker 偽元素(現代瀏覽器)替代,可控制顏色、大小:
    ::marker { color: #f60; font-size: 1.2em; content: "→ "; }
  • SVG 作為標記:url("data:image/svg+xml,...") 避免額外請求

實戰範例:自訂編號樣式

ol.steps {
  list-style: none;           /* 移除預設 */
  counter-reset: step;        /* 初始化計數器 */
}
ol.steps li::before {
  counter-increment: step;
  content: "Step " counter(step) ": ";
  font-weight: bold;
  color: #0066cc;
  margin-right: 0.5em;
}

常見坑點

  • display: flex 會導致 list-style 失效 → 改用 ::marker 或手動計數器
  • margin-left: 0 會隱藏標記(outside 模式下) → 保留至少 padding-left: 1.5em
  • 巢狀列表未重置計數器 → 使用 counter-reset 配合 counters()
copyright © 萬盛學電腦網 all rights reserved