Bootstrap Pagination: Build & Adapt a Page Link Bar
In this article
Bootstrap pagination lets you create a page link bar quickly using the .pagination class on a <ul> element, with each page link wrapped in an .page-item and .page-link. You can adapt it for different sizes, alignments, and states (active, disabled) by adding utility classes and modifier classes. This article explains the core structure, how to customize it, common pitfalls, and how to make it work with dynamic content.
How Bootstrap Pagination Works
Bootstrap’s pagination component is built with flexbox. The base markup is a <ul> with the class pagination, containing <li> elements with the class page-item. Inside each <li>, an <a> or <button> gets the class page-link. The browser’s default list styling is removed, and the links become block-level elements with padding and margins, creating a horizontal bar.
For example, a basic three-page pagination bar looks like this:
<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>
To indicate the current page, add active to the li and set aria-current="page" on the link. To disable a link (e.g., a “Previous” button on the first page), add disabled to the li and tabindex="-1" to the link.
Adapting Size and Alignment
Bootstrap provides two size modifiers: pagination-lg and pagination-sm. Add them to the <ul> to make the links larger or smaller. For alignment, use the flexbox utilities: wrap the pagination in a <div> and apply d-flex justify-content-center or justify-content-end.
For example, a centered, large pagination bar:
<div class="d-flex justify-content-center"> <ul class="pagination pagination-lg"> ... </ul> </div>
You can also change the spacing between items by overriding the CSS variable --bs-pagination-item-margin or by adding custom utility classes.
Adding Icons and Text for Previous/Next
Common practice is to include “Previous” and “Next” links. You can use text, icons (like the Bootstrap Icons chevron), or both. For accessibility, always include a aria-label on the link and use aria-hidden="true" on the icon.
<li class="page-item">
<a class="page-link" href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="visually-hidden">Previous</span>
</a>
</li>
This pattern ensures screen readers announce the action while visual users see the icon.
Dynamic Pagination with JavaScript
When building a pagination bar for dynamic content (e.g., a search result list), you typically generate the page links with JavaScript. A common approach is to calculate the total number of pages from the total item count and the page size, then loop to create the li elements. You also need to update the active state and disable the previous/next buttons based on the current page.
Here’s a minimal JavaScript example that generates a pagination bar for 10 pages:
const totalPages = 10;
let currentPage = 1;
const pagination = document.getElementById('pagination');
function renderPagination() {
let html = '';
for (let i = 1; i <= totalPages; i++) {
html += `<li class="page-item ${i === currentPage ? 'active' : ''}"><a class="page-link" href="#" data-page="${i}">${i}</a></li>`;
}
pagination.innerHTML = html;
}
pagination.addEventListener('click', (e) => {
if (e.target.tagName === 'A') {
e.preventDefault();
currentPage = parseInt(e.target.dataset.page);
renderPagination();
}
});
renderPagination();
This snippet re-renders the entire list on each click. For large datasets, consider updating only the active class to avoid unnecessary DOM manipulation.
Common Mistakes and How to Avoid Them
- Forgetting the
page-itemwrapper: Thepage-linkclass only works correctly when inside apage-itemli. Putting it directly on anawithout the wrapper breaks the styling. - Using
disabledon the link instead of theli: Bootstrap’s CSS targets theli.disabledto change cursor and color. Applyingdisabledto theaalone won’t have the same effect. - Not using
aria-current: For accessibility, the active page link should havearia-current="page". This helps screen reader users know which page they are on. - Hardcoding page numbers: For dynamic content, always generate the page count from the data. Hardcoding can lead to broken links when the data changes.
Customizing Colors and Borders
Bootstrap pagination uses CSS variables for theming. You can override --bs-pagination-color, --bs-pagination-bg, --bs-pagination-border-color, and --bs-pagination-hover-color in your own stylesheet. For example, to make the active page green:
.pagination {
--bs-pagination-active-bg: #28a745;
--bs-pagination-active-border-color: #28a745;
}
You can also change the border radius by modifying --bs-pagination-border-radius or by adding a utility class like rounded-0 to the ul.
Alternatives to Bootstrap Pagination
If you need more advanced features like ellipsis (“…”), jump-to-page input, or infinite scrolling, consider using a dedicated JavaScript library like DataTables or a custom solution. Bootstrap’s pagination is intentionally simple; it doesn’t include built-in logic for collapsing page numbers. For very large page counts, you may want to show a window of pages (e.g., 1 … 5 6 7 … 20) to avoid a long bar.
Frequently Asked Questions
How do I make pagination responsive?
Bootstrap pagination is responsive by default because it uses flexbox and wraps if necessary. However, for very long bars, you can use flex-wrap on the ul or hide some page numbers on small screens using d-none d-sm-block on individual li elements.
Can I use buttons instead of links?
Yes, you can use <button> elements instead of <a> inside .page-link. This is useful when you don’t want to change the URL. Just ensure the button has type="button" to avoid form submission.
Why is my pagination not centered?
Bootstrap’s pagination is a flex container, but it’s not full-width by default. To center it, wrap it in a div with d-flex justify-content-center. Alternatively, add justify-content-center directly to the ul if it’s inside a full-width container.
How do I show ellipsis for many pages?
Bootstrap doesn’t have a built-in ellipsis component. You can manually add a li with disabled and a span containing “…” or use a JavaScript library that generates collapsed pagination automatically.
Does Bootstrap 5 still support pagination?
Yes, Bootstrap 5 includes the pagination component with the same basic structure. The main difference from Bootstrap 4 is that it uses CSS variables for customization and requires no jQuery.
Can I use pagination with a table?
Yes, you can place a pagination bar below a table. You’ll need to write JavaScript to slice the table rows based on the current page. There’s no built-in table pagination in Bootstrap, but it’s a common pattern.