--- title: 样式列表 slug: Learn/CSS/Styling_text/Styling_lists translation_of: Learn/CSS/Styling_text/Styling_lists original_slug: Learn/CSS/为文本添加样式/Styling_lists ---
List列表 大体上和其他文本一样,但是仍有一些你需要知道的特殊CSS属性,和一些可供参考的最佳实践,这篇文章将阐述这一切。
| 前置知识: | Basic computer literacy, HTML basics (study Introduction to HTML), CSS basics (study Introduction to CSS), 基本文本和字体样式. |
|---|---|
| 目标: | 熟悉与列表相关的样式和最佳实践 |
首先,让我们看一个简单的例子。文章中我们将看到无序,有序和描述列表——它们都具有相似的样式特性,而某些特性却又各不相同。Github上有未加载样式的例子(也可以查看源码。)
例子中列表的HTML代码如下:
<h2>Shopping (unordered) list</h2> <p>Paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference.</p> <ul> <li>Humous</li> <li>Pitta</li> <li>Green salad</li> <li>Halloumi</li> </ul> <h2>Recipe (ordered) list</h2> <p>Paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference.</p> <ol> <li>Toast pitta, leave to cool, then slice down the edge.</li> <li>Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li> <li>Wash and chop the salad.</li> <li>Fill pitta with salad, humous, and fried halloumi.</li> </ol> <h2>Ingredient description list</h2> <p>Paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference, paragraph for reference.</p> <dl> <dt>Humous</dt> <dd>A thick dip/sauce generally made from chick peas blended with tahini, lemon juice, salt, garlic, and other ingredients.</dd> <dt>Pitta</dt> <dd>A soft, slightly leavened flatbread.</dd> <dt>Halloumi</dt> <dd>A semi-hard, unripened, brined cheese with a higher-than-usual melting point, usually made from goat/sheep milk.</dd> <dt>Green salad</dt> <dd>That green healthy stuff that many of us just use to garnish kebabs.</dd> </dl>
现在,如果你去到例子的展示页面,并使用浏览器开发者工具查看那些列表元素,你会注意到若干个默认的样式预设值:
40px (2.5em)。当您创建样式列表时,您需要调整样式,使其保持与周围元素相同的垂直间距(例如段落和图片,有时称为垂直节奏))和相互间的水平间距(您可以在 Github 上参考完成的样式示例 ,也可以找到源代码。)
用于文本样式和间距的CSS如下所示:
/* General styles */
html {
font-family: Helvetica, Arial, sans-serif;
font-size: 10px;
}
h2 {
font-size: 2rem;
}
ul,ol,dl,p {
font-size: 1.5rem;
}
li, p {
line-height: 1.5;
}
/* Description list styles */
dd, dt {
line-height: 1.5;
}
dt {
font-weight: bold;
}
dd {
margin-bottom: 1.5rem;
}
现在我们来看一下列表的一般间距,我们来研究一些列表具有的特定属性。 我们从三个属性开始了解,这三个属性可以在 {{htmlelement("ul")}} 或 {{htmlelement("ol")}} 元素上设置:
像上面所提及的, {{cssxref("list-style-type")}} 属性允许你设置项目符号点的类型,在我们的例子中,我们在有序列表上设置了大写罗马数字:
ol {
list-style-type: upper-roman;
}
效果显示如下:

您可以通过 {{cssxref("list-style-type")}} 参考页面查找到更多选项。
{{cssxref("list-style-position")}} 设置在每个项目开始之前,项目符号是出现在列表项内,还是出现在其外。 如上所示,默认值为 outside,这使项目符号位于列表项之外。
如果值设置为 inside,项目条目则位于行内。
ol {
list-style-type: upper-roman;
list-style-position: inside;
}

{{cssxref("list-style-image")}} 属性允许对于项目符号使用自定义图片。其语法相当简单:
ul {
list-style-image: url(star.svg);
}
然而,这个属性在控制项目符号的位置,大小等方面是有限的。 您最好使用{{cssxref("background")}} 系列属性,您将在 Styling boxes 模块中了解更多信息。在这里我们仅做一点尝试!
结束我们的例子,我们样式化无序列表像这样(放到您之前所见的顶部):
ul {
padding-left: 2rem;
list-style-type: none;
}
ul li {
padding-left: 2rem;
background-image: url(star.svg);
background-position: 0 0;
background-size: 1.6rem 1.6rem;
background-repeat: no-repeat;
}
我们的所做如下:
40px设置为 20px,然后在列表项上设置相同的数值。 这就是说,整个列表项仍然排列在列表中,但是列表项产生了一些用于背景图像的填充。 如果我们没有设置填充,背景图像将与列表项文本重叠,这看起来会很乱。0 0,这意味着项目符号将出现在每个列表项的最左上侧。no-repeat。效果显示如下:

上述提到的三种属性可以用一个单独的速记属性 {{cssxref("list-style")}} 来设置。例如:
ul {
list-style-type: square;
list-style-image: url(example.png);
list-style-position: inside;
}
可以被如下方式代替:
ul {
list-style: square url(example.png) inside;
}
属性值可以任意顺序排列,你可以设置一个,两个或者三个值(该属性的默认值为 disc, none, outside),如果指定了 type 和 image,如果由于某种原因导致图像无法加载,则 type 将用作回退。
有时,您可能想在有序列表上进行不同的计数方式。例如: 从1以外的数字开始,或向后倒数,或者按步或多于1计数。HTML和CSS有一些工具可以帮助您
{{htmlattrxref("start","ol")}} 属性允许你从1 以外的数字开始计数。示例如下:
<ol start="4"> <li>Toast pitta, leave to cool, then slice down the edge.</li> <li>Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li> <li>Wash and chop the salad.</li> <li>Fill pitta with salad, humous, and fried halloumi.</li> </ol>
输出的结果如下:
{{ EmbedLiveSample('start', '100%', 150) }}
{{htmlattrxref("reversed","ol")}} 属性将启动列表倒计数。示例如下:
<ol start="4" reversed> <li>Toast pitta, leave to cool, then slice down the edge.</li> <li>Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li> <li>Wash and chop the salad.</li> <li>Fill pitta with salad, humous, and fried halloumi.</li> </ol>
输出的结果如下:
{{ EmbedLiveSample('reversed', '100%', 150) }}
{{htmlattrxref("value","ol")}} 属性允许设置列表项指定数值,示例如下:
<ol> <li value="2">Toast pitta, leave to cool, then slice down the edge.</li> <li value="4">Fry the halloumi in a shallow, non-stick pan, until browned on both sides.</li> <li value="6">Wash and chop the salad.</li> <li value="8">Fill pitta with salad, humous, and fried halloumi.</li> </ol>
输出的结果如下:
{{ EmbedLiveSample('value', '100%', 150) }}
注意: 纵然你使用非数字的 {{cssxref("list-style-type")}}, 你仍需要使用与数值同等意义的值作为 value 的属性。
在该学习环节,我们希望你使用如上所学尝试为一个嵌套式列表添加样式。我们已经提供了 HTML , 在此之上请完成如下:
如果犯了错误,可以随时点击 Reset(重置) 按钮进行重新设置。如果你真的遇到困难无法继续下去,点击 Show solution(显示解决方案)按钮查看可行的解决方案。
<div class="body-wrapper" style="font-family: 'Open Sans Light',Helvetica,Arial,sans-serif;">
<h2>HTML Input</h2>
<textarea id="code" class="html-input" style="width: 90%;height: 10em;padding: 10px;border: 1px solid #0095dd;"><ul>
<li>First, light the candle.</li>
<li>Next, open the box.</li>
<li>Finally, place the three magic items in the box, in this exact order, to complete the spell:
<ol>
<li>The book of spells</li>
<li>The shiny rod</li>
<li>The goblin statue</li>
</ol>
</li>
</ul></textarea>
<h2>CSS Input</h2>
<textarea id="code" class="css-input" style="width: 90%;height: 10em;padding: 10px;border: 1px solid #0095dd;"></textarea>
<h2>Output</h2>
<div class="output" style="width: 90%;height: 12em;padding: 10px;border: 1px solid #0095dd;overflow: auto;"></div>
<div class="controls">
<input id="reset" type="button" value="Reset" style="margin: 10px 10px 0 0;">
<input id="solution" type="button" value="Show solution" style="margin: 10px 0 0 10px;">
</div>
</div>
var htmlInput = document.querySelector(".html-input");
var cssInput = document.querySelector(".css-input");
var reset = document.getElementById("reset");
var htmlCode = htmlInput.value;
var cssCode = cssInput.value;
var output = document.querySelector(".output");
var solution = document.getElementById("solution");
var styleElem = document.createElement('style');
var headElem = document.querySelector('head');
headElem.appendChild(styleElem);
function drawOutput() {
output.innerHTML = htmlInput.value;
styleElem.textContent = cssInput.value;
}
reset.addEventListener("click", function() {
htmlInput.value = htmlCode;
cssInput.value = cssCode;
drawOutput();
});
solution.addEventListener("click", function() {
htmlInput.value = htmlCode;
cssInput.value = 'ul {\n list-style-type: square;\n}\n\nul li, ol li {\n line-height: 1.5;\n}\n\nol {\n list-style-type: lower-alpha\n}';
drawOutput();
});
htmlInput.addEventListener("input", drawOutput);
cssInput.addEventListener("input", drawOutput);
window.addEventListener("load", drawOutput);
{{ EmbedLiveSample('Playable_code', 700, 800) }}
CSS计数器提供用于自定义列表计数和样式的高级工具,但它们相当复杂。 如果你想更深入了解,请查看如下资源:
一旦你掌握一些相关的基础原则和特定属性,列表的样式还是相对容易理解的。在下篇文章中我们将转到另一话题——为链接提供样式的各种技巧。
{{PreviousMenuNext("Learn/CSS/Styling_text/Fundamentals", "Learn/CSS/Styling_text/Styling_links", "Learn/CSS/Styling_text")}}