Bootstrap Pagination with Current Page States
In this article
This guide shows you how to build Bootstrap pagination that clearly highlights the active page. You'll learn the exact classes and attributes needed, plus how to handle edge cases like disabled states and responsive behavior. No prior JavaScript knowledge is required, just a basic understanding of HTML and CSS.
What You'll Need
- Bootstrap CSS (version 4 or 5) linked in your HTML file, either via CDN or locally.
- A text editor or IDE.
- Basic familiarity with HTML structure and classes.
Step-by-Step
- Create the basic pagination container. Start with a
<nav>element and give it anaria-labelfor accessibility, likearia-label="Page navigation". Inside, place a<ul>with the classpagination. - Add page items. Each page number goes inside an
<li>with the classpage-item. Inside each<li>, place an<a>(or<span>for non-clickable states) with the classpage-linkand the page number or label as text. - Mark the current page. On the
<li>that represents the page the user is currently on, add the classactive. Also addaria-current="page"to the same<li>for screen readers. This will style the item with a highlighted background and border. - Add previous and next buttons. Include a
Previouslink as the first item and aNextlink as the last. Use the samepage-itemandpage-linkclasses. For the previous button, you can use a left arrow character or the text "Previous". - Disable the previous button on the first page. If the current page is the first page, add the class
disabledto the<li>of the previous button, and replace the<a>with a<span>to prevent it from being clickable. Also addtabindex="-1"andaria-disabled="true"for accessibility. - Repeat for the next button on the last page. Similarly, disable the next button when the user is on the last page.
- Add ellipsis for many pages. If you have many pages (e.g., 20), you can use an ellipsis (
…) as a non-clickable item. Add a<li>with classpage-item disabledand a<span>with classpage-linkcontaining the ellipsis. This keeps the pagination compact. - Style the active state (optional). Bootstrap's default active state uses the primary color. You can override it with custom CSS by targeting
.page-item.active .page-linkand changingbackground-color,border-color, andcolor. - Make it responsive. For small screens, consider hiding some page numbers using utility classes like
d-none d-sm-blockon the<li>elements. This keeps the pagination usable without overflowing. - Test the states. In your browser, check that the active page is highlighted, the previous/next buttons are disabled appropriately, and all links work (if you have a JavaScript handler).
Common Mistakes
- Forgetting
aria-current: Adding onlyactivewithoutaria-current="page"makes it inaccessible to screen reader users. - Using
<a>for disabled items: A disabled link still appears in the tab order and can be clicked. Use<span>instead. - Not nesting correctly: The
activeanddisabledclasses must be on the<li>, not the<a>or<span>. - Putting the pagination outside a
<nav>: This loses semantic meaning and can confuse assistive tech. - Hardcoding page numbers without logic: If you're generating pagination dynamically, ensure the active class is applied based on the current page variable, not hardcoded.
Tips
- Use Bootstrap's built-in
pagination-lgorpagination-smclasses on the<ul>to change the size. - For a more modern look, you can use
justify-content-centeron the<ul>to center the pagination. - When building with JavaScript, always update the active class and disabled states dynamically when the page changes.
- Use the
aria-labelon the<nav>to describe the pagination's purpose, e.g., "Search results pages". - Consider adding a
titleattribute to the previous/next links for extra clarity on hover.
Troubleshooting
- Active style not showing: Check that you have the correct Bootstrap CSS version and that the
activeclass is on the<li>, not the<a>. Also verify no custom CSS is overriding it. - Disabled link still clickable: Replace the
<a>with a<span>and addtabindex="-1"to the<li>. Also ensure you're not attaching click handlers to the<li>. - Pagination not centered: Add the
justify-content-centerclass to the<ul class="pagination">. - Items wrapping awkwardly on mobile: Use responsive utility classes like
d-none d-md-blockon the<li>elements to hide less important page numbers on small screens. - Bootstrap styles not applying at all: Ensure the Bootstrap CSS link is correctly placed in the
<head>and that there are no typos in the class names.
Frequently Asked Questions
How do I change the color of the active page?
Override the CSS rule for .page-item.active .page-link. Set your desired background-color, border-color, and color.
Can I use Bootstrap pagination with React or Vue?
Yes, you can use the same classes, but you'll typically generate the HTML dynamically using the framework's templating syntax. The classes remain the same.
How do I make the pagination clickable to load new content?
Attach a click event handler to each .page-link (except disabled ones) that fetches new data and updates the pagination state. You'll need to re-render the active class after each click.
What is the difference between active and disabled?
active highlights the current page and is styled with a solid background. disabled makes an item non-interactive and visually muted, typically used for the previous/next buttons at the ends.
Is it necessary to use aria-current?
Yes, for accessibility. It tells screen readers that this specific item represents the current page, which is important for users navigating with assistive technology.
How do I show a large number of pages without clutter?
Use ellipsis items and only show a window of page numbers around the current page. For example, show pages 1, …, 4, 5, 6, …, 20.