萬盛學電腦網

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

CSS 列表樣式完全指南:list-style、計數器、圖標、重置最佳實踐

列表基礎屬性

/* 取消默認標記 */
list-style: none;

/* 分屬性寫法 */
list-style-type: square;        /* 標記類型:disc/circle/square/decimal/lower-roman/none */
list-style-position: inside;    /* inside/outside(標記位於內容區內/外) */
list-style-image: url(dot.png); /* 自定義圖片標記 */

/* 縮寫:type position image */
list-style: square inside url(dot.png);

現代重置最佳實踐

/* 通用列表重置 */
ul, ol { 
    margin: 0; 
    padding: 0; 
    list-style: none; 
}

/* 語義化恢復:文章內容區保留語義 */
.article-content ul { list-style: disc outside; padding-left: 1.5em; }
.article-content ol { list-style: decimal outside; padding-left: 1.5em; }

CSS 計數器:自定義編號系統

.numbered-section {
    counter-reset: section;  /* 初始化計數器 */
}
.numbered-section h2::before {
    counter-increment: section;
    content: "第 " counter(section) " 節 ";
    font-weight: bold;
    margin-right: 0.5em;
}

/* 多級嵌套計數器 */
.nested-list {
    counter-reset: level1;
}
.nested-list li {
    counter-reset: level2;
}
.nested-list li::before {
    counter-increment: level1;
    content: counter(level1) ". ";
}
.nested-list li li::before {
    content: counter(level1) "." counter(level2) " ";
}

圖標列表:偽元素 + 字體圖標/SVG

.icon-list li {
    position: relative;
    padding-left: 1.5em;
}
.icon-list li::before {
    content: "\e900";  /* Icon Font Unicode */
    font-family: "iconfont";
    position: absolute;
    left: 0;
    color: #007bff;
}

/* SVG 背景圖標 */
.icon-list li {
    padding-left: 1.5em;
    background: url('check.svg') no-repeat left center / 1em;
}

水平列表佈局

.nav-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0 1rem;
    list-style: none;
    margin: 0; padding: 0;
}
.nav-list a { padding: 0.5rem 1rem; }

相關延伸閱讀

CSS 字體屬性完整指南:font 縮寫、字體族、載入優化

copyright © 萬盛學電腦網 all rights reserved