For us to be able to create a simple pagination with HTML and CSS, let us talk on what importance it is to a website
What Is Pagination?
Pagination is way or a technique used to separate or divide larger content into a smaller set and manageable. It is mostly used when the content of a given page is larger to be shown at once. This however divides the content into subpages, making it easier for a user to navigate the website. Websites like blogs often have pagination to be able to contain certain part of the data at a given period of time.
Pagination is more effective with the help of a backend language, which can easily retrieve data (URL) from the database and place them in the corresponding pagination numbers.
For a start we’ll be creating a simple pagination (manual) and in a later chapter, we will be able to integrate it together with a backend programming language, making it dynamic
How to Build a Pagination Page
To begin, we will be using HTML, and CSS to create our pagination page, with HTML for the layout and CSS for the design
Steps involve
- Create a file (pagination.html) and add the necessary HTML attributes (<!DOCTYPE html>, <head>, <style>, <body>)
- Create a <div> and give it a class (pagination).
- Create anchor links with value from 1-4.
Sample Code
<div class=”pagination”>
<a href=”#”>«</a>
<a href=”#”>1</a>
<a href=”#”>2</a>
<a href=”#”>3</a>
<a href=”#”>4</a>
<a href=”#”>»</a>
</div>
The « and » are a decode for << and >>
Moving to the styling, adding just background color, hover property, and color should do
Sample CSS
<style>
.pagination a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
}
.pagination a:hover:not(.active) {background-color: red;}
</style>
Putting the HTML and CSS together, we should have something like this
Complete Code
<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<style>
.pagination a {
color: black;
padding: 8px 16px;
text-decoration: none;
transition: background-color .3s;
}
.pagination a:hover:not(.active) {background-color: red;}
</style>
</head>
<body>
<h2>My Pagination</h2>
<div class=”pagination”>
<a href=”#”>«</a>
<a href=”#”>1</a>
<a href=”#”>2</a>
<a href=”#”>3</a>
<a href=”#”>4</a>
<a href=”#”>»</a>
</div>
</body>
</html>
Now you can manually insert you links to the corresponding pages.
NB:
If the page was dynamic, all we needed to do is add a function that inserts the corresponding links to their various positions automatically.
Feel free to make any adjustment that suits your needs!!
Leave a Reply