Home > Web Front-end > CSS Tutorial > How to Create Decimal Numbered Ordered Lists with CSS Counters?

How to Create Decimal Numbered Ordered Lists with CSS Counters?

Patricia Arquette
Release: 2024-12-17 02:11:24
Original
845 people have browsed it

How to Create Decimal Numbered Ordered Lists with CSS Counters?

Ordering Lists with Decimal Numbers and CSS

In an ordered list, you may encounter the need to display numbers in a specific format, such as 1.1, 1.2, 1.3, instead of the default sequence of 1, 2, 3. While using list-style-type:decimal only numbers list items sequentially, it is possible to achieve the desired numbering format using CSS counters.

Solution: Using Counters

Counters allow you to manipulate the numeric value displayed for list items. Here's the CSS code to create a hierarchical numbering system:

OL { counter-reset: item }
LI { display: block }
LI:before { content: counters(item, ".") " "; counter-increment: item }
Copy after login

Example

Consider the following HTML structure:

<ol>
  <li>li element
    <ol>
      <li>sub li element</li>
      <li>sub li element</li>
      <li>sub li element</li>
    </ol>
  </li>
  <li>li element</li>
  <li>li element
    <ol>
      <li>sub li element</li>
      <li>sub li element</li>
      <li>sub li element</li>
    </ol>
  </li>
</ol>
Copy after login

Applying the CSS code above, the output will be:

1. li element
   1.1 sub li element
   1.2 sub li element
   1.3 sub li element
2. li element
3. li element
   3.1 sub li element
   3.2 sub li element
   3.3 sub li element
Copy after login

The above is the detailed content of How to Create Decimal Numbered Ordered Lists with CSS Counters?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template