list-style 是控制 <li>、<ul>、<ol> 等 display: list-item 元素外觀的簡寫屬性,包含三個子屬性:list-style-type、list-style-image、list-style-position。
| 值 | 外觀 | 備註 |
|---|---|---|
disc | ● 實心圓 | 預設(ul) |
circle | ○ 空心圓 | |
square | ■ 實心方塊 | |
decimal | 1, 2, 3… | 預設(ol) |
decimal-leading-zero | 01, 02, 03… | |
lower-roman | i, ii, iii… | 小寫羅馬數字 |
upper-roman | I, II, III… | 大寫羅馬數字 |
lower-alpha / lower-latin | a, b, c… | 小寫字母 |
upper-alpha / upper-latin | A, B, C… | 大寫字母 |
cjk-ideographic | 一、二、三… | 中文數字(支援度不一) |
hiragana / katakana | あ、い、う… / ア、イ、ウ… | 日文平/片假名 |
none | 無符號 | 常用於導航、重置 |
ul.custom-bullet {
list-style-image: url('bullet.svg');
/* 圖片無法載入時回退到 list-style-type */
list-style-type: disc;
}
::marker(現代)或 background-image + padding-left 模擬。| 值 | 行為 | 視覺效果 |
|---|---|---|
outside(預設) | 符號在內容框外側,文字換行不貼齊符號 | 標準清單外觀 |
inside | 符號在內容框內側,文字換行會貼齊符號 | 適合緊湊佈局、卡片式清單 |
注意:若 margin-left/padding-left 過小(< 1em),outside 的符號可能被切掉;建議給 ul/ol 至少 padding-left: 1.5em。
/* 順序:type | position | image(順序不影響,但建議固定順序) */
ul {
list-style: square inside url('bullet.png');
}
/* 常見組合 */
ul { list-style: none; } /* 移除符號 */
ol { list-style: decimal outside; } /* 數字編號,符號在外 */
.custom { list-style: url('icon.svg') inside none; } /* 圖片在內,無回退符號 */
縮寫時省略的值會回退至初始值(disc outside none)。
li::marker {
content: "▶ "; /* 自訂文字/emoji */
color: #e91e63; /* 符號顏色獨立控制 */
font-size: 1.2em; /* 符號大小獨立控制 */
/* 還可用 counter() 實現自定編號 */
}
list-style-image 圖片,但可用 content: url()。list-style-image 彈性大得多,推薦新專案採用。/*** Reset ***/
ul, ol { list-style: none; padding: 0; margin: 0; }
/*** 導航 ***/
.nav { display: flex; gap: 1rem; }
.nav-item::marker { content: ""; } /* 隱藏預設符號 */
/*** 文章內容區清單 ***/
.content ul { list-style: disc outside; padding-left: 1.5em; }
.content ol { list-style: decimal outside; padding-left: 1.5em; }
.content li::marker { color: #666; font-weight: bold; }
float 或 flex 容器下的 li 可能失去 list-item 顯示行為 → 需顯式 display: list-item。overflow: hidden 於 ul 會切掉 outside 的符號 → 改用 inside 或增加 padding-left。counter-reset + counter-increment + content: counters() 實現多層編號(如 1.1, 1.2)。善用 list-style 縮寫與 ::marker,清單樣式開發事半功倍。