Back
Beautiful CSS pagination and how it's done
Simple tutorial on how to create a pagination for page scrolling with beautiful colors.
What this pagination does
Five round page numbers in warm colors, from yellow to brown. The current page is larger and red, and the others grow slightly on hover. The pagination is a list of real links inside a labelled<nav>, so it works with the keyboard and screen readers announce which page is current. The demo above is the same code you can copy below.HTML
The page numbers are links inside a list, wrapped in a<nav> with aria-label="Pagination". The nav makes it a navigation landmark that screen reader users can jump to, and the list lets them hear how many pages there are.
Each link points to its page URL, here ?page=1 to ?page=5. Because they are links, Tab moves between them, Enter opens a page, and they can be opened in a new tab. The aria-label makes a screen reader say “Page 2” instead of only “2”.
The current page has aria-current="page". Screen readers announce it as the current page, and the CSS below uses the same attribute for the active style, so we don't need a separate active class.
<nav class="pagination" aria-label="Pagination">
<ul class="pages">
<li><a class="page" href="?page=1" aria-label="Page 1">1</a></li>
<li><a class="page" href="?page=2" aria-label="Page 2">2</a></li>
<li><a class="page" href="?page=3" aria-label="Page 3" aria-current="page">3</a></li>
<li><a class="page" href="?page=4" aria-label="Page 4">4</a></li>
<li><a class="page" href="?page=5" aria-label="Page 5">5</a></li>
</ul>
</nav>
JavaScript
On a real site you don't need any JavaScript: every link loads its own page, and the server (or your framework) addsaria-current="page" to the link for the page being shown.
The demo has no other pages to load, so the script stops the navigation with preventDefault() and moves aria-current to the clicked link. Leave this script out when your links point to real pages.
// Demo only: in a real site each link loads its page, and the server marks
// the current one with aria-current="page". Here we move the marker on click.
const pages = document.querySelectorAll('.pagination .page');
pages.forEach((page) => {
page.addEventListener('click', (event) => {
event.preventDefault();
pages.forEach((p) => p.removeAttribute('aria-current'));
page.setAttribute('aria-current', 'page');
});
});
CSS
First the list. We remove the bullets and default spacing, then center the page numbers in a row with flexbox and a 10px gap.
.pages {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
list-style: none;
margin: 0;
padding: 0;
}
Each link is a 30px circle (100% border radius) with the number centered by flexbox. The text is white and bold, the underline is removed, and the 0.5s transition makes the hover and active changes smooth.
.page {
width: 30px;
height: 30px;
border-radius: 100%;
display: flex;
justify-content: center;
align-items: center;
transition: 0.5s;
color: #fff;
font-weight: 500;
text-decoration: none;
}
On hover the page grows by 20%. Keyboard users get a visible dark outline with :focus-visible, which appears when tabbing but not on mouse clicks.
.page:hover {
transform: scale(1.2);
transition: 0.2s;
}
.page:focus-visible {
outline: 2px solid #333;
outline-offset: 3px;
}
Each page gets its own color, from yellow to brown. The selectors count the list items, because every link is the only child of its <li>.
li:nth-child(1) .page {
background-color: rgb(255, 225, 0);
}
li:nth-child(2) .page {
background-color: rgb(255, 200, 0);
}
li:nth-child(3) .page {
background-color: rgb(255, 157, 0);
}
li:nth-child(4) .page {
background-color: rgb(255, 123, 0);
}
li:nth-child(5) .page {
background-color: rgb(211, 106, 0);
}
The current page is styled through aria-current="page": 50% larger, red, and with the default cursor so it doesn't look like something to click. The .pages prefix makes this rule more specific than the color rules, so no !important is needed.
.pages .page[aria-current="page"] {
transform: scale(1.5);
background-color: rgb(245, 57, 0);
cursor: default;
}
Finally, visitors who ask their system for reduced motion get no scaling on hover and no transitions. The current page stays larger, because that size shows where we are.
@media (prefers-reduced-motion: reduce) {
.page,
.page:hover {
transition: none;
}
.page:hover:not([aria-current="page"]) {
transform: none;
}
}
Using it with more pages
For long lists, show the first page, the pages around the current one and the last page, with an ellipsis in between. Our pagination with ellipsis shows that layout. Add Previous and Next links at the ends if visitors move one page at a time, and leave them out (or disable them) on the first and last page. The five colors are set per position, so for more pages add moreli:nth-child() rules or switch to a single color. White text on the lightest yellow has low contrast, so for small text consider a darker number color on the first pages.Whole code
<nav class="pagination" aria-label="Pagination">
<ul class="pages">
<li><a class="page" href="?page=1" aria-label="Page 1">1</a></li>
<li><a class="page" href="?page=2" aria-label="Page 2">2</a></li>
<li><a class="page" href="?page=3" aria-label="Page 3" aria-current="page">3</a></li>
<li><a class="page" href="?page=4" aria-label="Page 4">4</a></li>
<li><a class="page" href="?page=5" aria-label="Page 5">5</a></li>
</ul>
</nav>
<script>
// Demo only: in a real site each link loads its page, and the server marks
// the current one with aria-current="page". Here we move the marker on click.
const pages = document.querySelectorAll('.pagination .page');
pages.forEach((page) => {
page.addEventListener('click', (event) => {
event.preventDefault();
pages.forEach((p) => p.removeAttribute('aria-current'));
page.setAttribute('aria-current', 'page');
});
});
</script>
<style>
.pages {
display: flex;
justify-content: center;
align-items: center;
gap: 10px;
list-style: none;
margin: 0;
padding: 0;
}
.page {
width: 30px;
height: 30px;
border-radius: 100%;
display: flex;
justify-content: center;
align-items: center;
transition: 0.5s;
color: #fff;
font-weight: 500;
text-decoration: none;
}
.page:hover {
transform: scale(1.2);
transition: 0.2s;
}
.page:focus-visible {
outline: 2px solid #333;
outline-offset: 3px;
}
li:nth-child(1) .page {
background-color: rgb(255, 225, 0);
}
li:nth-child(2) .page {
background-color: rgb(255, 200, 0);
}
li:nth-child(3) .page {
background-color: rgb(255, 157, 0);
}
li:nth-child(4) .page {
background-color: rgb(255, 123, 0);
}
li:nth-child(5) .page {
background-color: rgb(211, 106, 0);
}
.pages .page[aria-current="page"] {
transform: scale(1.5);
background-color: rgb(245, 57, 0);
cursor: default;
}
@media (prefers-reduced-motion: reduce) {
.page,
.page:hover {
transition: none;
}
.page:hover:not([aria-current="page"]) {
transform: none;
}
}
</style>
Thank you for reading this article.
Comments