Pagination splits a long list, such as search results or blog posts, into numbered pages with links between them. In Bootstrap 5 the component needs no JavaScript: it is a list of links with a few classes.
Page setup
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap pagination</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<main class="container py-4">
<!-- pagination goes here -->
</main>
</body>
</html>
Pagination with active and disabled states
<nav aria-label="Search results pages">
<ul class="pagination">
<li class="page-item disabled">
<a class="page-link" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li class="page-item active">
<a class="page-link" href="?page=1" aria-current="page">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>
<li class="page-item"><a class="page-link" href="?page=4">4</a></li>
<li class="page-item"><a class="page-link" href="?page=5">5</a></li>
<li class="page-item">
<a class="page-link" href="?page=2" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
Compared with Bootstrap 3, every <li> now needs .page-item and every link .page-link. Without them you get an unstyled bullet list.
The accessibility attributes follow Bootstrap’s documentation. The <nav> element with its own aria-label makes the block a named landmark, which matters when a page has more than one navigation area. aria-current="page" tells a screen reader which page is showing. The arrow characters are hidden from screen readers and replaced by “Previous” and “Next” through aria-label, because “left-pointing double angle quotation mark” helps nobody.
The disabled “previous” link has no href. Bootstrap’s .disabled class only switches off mouse clicks with pointer-events: none; a link with an href can still be reached with the Tab key and opened with Enter. Removing the href takes it out of the tab order.
Size and alignment
Add .pagination-lg or .pagination-sm to the <ul> for bigger or smaller links. The list is a flex container, so the flexbox utilities position it: .justify-content-center centers it and .justify-content-end moves it to the right.
<nav aria-label="Archive pages">
<ul class="pagination pagination-sm justify-content-center">
<li class="page-item"><a class="page-link" href="?page=1">1</a></li>
<li class="page-item active"><a class="page-link" href="?page=2" aria-current="page">2</a></li>
<li class="page-item"><a class="page-link" href="?page=3">3</a></li>
</ul>
</nav>
Replacing the old pager
The .pager component, with its rounded “Previous” and “Next” buttons, was removed in Bootstrap 4. Two buttons pushed to opposite edges do the same job:
<nav class="d-flex justify-content-between" aria-label="Post navigation">
<a class="btn btn-outline-secondary" href="/older-post/">← Previous</a>
<a class="btn btn-outline-secondary" href="/newer-post/">Next →</a>
</nav>
Rendering page links in ASP.NET Core
Hard-coded page numbers are only useful in a demo. With hundreds of pages you also want a window of, say, five numbers around the current page rather than all of them. This helper works out which numbers to show:
public static class Paging
{
public static IEnumerable<int> Window(int current, int totalPages, int size = 5)
{
int start = Math.Max(1, Math.Min(current - size / 2, totalPages - size + 1));
int end = Math.Min(totalPages, start + size - 1);
return Enumerable.Range(start, Math.Max(0, end - start + 1));
}
}
On page 1 of 10 it returns 1 to 5, on page 10 it returns 6 to 10, and with only three pages it returns 1 to 3.
In a Razor view, assuming the model has Page and TotalPages properties:
<nav aria-label="Search results pages">
<ul class="pagination">
<li class="page-item @(Model.Page == 1 ? "disabled" : "")">
<a class="page-link" href="@(Model.Page > 1 ? $"?page={Model.Page - 1}" : null)" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
@foreach (int number in Paging.Window(Model.Page, Model.TotalPages))
{
<li class="page-item @(number == Model.Page ? "active" : "")">
<a class="page-link" href="?page=@number" aria-current="@(number == Model.Page ? "page" : null)">@number</a>
</li>
}
<li class="page-item @(Model.Page >= Model.TotalPages ? "disabled" : "")">
<a class="page-link" href="@(Model.Page < Model.TotalPages ? $"?page={Model.Page + 1}" : null)" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
The trick is in the null values. When an attribute’s whole value is a Razor expression that evaluates to null, Razor leaves the attribute out entirely. That removes the href from disabled links and aria-current from every page except the current one, with no if blocks around the markup.
One more trap on the server side: validate the page query parameter. A request for page 0 or page 9999 should be clamped or answered with a 404, not passed straight into Skip((page - 1) * pageSize).
Related examples
- Simple Bootstrap table
- Striped Bootstrap table
- Bootstrap tab menu
- Pagination in the Bootstrap documentation