Yogesh Chauhan's Blog

How to replace HTML lists using CSS Counters?

in CSS & HTML on April 29, 2020

CSS counters are type of “variables”. They are maintained by CSS and their values can be incremented by CSS rules.

Their values are based on how many times they are used on the page.

CSS counters let you adjust the appearance of content based on its location in a document.

For example, you can use counters to automatically number the headings in a webpage. 

CSS Counter Properties

counter-reset – Creates or resets a counter

counter-increment – Increments a counter value

content – Inserts generated content

counter() or counters() function – Adds the value of a counter to an element

To use a CSS counter, it must first be initialized to a value with the counter-reset property (0 by default).

The same property can also be used to change its value to any specific number.

Once initialized, a counter’s value can be increased or decreased with counter-increment.

The counter’s name must not be “none”, “inherit”, or “initial”; otherwise the declaration is ignored.

The value of a counter can be displayed using either the counter() or counters() function in a content property.

counter() function

The counter() function has two forms: ‘counter(name)’ or ‘counter(name, style)’.

The generated text is the value of the innermost counter of the given name in scope at the given pseudo-element.The counter is rendered in the specified style (decimal by default).

counters() function

The counters() function also has two forms: ‘counters(name, string)’ or ‘counters(name, string, style)’.

The generated text is the value of all counters with the given name in scope at the given pseudo-element, from outermost to innermost, separated by the specified string.

The counters are rendered in the specified style (decimal by default).

Example

Nesting counters

A CSS counter can be especially useful for making outlined lists, because a new instance of the counter is automatically created in child elements.

Using the counters() function, separating text can be inserted between different levels of nested counters.

Example

Sources


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to use $INSERT command in Envision Basic?Envision BasicHow to show widgets on the Appearance tab in WordPress?WordPressHow to Block IPs and User Agents using code in Drupal or WordPress?Drupalinclude, include_once, require, require_once in PHPPHPHow to add Google Static Map using ACF map field?WordPressAccessing and Setting features of JavaScript ObjectsJavaScript