Bootstrap Pagination

You'll discover how to use Bootstrap to build pagination in this article.


Creating Pagination with Bootstrap

Pagination involves organizing content by dividing it into separate pages.

Pagination is a commonly used technique in various web applications. For instance, search engines use it to display limited results on search result pages, while blogs or forums use it to show a specific number of posts per page.

<div class="m-4">
    <nav>
        <ul class="pagination">
            <li class="page-item"><a href="#" class="page-link">Previous</a></li>
            <li class="page-item"><a href="#" class="page-link">1</a></li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">Next</a></li>
        </ul>
    </nav>
</div>

Pagination with Disabled and Active States

In Bootstrap pagination, links can be customized for different scenarios, such as indicating the current page number or displaying links as disabled.

<div class="m-4">
    <nav>
        <ul class="pagination">
            <li class="page-item disabled"><a href="#" class="page-link" tabindex="-1">Previous</a></li>
            <li class="page-item active"><a href="#" class="page-link">1</a></li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">Next</a></li>
        </ul>
    </nav>
</div>

Note: The .disabled class is utilized to make links appear disabled without removing their click functionality. Alternatively, you can swap active or disabled anchors with spans to achieve specific behavior.

<div class="m-4">
    <nav>
        <ul class="pagination">
            <li class="page-item disabled"><span class="page-link">Previous</span></li>
            <li class="page-item active"><a href="#" class="page-link">1</a></li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">Next</a></li>
        </ul>
    </nav>
</div>

Changing the Sizes of Pagination

You can also adjust the size of pagination to increase or decrease the clickable area.

By adding relative sizing classes like .pagination-lg or .pagination-sm to the .pagination base class, you can create larger or smaller pagination elements.


<div class="m-4">
    <!-- Large pagination -->
    <nav>
        <ul class="pagination pagination-lg">
            <li class="page-item disabled"><span class="page-link">Previous</span></li>
            <li class="page-item active"><a href="#" class="page-link">1</a></li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">Next</a></li>
        </ul>
    </nav>

Alignment of Pagination

The horizontal alignment of pagination is left by default. Add the .justify-content-center class to the .pagination base class, as shown below, to center the content on the page:

<div class="m-4">
    <nav>
        <ul class="pagination justify-content-center">
            <li class="page-item"><a href="#" class="page-link">Previous</a></li>
            <li class="page-item"><a href="#" class="page-link">1</a></li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">Next</a></li>
        </ul>
    </nav>
</div>

By default, pagination aligns horizontally to the left. To center the pagination on the page, add the class .justify-content-center to the .pagination base class. Similarly, the class .justify-content-end can be used to align the pagination to the right.

<div class="m-4">
    <nav>
        <ul class="pagination justify-content-end">
            <li class="page-item"><a href="#" class="page-link">Previous</a></li>
            <li class="page-item"><a href="#" class="page-link">1</a></li>
            <li class="page-item"><a href="#" class="page-link">2</a></li>
            <li class="page-item"><a href="#" class="page-link">3</a></li>
            <li class="page-item"><a href="#" class="page-link">Next</a></li>
        </ul>
    </nav>
</div>

FAQ

What is Bootstrap Pagination?

Bootstrap Pagination is a component provided by the Bootstrap framework, a popular front-end development framework. It enables the division of a large amount of content or data into multiple pages, making it more organized and easier for users to navigate through. Pagination is commonly used in web applications and websites to break down lengthy lists or sets of data, such as search results or blog posts, into smaller, more manageable portions.

How can you customize the appearance of Bootstrap Pagination?

Bootstrap provides various classes to customize the appearance of Pagination. Here are some commonly used classes:

  • .pagination: Applied to the ul element to style the pagination component.
  • .page-item: Applied to each li element that represents a page item.
  • .page-link: Applied to the a element inside each li to style the page link.
  • .active: Applied to the currently active page to highlight it.
  • .disabled: Applied to disabled page items, such as previous and next buttons when not applicable.

You can modify these classes or use utility classes like .btn to make the pagination blend seamlessly with the rest of your design.

How can you handle user interactions with Bootstrap Pagination?

Bootstrap Pagination itself doesn't provide built-in functionality for handling page changes. You typically need to use JavaScript to capture user interactions and update the content accordingly. You can set up event listeners on the pagination links to trigger actions when a user clicks on them. For instance, you can use jQuery to handle click events and load new content through AJAX calls or by toggling visibility of elements.

$('.pagination').on('click', '.page-link', function(e) {
  e.preventDefault();
  
  // Perform actions to load content for the clicked page
  // This could involve AJAX requests or toggling content visibility
});

How can you add navigation arrows to Bootstrap Pagination?

To add navigation arrows (previous and next buttons) to Bootstrap Pagination, you can use the .prev and .next classes provided by Bootstrap. Here's how you can do it:

<ul class="pagination">
  <li class="page-item"><a class="page-link prev" href="#" aria-label="Previous"><span aria-hidden="true">&amp;laquo;</span></a></li>
  <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>
  <!-- Add more page links as needed -->
  <li class="page-item"><a class="page-link next" href="#" aria-label="Next"><span aria-hidden="true">&amp;raquo;</span></a></li>
</ul>

Can you disable certain pagination items using Bootstrap?

Yes, you can disable certain pagination items, such as the previous and next buttons, using the .disabled class. This prevents users from interacting with those items. For instance:

<ul class="pagination">
  <li class="page-item disabled"><a class="page-link prev" href="#" aria-label="Previous"><span aria-hidden="true">&amp;laquo;</span></a></li>
  <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>
  <!-- Add more page links as needed -->
  <li class="page-item disabled"><a class="page-link next" href="#" aria-label="Next"><span aria-hidden="true">&amp;raquo;</span></a></li>
</ul>

How can you handle the display of ellipsis (...) for long pagination sequences?

When dealing with a large number of pages, Bootstrap provides the .disabled class for adding an ellipsis (...) to indicate omitted page numbers. Here's an example:

<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 disabled"><a class="page-link" href="#">...</a></li>
  <li class="page-item"><a class="page-link" href="#">15</a></li>
  <li class="page-item"><a class="page-link" href="#">16</a></li>
  <!-- Add more page links as needed -->
</ul>

You can dynamically generate the ellipsis based on the context of your pagination sequence.

Is Bootstrap Pagination responsive?

Yes, Bootstrap Pagination is designed to be responsive and adapt to different screen sizes. The pagination links and controls will adjust their size and layout according to the available space on the user's device, thanks to Bootstrap's responsive grid system. However, for very small screens, you might need to implement additional responsive design considerations to ensure optimal user experience.

How can you change the number of visible pagination links in Bootstrap?

Bootstrap Pagination has a feature called "maxVisiblePages" which controls the maximum number of visible page links before and after the active page. To change this, you can use the .pagination-sm or .pagination-lg classes to create smaller or larger pagination components respectively.

<ul class="pagination pagination-sm">
  <!-- Pagination links will go here -->
</ul>

How can you style the active page differently in Bootstrap Pagination?

To style the active page differently in Bootstrap Pagination, you can use the .active class. This class highlights the current page. For example:

<ul class="pagination">
  <li class="page-item"><a class="page-link" href="#">1</a></li>
  <li class="page-item active"><a class="page-link" href="#">2</a></li>
  <li class="page-item"><a class="page-link" href="#">3</a></li>
  <!-- Add more page links as needed -->
</ul>

You can then customize the appearance of the active page using CSS.

Can you add additional information or labels to Bootstrap Pagination?

Yes, you can include additional information or labels in Bootstrap Pagination. For example, you can include labels like "Previous" and "Next" for the navigation buttons:

<ul class="pagination">
  <li class="page-item"><a class="page-link" href="#" aria-label="Previous"><span aria-hidden="true">&amp;laquo;</span> Previous</a></li>
  <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>
  <!-- Add more page links as needed -->
  <li class="page-item"><a class="page-link" href="#" aria-label="Next">Next <span aria-hidden="true">&amp;raquo;</span></a></li>
</ul>

How can you align Bootstrap Pagination to the center of a container?

You can align Bootstrap Pagination to the center of a container using CSS. Wrap the pagination component in a container and use flexbox or other centering techniques to achieve the desired alignment:

<div class="pagination-container">
  <ul class="pagination">
    <!-- Pagination links will go here -->
  </ul>
</div>
.pagination-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

Can you create multi-line Bootstrap Pagination for a large number of pages?

By default, Bootstrap Pagination displays page links in a single line. To create multi-line pagination for a large number of pages, you may need to use custom CSS to adjust the layout. You could create multiple rows of pagination links or use CSS to modify the display based on screen width.

Remember that multi-line pagination can impact user experience, so it's important to design it in a way that remains user-friendly and easy to navigate.

Does Bootstrap 5 have any changes to its Pagination component compared to Bootstrap 4?

Bootstrap 5 had introduced some changes to its Pagination component, such as removing the .pagination-lg and .pagination-sm classes in favor of using utilities like .fs-* for font size adjustments. It's recommended to refer to the official Bootstrap documentation for the most up-to-date information on any changes in the Bootstrap 5 Pagination component.

Can you implement server-side pagination with Bootstrap using AJAX?

Yes, you can implement server-side pagination with Bootstrap using AJAX to load content dynamically as users navigate through pages. Here's a general approach:

  • Attach click event handlers to pagination links.
  • In the event handler, prevent the default link behavior (e.preventDefault()).
  • Use AJAX to fetch data from the server based on the clicked page.
  • Replace the content on the page with the fetched data.
$('.pagination').on('click', '.page-link', function(e) {
  e.preventDefault();

  const page = $(this).text(); // Get the clicked page number
  $.ajax({
    url: 'your-server-url',
    method: 'GET',
    data: { page: page }, // Send the page number to the server
    success: function(response) {
      // Update content on the page with the fetched data
    },
    error: function(error) {
      // Handle errors
    }
  });
});

Conclusion

The process of Creating Bootstrap Pagination proves essential for enhancing the user experience when navigating through a multipage interface. With a focus on Navigating Pages and Page Numbering, Bootstrap provides a reliable and user-friendly Paginator that facilitates seamless movement between different sections of content.

The flexibility of Pagination Styles within Bootstrap allows developers to integrate a variety of designs, ensuring a visually appealing and cohesive presentation. Further customization through Customizing Pagination options, such as creating a distinct Page Navigation Bar or incorporating Page Splitting for improved organization, offers a tailored approach to pagination implementation.

The concept of Dynamic Pagination enables the system to adapt to varying content sizes, providing a responsive and adaptive solution. The creation of a Paginated List with Numeric Pagination ensures a structured and intuitive navigation experience, offering users an efficient way to access specific content sections. Beyond the basics, attention to Pagination Elements Formatting and Size Adjustment becomes crucial, enabling the modification of pagination dimensions and the adjustment of sizes to align with the overall design aesthetic.

By employing techniques to Modify Pagination Dimensions and Adjusting Pagination Size, developers can create a cohesive and harmonious visual integration of pagination elements. Customizing Pagination Appearance and exploring various Pagination Styling Options provide the freedom to align pagination with the website's overall theme.

Lastly, the ability to Customize Pagination Alignment ensures a seamless blend with the layout, and the management of Disabled and Active Pagination states adds a layer of interactivity, guiding users through the active section of the content.