阿里云主机折上折
  • 微信号
Current Site:Index > - List item

- List item

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

Basic Definition of the <li> Tag

The <li> tag in HTML is used to define list items and must be nested within <ul> (unordered list) or <ol> (ordered list). It has no required attributes but can be further controlled with attributes like value (only for ordered lists) and type.

<ul>
  <li>Apple</li>
  <li>Banana</li>
</ul>

<ol>
  <li>First step</li>
  <li>Second step</li>
</ol>

<li> in Unordered Lists

In <ul>, <li> items are displayed as solid bullets (disc) by default, which can be styled using CSS. Each list item appears on a new line, forming a vertical list structure.

<ul>
  <li>Red</li>
  <li>Green</li>
  <li>Blue</li>
</ul>

Actual rendering:

  • Red
  • Green
  • Blue

<li> in Ordered Lists

In <ol>, <li> items are automatically numbered. The start attribute can set the initial number, and the reversed attribute enables reverse numbering.

<ol start="5">
  <li>Fifth item</li>
  <li>Sixth item</li>
</ol>

<ol reversed>
  <li>Third item</li>
  <li>Second item</li>
  <li>First item</li>
</ol>

The value Attribute of <li>

Only effective in ordered lists, it forces a specific number for the current item, with subsequent items recalculated accordingly.

<ol>
  <li value="10">Item ten</li>
  <li>Item eleven</li>  <!-- Continues numbering -->
  <li value="5">Item five</li>  <!-- Manual override -->
  <li>Item six</li>  <!-- Continues from 5 -->
</ol>

Implementing Nested Lists

<li> can contain complete list structures for multi-level nesting. Ensure proper tag closure.

<ul>
  <li>Fruits
    <ul>
      <li>Apple</li>
      <li>Orange</li>
    </ul>
  </li>
  <li>Vegetables
    <ol>
      <li>Carrot</li>
      <li>Broccoli</li>
    </ol>
  </li>
</ul>

CSS Styling Control

CSS allows full customization of <li> display, including marker types, positioning, and custom icons.

ul.custom {
  list-style-type: none;  /* Remove default marker */
  padding-left: 0;
}

ul.custom li {
  padding: 5px 0;
  background: url('icon.png') no-repeat left center;
  padding-left: 30px;
}

ol.roman {
  list-style-type: upper-roman;
}

Interactive List Items

Combine with JavaScript for dynamic interactions like click events, adding/removing items.

<ul id="dynamic-list">
  <li>Initial item</li>
</ul>

<script>
  const list = document.getElementById('dynamic-list');
  list.addEventListener('click', (e) => {
    if(e.target.tagName === 'LI') {
      e.target.classList.toggle('completed');
    }
  });
  
  // Dynamically add new item
  function addItem(text) {
    const li = document.createElement('li');
    li.textContent = text;
    list.appendChild(li);
  }
</script>

Accessibility Considerations

Ensure good accessibility for <li>:

  • Wrap navigation lists in <nav>
  • Add role="button" to clickable items
  • Avoid relying solely on color for information
<nav aria-label="Main menu">
  <ul>
    <li><a href="#" role="menuitem">Home</a></li>
    <li><a href="#" role="menuitem">Products</a></li>
  </ul>
</nav>

Practical Use Cases

  1. Navigation Menu:
<nav>
  <ul class="nav-menu">
    <li><a href="/">Home</a></li>
    <li><a href="/about">About</a></li>
  </ul>
</nav>
  1. Step-by-Step Instructions:
<ol class="steps">
  <li>Enter basic information</li>
  <li>Verify email</li>
  <li>Complete registration</li>
</ol>
  1. Product Features List:
<ul class="features">
  <li>4K resolution support</li>
  <li>Water and dust resistant</li>
  <li>2-year warranty</li>
</ul>

Browser Compatibility Notes

All modern browsers fully support the <li> tag, including:

  • Chrome 1+
  • Firefox 1+
  • Safari 1+
  • Edge 12+
  • Opera 8+

Compatibility considerations:

  • IE8 and earlier have limited CSS list styling support
  • Some mobile browsers may ignore the value attribute
  • Test cross-browser behavior when using custom list markers

Performance Optimization Tips

For large lists:

  • Use virtual scrolling to render only visible items
  • Avoid complex layouts within <li>
  • Consider server-side rendering for static lists
// Virtual scrolling example
const virtualList = new VirtualScroller({
  element: document.getElementById('long-list'),
  items: Array(1000).fill().map((_,i) => `Item ${i+1}`),
  renderItem: (text) => {
    const li = document.createElement('li');
    li.textContent = text;
    return li;
  }
});

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

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

上一篇:

    -有序列表

    下一篇:<dl>-描述列表

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 ☕.