Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Center pagination on a web page with CSS
Centering pagination on a web page involves creating a horizontal pagination bar and aligning it to the center of its container. This is commonly achieved using the text-align: center property on the parent container and display: inline-block on the pagination element.
Syntax
.pagination-container {
text-align: center;
}
.pagination {
display: inline-block;
}
Example
The following example demonstrates how to create a centered pagination bar with styled navigation links −
<!DOCTYPE html>
<html>
<head>
<style>
.pagination-container {
text-align: center;
margin: 20px 0;
}
.pagination {
display: inline-block;
}
.pagination a {
color: #333;
padding: 8px 16px;
text-decoration: none;
transition: background-color 0.3s;
border: 1px solid #ddd;
margin: 0 2px;
font-size: 16px;
border-radius: 4px;
}
.pagination a.active {
background-color: #007bff;
color: white;
border-color: #007bff;
}
.pagination a:hover:not(.active) {
background-color: #f8f9fa;
border-color: #007bff;
}
</style>
</head>
<body>
<h2>Centered Pagination Example</h2>
<div class="pagination-container">
<div class="pagination">
<a href="#">« Previous</a>
<a href="#">1</a>
<a href="#">2</a>
<a href="#" class="active">3</a>
<a href="#">4</a>
<a href="#">5</a>
<a href="#">Next »</a>
</div>
</div>
</body>
</html>
A centered pagination bar appears with Previous/Next buttons and numbered pages (1-5). Page 3 is highlighted in blue as the active page. Hovering over non-active pages shows a light background highlight.
Key Points
- Use
text-align: centeron the parent container to center the pagination - Apply
display: inline-blockto the pagination wrapper to make it behave as a single centered unit - Style individual pagination links with padding, borders, and hover effects for better user experience
Conclusion
Centering pagination is straightforward using text-align: center on the container and display: inline-block on the pagination element. This approach ensures the pagination remains centered regardless of the number of page links.
Advertisements
