search
HomeWeb Front-endCSS TutorialHow to customize list items using CSS using @counter-style rules?

如何使用 @counter-style 规则使用 CSS 自定义列表项?

Lists are an essential part of web development and are used to present information in an organized and structured manner. In HTML, there are 3 types of lists: ordered lists, unordered lists, and definition lists. However, styling these lists can be challenging when we need to design the lists as per the requirements. CSS provides the @counter-style rule, which allows us to customize list item markup in a more flexible and creative way.

In this article, we will learn how to use @counter-style rules to customize list items using CSS. The @counter-style rule is used to define counter styles that are not part of a predefined style set and defines how the counter value is converted into a string representation.

What is @counter-style?

The @counter-style rule is used to define a counter style that can be used in conjunction with the CSS counter property. This rule is used to define a custom list item marker style. The counter property allows us to increment or decrement a counter, which is used to generate content for pseudo-elements like :before and :after.

grammar

@counter-style name {
   // write all the CSS styles properties and values
}

The name parameter is used to specify the name of the counter style that we are defining. Within the curly braces, we can define a set of properties and values ​​that control the appearance of the counter. Some of the properties that we can use include −

  • System − It specifies the numbering system to use, such as decimal, lowercase letters, uppercase Roman numerals, etc.

  • Symbol - It specifies the symbol used for each level of the counter.

  • Suffix − It specifies the suffix added after the counter value.

  • Prefix − It specifies the prefix to add before the counter value.

  • Pad − It specifies the minimum number of digits to use when displaying the counter value.

Steps to use @counter-styles Rule in CSS

The following are the steps to use @counter-styles rules in CSS -

Step 1: Create an Ordered List

The first step is to create an ordered list and customize it with our own list item tags. In the example below, we want to use Roman numerals instead of the default numbering system. Below is the HTML code for our list −

<ol>
   <li>Learn to code in python</li>
   <li>Learn to code in java</li>
   <li>Learn to code in c++</li>
</ol>

Step 2: Define the @counter-style

@counter-style my-numerals {
   system: upper-roman;
   symbols: I II III IV V VI VII VIII IX X;
}

In the above code, we have defined a counter style named my-numerals and set the system property to upper-roman which means the counter will be set to use the uppercase Roman numerals in the list. Apart from this, we have also set the symbol's property to a string of Roman numerals from I to X.

Step 3: CSS Styles

ol {
   counter-reset: section;
}
li {
   counter-increment: section;
   list-style: none;
}
li:before {
   content: counter(section, my-numerals) ". ";
   margin-right: 10px;
}

In the above code, the counter-reset attribute is set to the section of the ol element, which means the counter will start from 0. Here, we also set the counter-increment attribute of the section for each li element.

Example 1

is translated as:

Example 1

<html>
<head>
   <title>Example to use @counter-style to customize the List Item Markers using CSS</title>
   <style>
      /* Defining a custom counter style to have the list as upper roman numerals */
      @counter-style roman-numerals {
         system: upper-roman;
         symbols: I II III IV V VI VII VIII IX X;
      }
      /* applying the custom counter style to the ordered list */
      ol {counter-reset: section; }
      /* incrementing the counter for each list item */
      li {counter-increment: section; list-style: none;}
      /* using the counter value to display the custom list item marker */
      li:before {
         content: counter(section, roman-numerals) ". ";
         margin-right: 8px;
         color: green;
         font-size: 15px;
      }
   </style>
</head>
<body>
   <h3 id="Example-to-use-counter-style-to-customize-the-List-Item-Markers-using-CSS">Example to use @counter-style to customize the List Item Markers using CSS</h3>
   <p>In this example, we have used the @counter-style rule to customize the list item markers of an ordered list.</p>
   <ol>
      <li>Learn to code in python</li>
      <li>Learn to code in java</li>
      <li>Learn to code in c++</li>
   </ol>
</body>
</html>

In the example above, we have defined a custom counter style named my-roman using the @counter-style rule. Here, we have set the system property to upper-roman to use the uppercase Roman numerals and also set the symbol's property to a string of Roman numerals from I to X.

After this, we applied the custom counter style to the ordered list using the counter-reset attribute, incremented the counter for each list item using the counter-increment attribute, and removed the default list using the list-style attribute Item tag.

Finally, to display the custom list item markup using Roman numerals, we use the :before pseudo-element, via the content attribute and the counter function (which takes two parameters: the name of the counter (section in this case) and the counter style name (in this case roman-numerals)).

The Chinese translation of

Example 2

is:

Example 2

<html>
<head>
   <title>Example to use @counter-style to customize the List Item Markers using CSS</title>
   <style>
      /* removing the default list item markers */
      ul { list-style: none;}
      /* using images as list item markers */
      li:before {
         content: "";
         display: inline-block;
         width: 25px;
         height: 25px;
         background-image: url("yourimage.png");
         background-repeat: no-repeat;
         margin-right: 8px;
      }
   </style>
</head>
<body>
   <h3 id="Example-to-use-counter-style-to-customize-the-List-Item-Markers-using-CSS">Example to use @counter-style to customize the List Item Markers using CSS</h3>
   <p>In this example, we have used the @counter-style rule to customize the list item markers of an ordered list.</p>
   <ol>
      <li>Learn to code in python</li>
      <li>Learn to code in java</li>
      <li>Learn to code in c++</li>
   </ol>
</body>
</html>

In the above example, we used the list-style attribute to remove the default list item markup of the unordered list element. In addition, we also use the :before pseudo-element to display the list items with the help of the content attribute and the empty string.

We have set the display attribute to inline-block to ensure the image displays correctly, and the width and height attributes to the size of the marker image. We use the background-image attribute to specify the URL of the tagged image and use the background-repeat attribute to prevent image duplication. Finally, we added some margin to the right side of the image using the margin-right property.

Conclusion

When dealing with HTML lists, you can use the @counter-style rule in CSS to customize the appearance of the list item tags. This rule provides a simple and flexible way to define custom styles for ordered lists. The syntax of @counter-style rules includes several parameters, such as system, symbols, suffix, prefix and pad. These parameters allow modification of the appearance of list item markers. Using the @counter-style rule makes it easier to create list item markup that matches your current project design needs.

The above is the detailed content of How to customize list items using CSS using @counter-style rules?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software