Styling Ordered List Numbers with CSS Counters

Dec 08, 2025 · Programming · 7 views · 7.8

Keywords: CSS | HTML | List Styling | Counters

Abstract: This article explains how to use CSS counters and :before pseudo-elements to style numbers in ordered lists, offering a step-by-step guide with code examples as an alternative to image-based approaches.

Introduction to Styling Ordered List Numbers

In web development, customizing the appearance of ordered list numbers can be challenging. The standard list-style property has limited options, prompting developers to explore methods like CSS counters.

Understanding CSS Counters

CSS counters allow for automatic numbering in stylesheets. By combining counter-reset and counter-increment with the :before pseudo-element, numbers can be dynamically generated and styled.

Implementation with Code Examples

First, reset the counter for the ordered list:

ol {
  list-style: none;
  counter-reset: item;
}

Then, increment the counter for each list item:

li {
  counter-increment: item;
  margin-bottom: 5px;
}

Next, use the :before pseudo-element to display the counter with custom styles:

li:before {
  content: counter(item);
  background: lightblue;
  border-radius: 100%;
  color: white;
  width: 1.2em;
  text-align: center;
  display: inline-block;
  margin-right: 10px;
}

This approach applies CSS properties such as background, border-radius, and color to the numbers, eliminating the need for external images.

Benefits and Comparison

Compared to using list-style-image with individual images, the CSS counter method is more efficient, maintainable, and scalable. It reduces resource overhead and adapts to various screen sizes.

Conclusion

Leveraging CSS counters and pseudo-elements enables developers to easily style ordered list numbers, enhancing visual design while maintaining code simplicity and performance.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.