阿里云主机折上折
  • 微信号
Current Site:Index > Ordered list (ol, li)

Ordered list (ol, li)

Author:Chuan Chen 阅读数:49274人阅读 分类: HTML

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 type
    • 1: Numbers (default)
    • A: Uppercase letters
    • a: Lowercase letters
    • I: Uppercase Roman numerals
    • i: Lowercase Roman numerals
<ol type="A">
  <li>Chapter 1</li>
  <li>Chapter 2</li>
  <li>Chapter 3</li>
</ol>
  • start: Specifies the starting number
  • reversed: 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

  1. Instruction Steps:
<ol>
  <li>Turn on the device</li>
  <li>Select language settings</li>
  <li>Connect to WiFi</li>
</ol>
  1. 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>
  1. 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

  1. Custom Numbering Content:
ol.fancy li::marker {
  content: "Step " counter(list-item) ": ";
}
  1. Multi-level Numbering Reset:
ol { counter-reset: section; }
li { counter-increment: section; }
li::before { content: counters(section,".") " "; }
  1. 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

  1. Provide additional information for screen readers:
<ol aria-label="Installation steps">
  <li aria-current="step">Download software</li>
  <li>Run installer</li>
</ol>
  1. Use appropriate ARIA roles for complex lists:
<ol role="list">
  <li role="listitem">Item 1</li>
  <li role="listitem">Item 2</li>
</ol>
  1. Ensure sufficient contrast between numbers and content

Performance Optimization Tips

  1. Avoid excessive nesting (more than 5 levels)
  2. Consider virtual scrolling for long lists
  3. 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:

  1. <div> Elements:
<ol>
  <li>
    <div class="step-header">Step 1</div>
    <div class="step-content">...</div>
  </li>
</ol>
  1. <figure> Elements:
<ol>
  <li>
    <figure>
      <img src="step1.jpg" alt="Step 1 illustration">
      <figcaption>Prepare materials</figcaption>
    </figure>
  </li>
</ol>
  1. Form Elements:
<ol>
  <li>
    <label>
      <input type="checkbox">
      Agree to terms
    </label>
  </li>
</ol>

本站部分内容来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn

Front End Chuan

Front End Chuan, Chen Chuan's Code Teahouse 🍵, specializing in exorcising all kinds of stubborn bugs 💻. Daily serving baldness-warning-level development insights 🛠️, with a bonus of one-liners that'll make you laugh for ten years 🐟. Occasionally drops pixel-perfect romance brewed in a coffee cup ☕.