<div id="pagination"></div>
$border-color: #3D315B;
$background-color: #266DD3;
$list-background-color: #444B6E;
$active-list-background-color: #9AB87A;
$border-radius: 5px;

* {
  font-family: 'Poppins';
}
body {
  background-color: $background-color;
  margin: 0;
}
a:hover {
  cursor: pointer;
}
#pagination {
  display: flex;
  height: 100vh;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  li {
    color: #fff;
    display: flex;
    a {
      background-color: #444B6E;
      padding: 5px 10px;
      border: 2px solid $border-color;
      border-right: 0;
    }
  }
  // li.previous,
  // li.next {
  //   animation: fadein .5s;
  // }
  li.active a {
    background-color: $active-list-background-color;
  }
  li:first-child a {
    border-radius: $border-radius 0 0 $border-radius;
  }
  li:last-child a {
    border-radius: 0 $border-radius $border-radius 0;
    border-right: 2px solid $border-color;
  }
}

// When I feel motivated to get this working
// @keyframes fadein {
//   from {opacity: 0;}
//   to {opacity: 1;}
// }
View Compiled
let pages = 25;

document.getElementById('pagination').innerHTML = createPagination(pages, 12);

function createPagination(pages, page) {
  let str = '<ul>';
  let active;
  let pageCutLow = page - 1;
  let pageCutHigh = page + 1;
  // Show the Previous button only if you are on a page other than the first
  if (page > 1) {
    str += '<li class="page-item previous no"><a onclick="createPagination(pages, '+(page-1)+')">Previous</a></li>';
  }
  // Show all the pagination elements if there are less than 6 pages total
  if (pages < 6) {
    for (let p = 1; p <= pages; p++) {
      active = page == p ? "active" : "no";
      str += '<li class="'+active+'"><a onclick="createPagination(pages, '+p+')">'+ p +'</a></li>';
    }
  }
  // Use "..." to collapse pages outside of a certain range
  else {
    // Show the very first page followed by a "..." at the beginning of the
    // pagination section (after the Previous button)
    if (page > 2) {
      str += '<li class="no page-item"><a onclick="createPagination(pages, 1)">1</a></li>';
      if (page > 3) {
          str += '<li class="out-of-range"><a onclick="createPagination(pages,'+(page-2)+')">...</a></li>';
      }
    }
    // Determine how many pages to show after the current page index
    if (page === 1) {
      pageCutHigh += 2;
    } else if (page === 2) {
      pageCutHigh += 1;
    }
    // Determine how many pages to show before the current page index
    if (page === pages) {
      pageCutLow -= 2;
    } else if (page === pages-1) {
      pageCutLow -= 1;
    }
    // Output the indexes for pages that fall inside the range of pageCutLow
    // and pageCutHigh
    for (let p = pageCutLow; p <= pageCutHigh; p++) {
      if (p === 0) {
        p += 1;
      }
      if (p > pages) {
        continue
      }
      active = page == p ? "active" : "no";
      str += '<li class="page-item '+active+'"><a onclick="createPagination(pages, '+p+')">'+ p +'</a></li>';
    }
    // Show the very last page preceded by a "..." at the end of the pagination
    // section (before the Next button)
    if (page < pages-1) {
      if (page < pages-2) {
        str += '<li class="out-of-range"><a onclick="createPagination(pages,'+(page+2)+')">...</a></li>';
      }
      str += '<li class="page-item no"><a onclick="createPagination(pages, pages)">'+pages+'</a></li>';
    }
  }
  // Show the Next button only if you are on a page other than the last
  if (page < pages) {
    str += '<li class="page-item next no"><a onclick="createPagination(pages, '+(page+1)+')">Next</a></li>';
  }
  str += '</ul>';
  // Return the pagination string to be outputted in the pug templates
  document.getElementById('pagination').innerHTML = str;
  return str;
}

External CSS

  1. https://fonts.googleapis.com/css?family=Poppins

External JavaScript

This Pen doesn't use any external JavaScript resources.