列表属性用于控制列表的外观,包括列表标记的类型、位置和图像。掌握列表属性对于创建美观的列表样式非常重要。
list-style-type 属性用于设置列表标记的类型。
/* 圆点(默认) */
.disc {
list-style-type: disc;
}
/* 圆圈 */
.circle {
list-style-type: circle;
}
/* 方块 */
.square {
list-style-type: square;
}
/* 无标记 */
.none {
list-style-type: none;
}
/* 数字(默认) */
.decimal {
list-style-type: decimal;
}
/* 小写字母 */
.lower-alpha {
list-style-type: lower-alpha;
}
/* 大写字母 */
.upper-alpha {
list-style-type: upper-alpha;
}
/* 小写罗马数字 */
.lower-roman {
list-style-type: lower-roman;
}
/* 大写罗马数字 */
.upper-roman {
list-style-type: upper-roman;
}
/* 希腊字母 */
.lower-greek {
list-style-type: lower-greek;
}
list-style-position 属性用于设置列表标记的位置。
/* 标记在内容框内(默认) */
.inside {
list-style-position: inside;
}
/* 标记在内容框外 */
.outside {
list-style-position: outside;
}
/* 列表标记在内容框内 */
.inside-list {
list-style-position: inside;
padding-left: 20px;
}
/* 列表标记在内容框外 */
.outside-list {
list-style-position: outside;
padding-left: 40px;
}
list-style-image 属性用于使用图像作为列表标记。
/* 使用图像作为标记 */
.image-list {
list-style-image: url('bullet.png');
}
/* 使用 none */
.no-image {
list-style-image: none;
}
/* 自定义列表标记 */
.custom-list {
list-style-image: url('custom-bullet.png');
padding-left: 30px;
}
list-style 简写属性可以同时设置列表的所有属性。
/* 完整语法 */
list-style: list-style-type list-style-position list-style-image;
/* 示例 */
.custom {
list-style: circle inside url('bullet.png');
}
/* 部分属性 */
.simple {
list-style: disc;
}
/* 导航菜单列表 */
.nav-menu {
list-style: none;
padding: 0;
margin: 0;
}
.nav-menu li {
display: inline-block;
margin-right: 20px;
}
.nav-menu a {
color: white;
text-decoration: none;
font-weight: bold;
}
.nav-menu a:hover {
color: #007bff;
}
/* 面包屑导航 */
.breadcrumb {
list-style: none;
padding: 0;
margin: 0;
}
.breadcrumb li {
display: inline-block;
margin-right: 5px;
}
.breadcrumb li::after {
content: "/";
margin-left: 5px;
}
.breadcrumb li:last-child::after {
content: "";
}
.breadcrumb a {
color: #007bff;
text-decoration: none;
}
.breadcrumb a:hover {
text-decoration: underline;
}
/* 功能列表 */
.feature-list {
list-style: none;
padding: 0;
}
.feature-list li {
padding: 10px 0;
border-bottom: 1px solid #eee;
}
.feature-list li::before {
content: "✓";
color: #2ecc71;
margin-right: 10px;
font-weight: bold;
}
/* 步骤列表 */
.step-list {
list-style: none;
counter-reset: step;
padding: 0;
}
.step-list li {
position: relative;
padding-left: 40px;
margin-bottom: 20px;
}
.step-list li::before {
counter-increment: step;
content: counter(step);
position: absolute;
left: 0;
top: 0;
width: 30px;
height: 30px;
background: #007bff;
color: white;
border-radius: 50%;
text-align: center;
line-height: 30px;
font-weight: bold;
}
/* 带图标的列表 */
.icon-list {
list-style: none;
padding: 0;
}
.icon-list li {
padding-left: 30px;
margin-bottom: 10px;
position: relative;
}
.icon-list li::before {
content: "";
position: absolute;
left: 0;
top: 5px;
width: 16px;
height: 16px;
background-image: url('icon.png');
background-size: contain;
background-repeat: no-repeat;
}
/* 使用伪元素自定义列表标记 */
.custom-marker {
list-style: none;
padding-left: 0;
}
.custom-marker li {
position: relative;
padding-left: 25px;
margin-bottom: 10px;
}
.custom-marker li::before {
content: "→";
position: absolute;
left: 0;
color: #007bff;
font-weight: bold;
}
/* 使用背景图像自定义列表标记 */
.bg-image-list {
list-style: none;
padding-left: 0;
}
.bg-image-list li {
padding-left: 25px;
margin-bottom: 10px;
background: url('arrow.png') no-repeat left center;
background-size: 15px 15px;
}
/* 推荐 - 移除导航菜单的默认列表样式 */
.nav-menu {
list-style: none;
padding: 0;
margin: 0;
}
/* 不推荐 - 保留默认列表样式 */
.nav-menu {
/* 默认样式可能不符合设计要求 */
}
<!-- 推荐 - 使用语义化的列表 -->
<ul class="nav-menu">
<li><a href="#">首页</a></li>
<li><a href="#">关于</a></li>
<li><a href="#">联系</a></li>
</ul>
<!-- 不推荐 - 使用 div 模拟列表 -->
<div class="nav-menu">
<a href="#">首页</a>
<a href="#">关于</a>
<a href="#">联系</a>
</div>
/* 推荐 - 使用合适的列表样式 */
.article-list {
list-style: disc;
padding-left: 20px;
}
/* 不推荐 - 过度使用自定义列表样式 */
.custom-list {
list-style: url('fancy-bullet.png') inside;
/* 可能影响性能 */
}
/* 响应式列表 */
.responsive-list {
list-style: none;
padding: 0;
}
.responsive-list li {
padding: 10px;
border-bottom: 1px solid #eee;
}
@media (min-width: 768px) {
.responsive-list {
display: flex;
flex-wrap: wrap;
}
.responsive-list li {
width: 50%;
border-bottom: none;
}
}
列表属性用于控制列表的外观:
记住以下几点: