Automatic serial number in HTML table – CSS Counter

Here we will discuss how to get serial number automatically in a column in the html table. CSS counters are “variables” maintained by CSS whose values can be incremented by CSS rules (to track how many times they are used). Counters let you adjust the appearance of content based on its placement in the document.

Program :

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		body
		{
		    counter-reset: Serial;          
		}

		table
		{
		    border-collapse: separate;
		}

		tr td:first-child:before
		{
		  counter-increment: Serial;      
		  content: counter(Serial); 
		}
	</style>
</head>
<body>
	<table border="1" cellpadding="10">
		<tr>
			<th>Sl.No</th>
			<th>Name</th>
			<th>Age</th>
		</tr>
		<tr>
			<td></td>
			<td>John</td>
			<td>30</td>
		</tr>
		<tr>
			<td></td>
			<td>Sam</td>
			<td>28</td>
		</tr>
		<tr>
			<td></td>
			<td>Siya</td>
			<td>25</td>
		</tr>
	</table>
</body>
</html>

Following are the properties of CSS counters :

  • 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

Output :

css counter

We can use this same for any other html element like div, span, heading, etc. Let’s have a look at div

Program :

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		body
		{
		    counter-reset: Serial;    
		}

		div::before {
		  counter-increment: Serial;
		  content: "Section " counter(Serial) ": ";
		}
	</style>
</head>
<body style="margin:20%">
	
	<h3>Serial Number using CSS Counters:</h3>
	<div>Php Programs</div>
	<div>CSS Examples</div>
	<div>jQuery Programs</div>

</body>
</html>

Output :

css counter

To use multiple counters in same page, specify the counter name in the counter-reset property without delimiters as counter-reset: Serial Section;

CSS Code :

body
{
   counter-reset: Serial Section;      
}

table
{
    border-collapse: separate;
}

tr td:first-child:before
{
    counter-increment: Serial;      
    content: counter(Serial); 
}

div::before {
    counter-increment: Section;
    content: "Section " counter(Section) ": ";
}

For more CSS programs Click here

To know more about Css Counters Click here

Related Posts

sum of datatable column

Calculate sum of Datatable columns and display in footer – Easy method with sum()

In this example let’s check how to calculate sum of datatable columns (multiple columns) and display the same in footer. In some cases we may need to…

jquery datatable

jQuery Datatable – Easy Steps

Here is simple example to create jQuery Datatable. jQuery Datatable organizes data in grid like format of rows and columns. It is very organized, interactive and intutive….

scroll to top on button click

Scroll to top on button click

How to scroll to top on button click? When we are at the bottom of our page instead of scrolling mouse it is very easy and user-friendly…

disable/enable submit button

jQuery disable/enable submit button

How to disable/enable submit button using jQuery? When we want to submit the form after filling all the mandatory data, then the easiest method is to disable…

Dropdown Tree

Dropdown Tree with Bootstrap and jQuery, Easy implementation

Implement an interactive DropDownTree with user-friendly UI. The DropDownTree is used to represent data in heirarchical structure, rendered in a tree-like structure, which provides multiple selection option and custom…

Bootstrap button loading – very easy method

How to create a bootstrap button loading or spinner In this tutorial we will discuss how to show loading animation on buttons. HTML : jQuery : Bootstrap…

Leave a Reply

Your email address will not be published. Required fields are marked *