Home >Web Front-end >CSS Tutorial >How to customize list items using CSS using @counter-style rules?

How to customize list items using CSS using @counter-style rules?

PHPz
PHPzforward
2023-09-10 21:25:021479browse

如何使用 @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>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>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.com. If there is any infringement, please contact admin@php.cn delete