CSS Pagination
This lesson builds a responsive pagination with CSS.
A site with a large number of pages can benefit from having pagination on each one.
The usual markup is an unordered list (<ul>) holding a series of links, with one link per page number, plus "previous" and "next" controls.
Example
A basic version:
Output: complete HTML page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Pagination</title>
<style>
.pagination {
display: flex;
justify-content: center;
list-style: none;
padding: 0;
}
.pagination li a {
display: block;
padding: 8px 12px;
text-decoration: none;
border: 1px solid gray;
color: black;
margin: 0 4px;
border-radius: 5px;
}
</style>
</head>
<body>
<h2>Simple Pagination</h2>
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">»</a></li>
</ul>
</body>
</html>
Example Explained
Three declarations on the pagination container do the layout:
display: flex;places the page numbers in a horizontal rowjustify-content: center;centers that rowlist-style: none;drops the list bullets
The look of the page numbers comes from the <a> inside each <li>, styled with properties such as display, padding, text-decoration, border, color, and border-radius.
Pagination With an Active Class
An .active class marks the current page, so users can tell where they are.
Example
The same list, now with an .active class:
Output: complete HTML page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagination With Active Class</title>
<style>
.pagination {
display: flex;
justify-content: center;
list-style: none;
padding: 0;
}
.pagination li a {
display: block;
padding: 8px 12px;
text-decoration: none;
border: 1px solid gray;
color: black;
margin: 0 4px;
border-radius: 5px;
}
.pagination li a.active {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h2>Pagination With Active Class</h2>
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a href="#">1</a></li>
<li><a href="#" class="active">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">»</a></li>
</ul>
</body>
</html>
Pagination With a Disabled Class
On the last page, the "Next" button should not work.
A .disabled class handles this: its color, cursor, and pointer-events settings leave "Next" unclickable.
Example
The list again, with a .disabled class:
Output: complete HTML page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pagination With Disabled Class</title>
<style>
.pagination {
display: flex;
justify-content: center;
list-style: none;
padding: 0;
}
.pagination li a {
display: block;
padding: 8px 12px;
text-decoration: none;
border: 1px solid gray;
color: black;
margin: 0 4px;
border-radius: 5px;
}
.pagination li a.active {
background-color: #4CAF50;
color: white;
}
.pagination li a.disabled {
color: #dddddd;
cursor: not-allowed;
pointer-events: none;
}
</style>
</head>
<body>
<h2>Pagination With Disabled Class</h2>
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#" class="active">5</a></li>
<li><a href="#" class="disabled">»</a></li>
</ul>
</body>
</html>