Ordered list (ol, li)
Basic Concepts of Ordered Lists
An ordered list (Ordered List) is an HTML element used to display content with sequential relationships. It is defined by the <ol>
tag, with list items represented by the <li>
tag. Browsers automatically add numbering to each list item, using Arabic numerals (1, 2, 3...) by default.
<ol>
<li>Step 1: Prepare materials</li>
<li>Step 2: Mix and stir</li>
<li>Step 3: Place in the oven</li>
</ol>
Attributes of the <ol>
Tag
The <ol>
tag supports several attributes to control the display style:
type
: Sets the numbering type1
: Numbers (default)A
: Uppercase lettersa
: Lowercase lettersI
: Uppercase Roman numeralsi
: Lowercase Roman numerals
<ol type="A">
<li>Chapter 1</li>
<li>Chapter 2</li>
<li>Chapter 3</li>
</ol>
start
: Specifies the starting numberreversed
: Reverses the order
<ol start="5" reversed>
<li>Fifth item</li>
<li>Fourth item</li>
<li>Third item</li>
</ol>
Special Attributes of the <li>
Tag
The <li>
tag can use the value
attribute to change the current item's number:
<ol>
<li>First item</li>
<li value="5">Fifth item (skips 2-4)</li>
<li>Sixth item</li>
</ol>
Nested Ordered Lists
Ordered lists can be nested multiple levels to form complex hierarchical structures:
<ol>
<li>Chapter 1
<ol>
<li>Section 1</li>
<li>Section 2</li>
</ol>
</li>
<li>Chapter 2
<ol>
<li>Section 1</li>
<li>Section 2</li>
</ol>
</li>
</ol>
CSS Styling Control
While browsers provide default styling, it can be fully customized with CSS:
ol.custom {
list-style-type: none;
counter-reset: section;
}
ol.custom li::before {
content: counters(section, ".") " ";
counter-increment: section;
color: red;
font-weight: bold;
}
<ol class="custom">
<li>Item
<ol>
<li>Sub-item</li>
<li>Sub-item</li>
</ol>
</li>
<li>Item</li>
</ol>
Practical Use Cases
- Instruction Steps:
<ol>
<li>Turn on the device</li>
<li>Select language settings</li>
<li>Connect to WiFi</li>
</ol>
- Table of Contents:
<ol type="I">
<li>Introduction
<ol type="A">
<li>Research background</li>
<li>Research objectives</li>
</ol>
</li>
<li>Main content</li>
</ol>
- Rankings Display:
<ol start="1">
<li>First place: John</li>
<li>Second place: Jane</li>
<li>Third place: Mike</li>
</ol>
Differences from Unordered Lists
The main differences between ordered lists and unordered lists (<ul>
) are:
- Ordered lists emphasize the importance of item sequence
- Unordered list items are of equal importance
- Ordered lists display numbers by default
- Unordered lists display bullet points by default
<!-- Ordered list -->
<ol>
<li>Step 1</li>
<li>Step 2</li>
</ol>
<!-- Unordered list -->
<ul>
<li>Apple</li>
<li>Banana</li>
</ul>
Browser Compatibility Considerations
All modern browsers fully support <ol>
and <li>
tags, but note:
- Some older IE versions have incomplete CSS counter support
- Long lists on mobile devices may require special styling to prevent overflow
- Printing may require adjusted list styles
@media print {
ol {
page-break-inside: avoid;
}
}
Advanced Application Techniques
- Custom Numbering Content:
ol.fancy li::marker {
content: "Step " counter(list-item) ": ";
}
- Multi-level Numbering Reset:
ol { counter-reset: section; }
li { counter-increment: section; }
li::before { content: counters(section,".") " "; }
- Dynamic Generation with JavaScript:
const steps = ["Prepare", "Execute", "Finish"];
const ol = document.createElement('ol');
steps.forEach(step => {
const li = document.createElement('li');
li.textContent = step;
ol.appendChild(li);
});
document.body.appendChild(ol);
Accessibility Best Practices
- Provide additional information for screen readers:
<ol aria-label="Installation steps">
<li aria-current="step">Download software</li>
<li>Run installer</li>
</ol>
- Use appropriate ARIA roles for complex lists:
<ol role="list">
<li role="listitem">Item 1</li>
<li role="listitem">Item 2</li>
</ol>
- Ensure sufficient contrast between numbers and content
Performance Optimization Tips
- Avoid excessive nesting (more than 5 levels)
- Consider virtual scrolling for long lists
- Pre-rendered static lists perform better than JavaScript-generated ones
// Bad practice - recreates on every render
function renderList(items) {
return items.map(item => `<li>${item}</li>`).join('');
}
// Good practice - generate once
const longList = Array(1000).fill().map((_,i) => `<li>Item ${i+1}</li>`).join('');
document.querySelector('ol').innerHTML = longList;
Combination with Other HTML Elements
Ordered lists are often combined with:
<div>
Elements:
<ol>
<li>
<div class="step-header">Step 1</div>
<div class="step-content">...</div>
</li>
</ol>
<figure>
Elements:
<ol>
<li>
<figure>
<img src="step1.jpg" alt="Step 1 illustration">
<figcaption>Prepare materials</figcaption>
</figure>
</li>
</ol>
- Form Elements:
<ol>
<li>
<label>
<input type="checkbox">
Agree to terms
</label>
</li>
</ol>
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:无序列表(ul, li)
下一篇:列表项的样式控制