Pagination

CSS pagination is a technique of creating page numbers for a website. This help users to easily navigate between large amounts of content. In this chapter, we will learn how to setup and style pagination using CSS.

CSS Pagination Example

Here is a example of pagination styled using CSS.

12345

First Page

Table of Contents

  • Setup Pagination For a Website
  • Simple Pagination Example
  • Styling Pagination Using CSS
  • Rounded Pagination Buttons
  • Hoverable Transition Effect
  • Bordered Pagination
  • Space Between Links
  • Pagination Size
  • Centered Pagination
  • Next and Previous Buttons
  • Breadcrumb Pagination
  • Pagination With List

How to Setup Pagination For a Website?

To set up pagination for a website, you need to break your content into small pages and provide navigation to move between them in every page. Let’s see step by step procedure for setting up pagination.

Setup HTML Structure

First we need to set HTML structure for pagination setup. We can use anchor tags enclosed in div elements for this. Following is code for HTML structure of a pagination setup.

<div class="pagination"><a href="#">«</a><!-- Previous Page --><a href="#" class="active">1</a><a href="#">2</a><a href="#">3</a><a href="#">»</a><!-- Next Page --></div><div id="page1" class="page active">Page 1 Shows....</div><div id="page2" class="page">Page 2 Shows....</div>

Control Visibility With CSS

Initially all the pages should be invisible, expect the first page. For this, we can use display property as none for all pages. As class “active” is added to first page, it will be visible initially.

.pagination{display: flex;justify-content: center;}.page{display: none;}.active{display: block;}

Pagination Logic With JavaScript

Now, we need to add some JavaScript to handle pagination logic. We can capture click event on pagination links using JavaScript addEventListener() method and change visibility of pages based on that.

Simple Pagination Example

The example belows shows simple pagination setup developed using the above steps.

Example

<!DOCTYPE html><html><head><style>
    body{
        height: 150px;
    }
    .pagination {
        display: flex;            
        justify-content: center;    
        margin: 20px 0;            
    }
    .pagination a {
        color: green;               
        border: 5px solid #ddd;     
        padding: 5px 10px;         
        margin: 0 5px;           
    }
    .page {
        display: none;         
    }
    .active {
        display: block;      
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#page1"&gt;1&lt;/a&gt;&lt;a href="#page2"&gt;2&lt;/a&gt;&lt;a href="#page3"&gt;3&lt;/a&gt;&lt;a href="#page4"&gt;4&lt;/a&gt;&lt;a href="#page5"&gt;5&lt;/a&gt;&lt;/div&gt;&lt;div id="page1" class="page active"&gt;Page 1 Shows....&lt;/div&gt;&lt;div id="page2" class="page"&gt;Page 2 Shows....&lt;/div&gt;&lt;div id="page3" class="page"&gt;Page 3 Shows....&lt;/div&gt;&lt;div id="page4" class="page"&gt;Page 4 Shows....&lt;/div&gt;&lt;div id="page5" class="page"&gt;Page 5 Shows....&lt;/div&gt;&lt;script&gt;
    document.addEventListener('DOMContentLoaded', function() {
        const pages = document.querySelectorAll('.pagination a');
        const contentPages = document.querySelectorAll('.page');
        pages.forEach(page =&gt; {
            page.addEventListener('click', function(event) {
                event.preventDefault();
                // Remove 'active' class from all content pages
                contentPages.forEach(p =&gt; p.classList.remove('active'));
                // Find the target page and display it
                const targetPage = document.querySelector(this.getAttribute('href'));
                if (targetPage) {
                    targetPage.classList.add('active');
                }
                // Add 'active' class to the clicked link
                this.classList.add('active');
            });
        });
    });
&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Styling Pagination Using CSS

To style pagination links, we can use pseudo-class :active and :hover.

  • Pseudo-class :active Can be used to highlight current page in pagination links.
  • Pseudo-class :hover Can be used to style mouse hover states of pagination link.

The following example shows how these properties can enhance appearance of pagination links.

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Rounded Pagination Buttons

We can create a pagination links with a rounded button by using border-radius CSS property. Let see an example:

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        border-radius: 8px;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Hoverable Transition Effect

We can make pagination link smooth with transition effects when hovering over the each page links using transition property. Let see an example:

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        transition: background-color 0.4s;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Bordered Pagination

To add border to pagination links, we can use CSS border property. Here is an example:

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        transition: background-color 0.4s;
        border: 2px solid black;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

We can use CSS margin property to create a space around each link in the pagination component. Let see an example for this.

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        transition: background-color 0.4s;
        border: 2px solid black;
        margin: 2px;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Pagination Size

To change the size of pagination links, we can use font-size property. Let see an example:

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        border: 2px solid black;
        font-size: 20px;
        margin: 2px;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Centered Pagination

We can use the justify-content CSS property to center pagination links. Here is an example.

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
        justify-content: center;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        transition: background-color 0.7s;
        border: 2px solid black;
        margin: 2px;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#"&gt;«&lt;/a&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;a href="#"&gt;»&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Pagination With Next and Previous Buttons

We can add previous and next buttons across pagination links for quicker navigation. Let see an example for this.

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        list-style: none;
        padding: 0;
        margin: 10px;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        transition: background-color 0.4s;
        border: 2px solid black;
        margin: 2px;
    }
    .prev-next{
        background-color: grey;
    }
    .pagination a.active-link {
        background-color: violet;
        color: white;
    }
    .pagination a:hover:not(.active-link) {
        background-color: pink;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;div class="pagination"&gt;&lt;a href="#" class="prev-next"&gt;&lt; Previous&lt;/a&gt;&lt;a href="#" class="active-link"&gt;Page 1&lt;/a&gt;&lt;a href="#"&gt;Page 2&lt;/a&gt;&lt;a href="#"&gt;Page 3&lt;/a&gt;&lt;a href="#"&gt;Page 4&lt;/a&gt;&lt;a href="#" class="prev-next"&gt;Next &gt;&lt;/a&gt;&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Breadcrumb pagination is a navigation method that shows users the path theyve taken to reach their current page. Let's see an example to design breadcrumb pagination.

Example

<!DOCTYPE html><html><head><style>
    ul.breadcrumb-pagination {
        padding: 10px;
        list-style: none;
        background-color: pink;
    }
    ul.breadcrumb-pagination li {
        display: inline-block;
    }
    ul.breadcrumb-pagination li a {
        color: blue;
    }
    ul.breadcrumb-pagination li+li:before {
        padding: 10px;
        content: "/\00a0";
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul class="breadcrumb-pagination"&gt;&lt;li&gt;&lt;a href="#"&gt;Tutorialspoint&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;CSS Pagination&lt;/a&gt;&lt;/li&gt;&lt;li class="active-link"&gt;CSS Pagnation With Breadcrumb&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Pagination With List

We can also use unordered list (<ul>) and list items (<li>) for creating the pagination. Let see an example.

Example

<!DOCTYPE html><html><head><style>
    .pagination {
        display: flex;
        padding: 0;
        list-style: none;
    }
    .pagination li {
        margin: 5px;
    }
    .pagination a {
        text-decoration: none;
        padding: 8px 12px;
        color: #333;
        border: 2px solid black;
    }
    .pagination a:hover {
        background-color: pink;
    }
    .pagination .active-link {
        background-color: violet;
        color: white;
    }
&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;ul class="pagination"&gt;&lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#" class="active-link"&gt;A&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;B&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;C&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;D&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;E&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#"&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/body&gt;&lt;/html&gt;</pre>

Comments

Leave a Reply

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