Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Bootstrap Pagination with Current Page States

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Create the basic pagination container. Start with a <nav> element and give it an aria-label for accessibility, like aria-label="Page navigation". Inside, place a <ul> with the class pagination.
  2. Add page items. Each page number goes inside an <li> with the class page-item. Inside each <li>, place an <a> (or <span> for non-clickable states) with the class page-link and the page number or label as text.
  3. Mark the current page. On the <li> that represents the page the user is currently on, add the class active. Also add aria-current="page" to the same <li> for screen readers. This will style the item with a highlighted background and border.
  4. Add previous and next buttons. Include a Previous link as the first item and a Next link as the last. Use the same page-item and page-link classes. For the previous button, you can use a left arrow character or the text "Previous".
  5. Disable the previous button on the first page. If the current page is the first page, add the class disabled to the <li> of the previous button, and replace the <a> with a <span> to prevent it from being clickable. Also add tabindex="-1" and aria-disabled="true" for accessibility.
  6. Repeat for the next button on the last page. Similarly, disable the next button when the user is on the last page.
  7. 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 class page-item disabled and a <span> with class page-link containing the ellipsis. This keeps the pagination compact.
  8. 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-link and changing background-color, border-color, and color.
  9. Make it responsive. For small screens, consider hiding some page numbers using utility classes like d-none d-sm-block on the <li> elements. This keeps the pagination usable without overflowing.
  10. 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 only active without aria-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 active and disabled classes 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-lg or pagination-sm classes on the <ul> to change the size.
  • For a more modern look, you can use justify-content-center on 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-label on the <nav> to describe the pagination's purpose, e.g., "Search results pages".
  • Consider adding a title attribute 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 active class 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 add tabindex="-1" to the <li>. Also ensure you're not attaching click handlers to the <li>.
  • Pagination not centered: Add the justify-content-center class to the <ul class="pagination">.
  • Items wrapping awkwardly on mobile: Use responsive utility classes like d-none d-md-block on 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.