list-style 是控制HTML列表样式的CSS简写属性,包含 list-style-type(标记类型)、list-style-position(标记位置)和 list-style-image(自定义图像)。通过设置这些子属性,可改变项目符号或编号的外观与布局;使用 list-style: none 并重置 padding 和 margin 可彻底清除默认样式,结合伪元素与CSS计数器能实现高度自定义的标记和复杂编号系统,如多级章节编号;list-style-position 的 outside 使文本换行后与标记对齐,inside 则让标记成为内容一部分并形成缩进块,选择需根据可读性、设计需求和文本长度权衡;有序列表可通过 start 属性或CSS计数器实现从指定数字开始及非传统编号。

list-style
<ul>
<ol>
要控制列表样式,我们主要会用到
list-style
list-style-type
list-style-position
list-style-image
1. list-style-type
<ul>
disc
circle
square
none
对于有序列表(
<ol>
decimal
decimal-leading-zero
lower-alpha
lower-latin
upper-alpha
upper-latin
lower-roman
upper-roman
示例:
立即学习“前端免费学习笔记(深入)”;
ul {
list-style-type: square; /* 无序列表使用方块 */
}
ol {
list-style-type: upper-roman; /* 有序列表使用大写罗马数字 */
}2. list-style-position
outside
inside
示例:
立即学习“前端免费学习笔记(深入)”;
ul.outside-list {
list-style-position: outside; /* 标记在内容外部 */
}
ul.inside-list {
list-style-position: inside; /* 标记在内容内部 */
}3. list-style-image
url('path/to/your-image.png')none
需要注意的是,
list-style-image
list-style-type
list-style-image
list-style-type
示例:
立即学习“前端免费学习笔记(深入)”;
ul {
list-style-image: url('images/custom-bullet.png'); /* 使用自定义图片 */
list-style-type: square; /* 如果图片加载失败,则回退到方块 */
}list-style
list-style
type
position
image
示例:
立即学习“前端免费学习笔记(深入)”;
ul {
list-style: square inside url('images/bullet.png');
}
/* 等同于:
ul {
list-style-type: square;
list-style-position: inside;
list-style-image: url('images/bullet.png');
}
*/使用
list-style: none;
list-style-type
list-style-position
list-style-image
none
彻底移除列表的默认样式并进行自定义,这在前端开发中是家常便饭。浏览器给
<ul>
<ol>
list-style-type
padding-left
margin
list-style: none;
要彻底“清零”列表的默认样式,我通常会这样操作:
ul, ol {
list-style: none; /* 移除项目符号或编号 */
padding: 0; /* 移除左侧内边距 */
margin: 0; /* 移除外边距 */
}这样一来,列表就变成了一个纯粹的块级元素,没有任何视觉上的“列表”痕迹。接下来,我们就可以利用 CSS 的强大功能,尤其是伪元素(
::before
::after
使用伪元素自定义标记:
伪元素可以让我们在不修改HTML结构的情况下,在元素内容的前面或后面插入内容。这对于自定义列表标记来说,简直是天作之合。
ul.custom-list li {
position: relative; /* 为伪元素定位提供参考 */
padding-left: 20px; /* 为自定义标记留出空间 */
line-height: 1.5; /* 确保行高舒适 */
}
ul.custom-list li::before {
content: "?"; /* 自定义标记内容,可以是字符、表情符号 */
position: absolute;
left: 0;
top: 0; /* 或者 top: 0.2em; 根据字体大小微调 */
color: #007bff; /* 标记颜色 */
font-size: 1.2em; /* 标记大小 */
}
/* 如果是自定义编号,可以使用CSS计数器 */
ol.custom-numbered-list {
counter-reset: my-custom-counter; /* 初始化计数器 */
}
ol.custom-numbered-list li {
position: relative;
padding-left: 30px; /* 留出更多空间给编号 */
}
ol.custom-numbered-list li::before {
counter-increment: my-custom-counter; /* 每次出现li,计数器加1 */
content: counter(my-custom-counter) ". "; /* 显示计数器值和点号 */
position: absolute;
left: 0;
top: 0;
font-weight: bold;
color: #28a745;
}通过
position: relative
position: absolute
content
url()
background-image
counter-reset
counter-increment
counter()
list-style-type
list-style-position
inside
outside
list-style-position
inside
outside
outside
outside
inside
inside
inside
outside
inside
如何选择?
我的选择标准通常是这样的:
outside
inside
outside
inside
outside
list-style-position
padding-left
left
总的来说,
outside
inside
<ol>
是的,完全可以!CSS 为有序列表(
<ol>
list-style-type
1. 让编号从特定数字开始:
最简单的方法是使用 HTML 的
start
<ol>
<ol start="5">
<li>这是第五项</li>
<li>这是第六项</li>
<li>这是第七项</li>
</ol>这会直接让列表从数字 5 开始编号。
另一种 CSS 的方法是使用
counter-reset
start
2. 使用 CSS 计数器创建自定义编号:
CSS 计数器(
counter-reset
counter-increment
counter()
我们来看一个例子,如何创建一个从特定数字开始,并且可以实现多级编号的列表:
/* 首先,移除默认的列表样式和内边距 */
ol {
list-style: none;
padding: 0;
margin: 0;
}
/* 定义一个主计数器,例如用于章节 */
ol.chapter-list {
counter-reset: chapter; /* 初始化名为 'chapter' 的计数器 */
}
ol.chapter-list > li {
counter-increment: chapter; /* 每次遇到一级li,'chapter' 计数器加1 */
position: relative;
padding-left: 40px; /* 为编号留出空间 */
margin-bottom: 10px;
}
ol.chapter-list > li::before {
content: counter(chapter) ". "; /* 显示 'chapter' 计数器的值 */
position: absolute;
left: 0;
font-weight: bold;
color: #c0392b;
}
/* 现在处理二级列表,例如子章节 */
ol.chapter-list li ol {
counter-reset: section; /* 在每个一级li内部,重置名为 'section' 的计数器 */
margin-top: 5px;
}
ol.chapter-list li ol li {
counter-increment: section; /* 每次遇到二级li,'section' 计数器加1 */
position: relative;
padding-left: 60px; /* 为二级编号留出更多空间 */
margin-bottom: 5px;
}
ol.chapter-list li ol li::before {
content: counter(chapter) "." counter(section) ". "; /* 显示 'chapter' 和 'section' 计数器组合 */
position: absolute;
left: 0;
font-weight: normal;
color: #2c3e50;
}HTML 结构:
<ol class="chapter-list">
<li>
第一章标题
<ol>
<li>第一章第一节</li>
<li>第一章第二节</li>
</ol>
</li>
<li>
第二章标题
<ol>
<li>第二章第一节</li>
<li>第二章第二节</li>
<li>第二章第三节</li>
</ol>
</li>
</ol>这段代码展示了如何利用
counter-reset
counter-increment
counter-reset
counter-increment
content
counter()
list-style-type
以上就是如何使用csslist-style属性控制列表样式的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号