Lesson 28 of 60 โ€“ Bootstrap Pagination
47%

Bootstrap Pagination

Bootstrap Pagination is used to divide large amounts of content into multiple pages. It is commonly used for blog posts, product lists, student records, search results, tutorial pages, and database records.

Note: Bootstrap pagination uses classes such as .pagination, .page-item, and .page-link.

1. What Is Pagination?

Pagination divides a large collection of content into smaller pages. Instead of displaying hundreds of records on one page, we can display a limited number of records and provide navigation links.

For example, a student management system may display 10 students per page and provide links for page 1, page 2, page 3, and so on.

2. Basic Pagination

The .pagination class creates the main Bootstrap pagination container.

<nav>

    <ul class="pagination">

        <li class="page-item">
            <a class="page-link" href="#">
            1
            </a>
        </li>

        <li class="page-item">
            <a class="page-link" href="#">
            2
            </a>
        </li>

        <li class="page-item">
            <a class="page-link" href="#">
            3
            </a>
        </li>

    </ul>

</nav>

3. The .pagination Class

The .pagination class is applied to the <ul> element that contains the pagination items.

<ul class="pagination">

    ...

</ul>

It provides the basic layout and styling for the pagination control.

4. The .page-item Class

The .page-item class is used for each individual pagination item.

<li class="page-item">

    ...

</li>

Each page number, Previous button, and Next button is generally placed inside a .page-item.

5. The .page-link Class

The .page-link class styles the clickable pagination link.

<a
class="page-link"
href="#">

1

</a>

6. Previous and Next Buttons

Pagination commonly contains Previous and Next controls.

<ul class="pagination">

    <li class="page-item">

        <a class="page-link"
        href="#">

        Previous

        </a>

    </li>


    <li class="page-item">

        <a class="page-link"
        href="#">

        1

        </a>

    </li>


    <li class="page-item">

        <a class="page-link"
        href="#">

        Next

        </a>

    </li>

</ul>

7. Pagination with Many Pages

You can add multiple page numbers inside the pagination component.

<ul class="pagination">

    <li class="page-item">
    <a class="page-link" href="#">
    1
    </a>
    </li>

    <li class="page-item">
    <a class="page-link" href="#">
    2
    </a>
    </li>

    <li class="page-item">
    <a class="page-link" href="#">
    3
    </a>
    </li>

    <li class="page-item">
    <a class="page-link" href="#">
    4
    </a>
    </li>

    <li class="page-item">
    <a class="page-link" href="#">
    5
    </a>
    </li>

</ul>

8. Active Page

Use the .active class to indicate the current page.

<li
class="page-item active"
aria-current="page">

    <a class="page-link"
    href="#">

    2

    </a>

</li>

9. Disabled Page

Use the .disabled class when a pagination control should appear unavailable.

<li class="page-item disabled"
aria-disabled="true">

    <a class="page-link"
    href="#">

    Previous

    </a>

</li>

10. Pagination with Previous and Next Symbols

You can use symbols such as ยซ and ยป for Previous and Next navigation.

<li class="page-item">

    <a class="page-link"
    href="#">

    &laquo;

    </a>

</li>

<li class="page-item">

    <a class="page-link"
    href="#">

    &raquo;

    </a>

</li>

11. Pagination with Previous Text

You can use readable text instead of symbols for navigation controls.

<li class="page-item">

    <a class="page-link"
    href="#">

    Previous

    </a>

</li>
Tip: Text such as Previous and Next can be easier to understand for some users than symbols alone.

12. Small Pagination

Use .pagination-sm to create a smaller pagination component.

<ul class="pagination pagination-sm">

    <li class="page-item">
    <a class="page-link" href="#">
    1
    </a>
    </li>

    <li class="page-item">
    <a class="page-link" href="#">
    2
    </a>
    </li>

</ul>

13. Large Pagination

Use .pagination-lg to create a larger pagination component.

<ul class="pagination pagination-lg">

    <li class="page-item">
    <a class="page-link" href="#">
    1
    </a>
    </li>

    <li class="page-item">
    <a class="page-link" href="#">
    2
    </a>
    </li>

</ul>

14. Centered Pagination

Flexbox utilities can be used to center a pagination component.

<ul
class="pagination justify-content-center">

    ...

</ul>

15. Right-Aligned Pagination

Use justify-content-end to align pagination toward the end of the available space.

<ul
class="pagination justify-content-end">

    ...

</ul>

16. Left-Aligned Pagination

The default Bootstrap pagination layout starts from the beginning of the available space.

<ul
class="pagination justify-content-start">

    ...

</ul>

17. Pagination with Ellipsis

For a large number of pages, an ellipsis can be used to indicate omitted page numbers.

<li class="page-item">
<a class="page-link" href="#">
1
</a>
</li>

<li class="page-item disabled">
<span class="page-link">
...
</span>
</li>

<li class="page-item">
<a class="page-link" href="#">
10
</a>
</li>

18. Pagination with Database Records

Pagination is very useful when displaying database records. For example, a PHP application may retrieve only 10 students from a database for each page.

<?php

$limit = 10;

$page = 1;

$offset = ($page - 1) * $limit;

$sql = "SELECT * FROM students
        LIMIT $offset, $limit";

?>

The Bootstrap pagination component controls which page number is requested. PHP or another backend language then retrieves the corresponding records.

19. Page Number Using PHP

A PHP application can read the current page number from the URL.

<?php

$page = isset($_GET['page'])
        ? (int)$_GET['page']
        : 1;

if ($page < 1) {
    $page = 1;
}

?>

For example:

students.php?page=2

This tells the server that page 2 should be displayed.

20. Previous and Next with PHP

PHP can generate Previous and Next links dynamically.

<?php

$previous = $page - 1;
$next = $page + 1;

?>

<a
href="students.php?page=<?php echo $previous; ?>"
class="page-link">

Previous

</a>

<a
href="students.php?page=<?php echo $next; ?>"
class="page-link">

Next

</a>

In a real application, you should also check whether the previous or next page actually exists.

21. Pagination Navigation Label

Wrap pagination inside a <nav> element and use aria-label to describe the navigation purpose.

<nav
aria-label="Student records navigation">

    <ul class="pagination">

        ...

    </ul>

</nav>

This improves the meaning of the navigation for assistive technology.

22. Accessible Previous and Next Links

When using symbols instead of words, add accessible labels to explain their meaning.

<a
class="page-link"
href="#"
aria-label="Previous">

&laquo;

</a>

<a
class="page-link"
href="#"
aria-label="Next">

&raquo;

</a>

This gives screen readers meaningful descriptions for the navigation controls.

23. Pagination with Search Results

Pagination is commonly used with search results. For example, a website may display 10 search results on each page.

<nav
aria-label="Search results pages">

    <ul class="pagination">

        <li class="page-item active">
            <a class="page-link"
            href="?page=1">
            1
            </a>
        </li>

        <li class="page-item">
            <a class="page-link"
            href="?page=2">
            2
            </a>
        </li>

        <li class="page-item">
            <a class="page-link"
            href="?page=3">
            3
            </a>
        </li>

    </ul>

</nav>

24. Pagination with Student Records

In an admin panel, pagination can be used to display student records without loading every record at once.

<table class="table">

    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Course</th>
    </tr>

    <tr>
        <td>101</td>
        <td>Rahul</td>
        <td>ADCA</td>
    </tr>

</table>


<nav aria-label="Student pages">

    <ul class="pagination">

        <li class="page-item active">
            <a class="page-link"
            href="#">
            1
            </a>
        </li>

        <li class="page-item">
            <a class="page-link"
            href="#">
            2
            </a>
        </li>

    </ul>

</nav>

25. Pagination with Cards

Pagination can also be used below cards when displaying products, courses, articles, or tutorials.

<div class="card">

    <div class="card-body">

        <h5>Course 1</h5>

        <p>
        Web Development
        </p>

    </div>

</div>


<nav class="mt-3"
aria-label="Course pages">

    <ul class="pagination">

        ...

    </ul>

</nav>

26. Responsive Pagination

Bootstrap pagination is designed to work with responsive layouts. However, when there are many page links, you should avoid creating an overly long navigation control on small screens.

A common solution is to show only the most useful page numbers along with Previous and Next controls.

<nav
aria-label="Responsive pagination">

    <ul class="pagination">

        <li class="page-item">
            <a class="page-link"
            href="#">
            Previous
            </a>
        </li>

        <li class="page-item active">
            <a class="page-link"
            href="#">
            1
            </a>
        </li>

        <li class="page-item">
            <a class="page-link"
            href="#">
            2
            </a>
        </li>

        <li class="page-item">
            <a class="page-link"
            href="#">
            Next
            </a>
        </li>

    </ul>

</nav>

27. Pagination Accessibility

Pagination should be accessible and understandable for all users.

  • Use a <nav> element for pagination navigation.
  • Give the navigation a meaningful aria-label.
  • Use aria-current="page" on the current page.
  • Provide meaningful labels for Previous and Next controls.
  • Do not rely only on color to identify the current page.
  • Keep pagination controls understandable on small screens.
<nav
aria-label="Page navigation">

    <ul class="pagination">

        <li
        class="page-item active"
        aria-current="page">

            <a
            class="page-link"
            href="#">

            1

            </a>

        </li>

    </ul>

</nav>

28. Common Pagination Mistakes

Avoid these common mistakes when creating pagination:

  • Forgetting the .pagination class.
  • Forgetting the .page-item wrapper.
  • Forgetting the .page-link class.
  • Not indicating the current page.
  • Showing too many page numbers.
  • Creating links to pages that do not exist.
  • Not disabling Previous on the first page.
  • Not disabling Next on the last page.
  • Ignoring accessibility.
  • Not preserving search or filter parameters when changing pages.
Tip: When using pagination with search or filters, make sure the page links preserve the current search and filter values.

29. Practical PHP Pagination Structure

A typical PHP pagination system follows these steps:

  1. Read the current page number.
  2. Set the number of records per page.
  3. Calculate the database offset.
  4. Fetch records for the current page.
  5. Calculate the total number of pages.
  6. Generate Bootstrap pagination links.
  7. Disable Previous on the first page.
  8. Disable Next on the last page.
<?php

$limit = 10;

$page = isset($_GET['page'])
        ? (int) $_GET['page']
        : 1;

if ($page < 1) {
    $page = 1;
}

$offset = ($page - 1) * $limit;

$sql = "SELECT *
        FROM students
        LIMIT $offset, $limit";

?>

30. Complete Bootstrap Pagination Example

The following example combines Previous, page numbers, active page, ellipsis, and Next navigation.

<nav
aria-label="Student records pagination">

    <ul class="pagination">


        <li class="page-item disabled"
        aria-disabled="true">

            <a
            class="page-link"
            href="#"
            aria-label="Previous">

            Previous

            </a>

        </li>


        <li
        class="page-item active"
        aria-current="page">

            <a
            class="page-link"
            href="#">

            1

            </a>

        </li>


        <li class="page-item">

            <a
            class="page-link"
            href="#">

            2

            </a>

        </li>


        <li class="page-item">

            <a
            class="page-link"
            href="#">

            3

            </a>

        </li>


        <li class="page-item disabled"
        aria-disabled="true">

            <span class="page-link">
            ...
            </span>

        </li>


        <li class="page-item">

            <a
            class="page-link"
            href="#">

            10

            </a>

        </li>


        <li class="page-item">

            <a
            class="page-link"
            href="#"
            aria-label="Next">

            Next

            </a>

        </li>


    </ul>

</nav>

๐Ÿ“Œ Key Points

  • .pagination creates the main pagination container.
  • .page-item represents an individual pagination item.
  • .page-link styles the clickable page link.
  • Use .active to identify the current page.
  • Use .disabled when a pagination control is unavailable.
  • .pagination-sm creates a smaller pagination component.
  • .pagination-lg creates a larger pagination component.
  • justify-content-center centers pagination.
  • justify-content-end aligns pagination toward the end.
  • Pagination works well with PHP and database records.
  • Use URL parameters such as ?page=2 to identify pages.
  • Use aria-current="page" for the current page.
  • Use meaningful aria-label values for navigation.
  • Keep pagination simple and usable on smaller screens.
  • Pagination should preserve relevant search and filter parameters.

๐Ÿง  Quick Quiz

Question: Which Bootstrap class creates the main pagination container?