TExcelTAD
Home › CSS › CSS Pagination

CSS Pagination

When a site has many results, articles or products, it splits them across pages and shows pagination: a row of links like « 1 2 3 4 ». It's a small component, but a great exercise in combining lists, flexbox, links and state styles.

The markup

Pagination is navigation, so use a <nav> with an accessible label and a list of links. Mark the current page with aria-current="page" — and you can style that attribute directly.

html
<nav aria-label="Pages">
  <ul class="pagination">
    <li><a href="#">«</a></li>
    <li><a href="#">1</a></li>
    <li><a href="#" aria-current="page">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">»</a></li>
  </ul>
</nav>

Basic pagination

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  .pagination { list-style: none; padding: 0; margin: 0; display: flex; gap: 4px; }
  .pagination a {
    display: block;
    min-width: 40px;
    padding: 8px 12px;
    text-align: center;
    text-decoration: none;
    color: #333;
    border-radius: 6px;
    transition: background .2s;
  }
  .pagination a:hover { background: #e0e0e0; }
  .pagination a[aria-current="page"] { background: #1e88e5; color: white; }
</style>
</head>
<body>
  <nav aria-label="Pages">
    <ul class="pagination">
      <li><a href="#">«</a></li>
      <li><a href="#">1</a></li>
      <li><a href="#" aria-current="page">2</a></li>
      <li><a href="#">3</a></li>
      <li><a href="#">4</a></li>
      <li><a href="#">5</a></li>
      <li><a href="#">»</a></li>
    </ul>
  </nav>
</body>
</html>

Tip: Styling [aria-current="page"] instead of an .active class keeps the visual state and the accessibility state in sync — you can't forget one.

Bordered and joined

Joined pagination draws one continuous strip: each link has a border, neighbours overlap by one pixel, and only the outer corners are rounded.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  .pg { list-style: none; padding: 0; display: flex; }
  .pg a { display: block; padding: 8px 14px; border: 1px solid #c5cae9; margin-left: -1px; color: #3949ab; text-decoration: none; }
  .pg li:first-child a { border-radius: 8px 0 0 8px; margin-left: 0; }
  .pg li:last-child a { border-radius: 0 8px 8px 0; }
  .pg a:hover:not([aria-current]) { background: #e8eaf6; }
  .pg a[aria-current] { background: #3949ab; border-color: #3949ab; color: white; position: relative; }
  .pg .gap { padding: 8px 10px; border: 1px solid #c5cae9; margin-left: -1px; color: #999; }
  .lg a { padding: 12px 20px; font-size: 20px; }
</style>
</head>
<body>
  <ul class="pg">
    <li><a href="#">Prev</a></li><li><a href="#">1</a></li><li><span class="gap">…</span></li>
    <li><a href="#">7</a></li><li><a href="#" aria-current="page">8</a></li><li><a href="#">9</a></li>
    <li><span class="gap">…</span></li><li><a href="#">20</a></li><li><a href="#">Next</a></li>
  </ul>
  <ul class="pg lg"><li><a href="#">1</a></li><li><a href="#" aria-current="page">2</a></li><li><a href="#">3</a></li></ul>
</body>
</html>

Disabled and round styles

On the first page, "Previous" makes no sense. Show it greyed out and unclickable with pointer-events: none. Circular page numbers are another popular look.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 20px; }
  .dots { list-style: none; padding: 0; display: flex; gap: 8px; align-items: center; }
  .dots a { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; background: #f1f8e9; color: #33691e; text-decoration: none; font-weight: 600; }
  .dots a:hover { background: #c5e1a5; }
  .dots a[aria-current] { background: #558b2f; color: white; transform: scale(1.15); }
  .dots a[aria-disabled="true"] { opacity: .35; pointer-events: none; }
</style>
</head>
<body>
  <ul class="dots">
    <li><a href="#" aria-disabled="true">‹</a></li>
    <li><a href="#" aria-current="page">1</a></li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">›</a></li>
  </ul>
</body>
</html>

Centring and responsiveness

Centre the bar with justify-content: center. On narrow screens, allow wrapping with flex-wrap: wrap, or hide the middle numbers and keep only Prev/Next.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .pg { list-style: none; padding: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
  .pg a { display: block; padding: 8px 12px; border-radius: 6px; background: #fff3e0; color: #e65100; text-decoration: none; }
  .pg a[aria-current] { background: #e65100; color: white; }
  @media (max-width: 420px) {
    .pg .num:not([aria-current]) { display: none; }
  }
</style>
</head>
<body>
  <ul class="pg">
    <li><a href="#">← Prev</a></li>
    <li><a class="num" href="#">1</a></li><li><a class="num" href="#">2</a></li>
    <li><a class="num" href="#" aria-current="page">3</a></li><li><a class="num" href="#">4</a></li>
    <li><a class="num" href="#">5</a></li>
    <li><a href="#">Next →</a></li>
  </ul>
  <p style="text-align:center">Drag the playground narrower than 420px.</p>
</body>
</html>

Summary

Need CSS
Horizontal row display: flex on the list, list-style: none
Clickable area display: block + padding on the links
Current page [aria-current="page"] selector
Disabled link opacity + pointer-events: none
Centred justify-content: center

Test yourself

What attribute best marks the current page link for both styling and screen readers?

  1. class="active"
  2. aria-current="page"
  3. data-page="on"
  4. selected

How do you stop a disabled pagination link from being clicked using CSS?

  1. cursor: none
  2. pointer-events: none
  3. display: inline
  4. z-index: -1

Why give pagination links display: block and padding?

  1. To make the clickable area larger
  2. To remove underlines
  3. To change the font
  4. To centre the list