Hey friends, today in this blog you’ll learn how to create a Fully Functional Pagination UI Design using HTML CSS & JavaScript. In the earlier blog, I’ve also shared many pagination designs but these paginations are not functional and these were created only for design purposes. Now I’m going to create a fully functional and easy to navigate pagination in JavaScript.
You may know, Pagination is a method or process of dividing the web pages, or a section of content into discrete/many pages. Pagination plays an important role in the SEO (Search Engine Optimization) of your website and it is also important and most used in web design.
In this design [Pagination UI Design], there is pagination with the previous and next buttons and some numbers. I already told you, this is fully functional pagination so when you click on the next or previous button, the number of the pagination also changed accordingly. There is a total of 20 pages or numbers but you can easily add more according to your web pages. If you want to see this pagination and how it is created then you can watch a full video tutorial on this program [Pagination UI Design].
Video Tutorial of Pagination UI Design
In the video, you’ve seen the fully functional pagination and how it is created. In this tutorial, I’ve only shown the pagination but I’ll make another tutorial soon, where I’ll add this pagination with web pages. If you’re a beginner then you may have difficulty understanding the JavaScript codes of this program but I did my best to explain each JavaScript line with comments and don’t worry you’ll easily understand the code once you downloaded the source files of this program.
If you like this pagination design and want to get source codes of this then you can easily copy the codes from the given boxes or you can also download the source code files from the download button. If you have knowledge of backend languages like PHP, then you can easily integrate this pagination into your website.
To create this program [Pagination UI Design]. First, you need to create three files, HTML File, CSS File, and JavaScript File. After creating these files just paste the following codes into your files. You can also download the source code files of the pagination design from the given download button.
First, create an HTML file with the name of index.html and paste the given codes in your HTML file. Remember, you’ve to create a file with .html extension.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<!-- Created By CodingNepal - www.codingnepalweb.com -->
<ul><!--pages or li are comes from javascript --></ul>
</div>
<scriptsrc="script.js"></script>
</body>
</html>
<!DOCTYPE html>
<!-- Created By CodingNepal - www.codingnepalweb.com -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<<title>Pagination in JavaScript | CodingNepal</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
</head>
<body>
<div class="pagination">
<ul> <!--pages or li are comes from javascript --> </ul>
</div>
<script src="script.js"></script>
</body>
</html>
<!DOCTYPE html>
<!-- Created By CodingNepal - www.codingnepalweb.com -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<<title>Pagination in JavaScript | CodingNepal</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
</head>
<body>
<div class="pagination">
<ul> <!--pages or li are comes from javascript --> </ul>
</div>
<script src="script.js"></script>
</body>
</html>
Second, create a CSS file with the name of style.css and paste the given codes in your CSS file. Remember, you’ve to create a file with .css extension.
Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// selecting required element
const element = document.querySelector(".pagination ul");
let totalPages = 20;
let page = 10;
//calling function with passing parameters and adding inside element which is ul tag
element.innerHTML = liTag; //add li tag inside ul tag
return liTag; //reurn the li tag
}
// selecting required element
const element = document.querySelector(".pagination ul");
let totalPages = 20;
let page = 10;
//calling function with passing parameters and adding inside element which is ul tag
element.innerHTML = createPagination(totalPages, page);
function createPagination(totalPages, page){
let liTag = '';
let active;
let beforePage = page - 1;
let afterPage = page + 1;
if(page > 1){ //show the next button if the page value is greater than 1
liTag += `<li class="btn prev" onclick="createPagination(totalPages, ${page - 1})"><span><i class="fas fa-angle-left"></i> Prev</span></li>`;
}
if(page > 2){ //if page value is less than 2 then add 1 after the previous button
liTag += `<li class="first numb" onclick="createPagination(totalPages, 1)"><span>1</span></li>`;
if(page > 3){ //if page value is greater than 3 then add this (...) after the first li or page
liTag += `<li class="dots"><span>...</span></li>`;
}
}
// how many pages or li show before the current li
if (page == totalPages) {
beforePage = beforePage - 2;
} else if (page == totalPages - 1) {
beforePage = beforePage - 1;
}
// how many pages or li show after the current li
if (page == 1) {
afterPage = afterPage + 2;
} else if (page == 2) {
afterPage = afterPage + 1;
}
for (var plength = beforePage; plength <= afterPage; plength++) {
if (plength > totalPages) { //if plength is greater than totalPage length then continue
continue;
}
if (plength == 0) { //if plength is 0 than add +1 in plength value
plength = plength + 1;
}
if(page == plength){ //if page is equal to plength than assign active string in the active variable
active = "active";
}else{ //else leave empty to the active variable
active = "";
}
liTag += `<li class="numb ${active}" onclick="createPagination(totalPages, ${plength})"><span>${plength}</span></li>`;
}
if(page < totalPages - 1){ //if page value is less than totalPage value by -1 then show the last li or page
if(page < totalPages - 2){ //if page value is less than totalPage value by -2 then add this (...) before the last li or page
liTag += `<li class="dots"><span>...</span></li>`;
}
liTag += `<li class="last numb" onclick="createPagination(totalPages, ${totalPages})"><span>${totalPages}</span></li>`;
}
if (page < totalPages) { //show the next button if the page value is less than totalPage(20)
liTag += `<li class="btn next" onclick="createPagination(totalPages, ${page + 1})"><span>Next <i class="fas fa-angle-right"></i></span></li>`;
}
element.innerHTML = liTag; //add li tag inside ul tag
return liTag; //reurn the li tag
}
// selecting required element
const element = document.querySelector(".pagination ul");
let totalPages = 20;
let page = 10;
//calling function with passing parameters and adding inside element which is ul tag
element.innerHTML = createPagination(totalPages, page);
function createPagination(totalPages, page){
let liTag = '';
let active;
let beforePage = page - 1;
let afterPage = page + 1;
if(page > 1){ //show the next button if the page value is greater than 1
liTag += `<li class="btn prev" onclick="createPagination(totalPages, ${page - 1})"><span><i class="fas fa-angle-left"></i> Prev</span></li>`;
}
if(page > 2){ //if page value is less than 2 then add 1 after the previous button
liTag += `<li class="first numb" onclick="createPagination(totalPages, 1)"><span>1</span></li>`;
if(page > 3){ //if page value is greater than 3 then add this (...) after the first li or page
liTag += `<li class="dots"><span>...</span></li>`;
}
}
// how many pages or li show before the current li
if (page == totalPages) {
beforePage = beforePage - 2;
} else if (page == totalPages - 1) {
beforePage = beforePage - 1;
}
// how many pages or li show after the current li
if (page == 1) {
afterPage = afterPage + 2;
} else if (page == 2) {
afterPage = afterPage + 1;
}
for (var plength = beforePage; plength <= afterPage; plength++) {
if (plength > totalPages) { //if plength is greater than totalPage length then continue
continue;
}
if (plength == 0) { //if plength is 0 than add +1 in plength value
plength = plength + 1;
}
if(page == plength){ //if page is equal to plength than assign active string in the active variable
active = "active";
}else{ //else leave empty to the active variable
active = "";
}
liTag += `<li class="numb ${active}" onclick="createPagination(totalPages, ${plength})"><span>${plength}</span></li>`;
}
if(page < totalPages - 1){ //if page value is less than totalPage value by -1 then show the last li or page
if(page < totalPages - 2){ //if page value is less than totalPage value by -2 then add this (...) before the last li or page
liTag += `<li class="dots"><span>...</span></li>`;
}
liTag += `<li class="last numb" onclick="createPagination(totalPages, ${totalPages})"><span>${totalPages}</span></li>`;
}
if (page < totalPages) { //show the next button if the page value is less than totalPage(20)
liTag += `<li class="btn next" onclick="createPagination(totalPages, ${page + 1})"><span>Next <i class="fas fa-angle-right"></i></span></li>`;
}
element.innerHTML = liTag; //add li tag inside ul tag
return liTag; //reurn the li tag
}
That’s all, now you’ve successfully created a Pagination UI Design using HTML CSS & JavaScript. If your code doesn’t work or you’ve faced any error/problem then please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.
can you provide necessary data for link other pages to pagination? it is not me my other friend waiting for this. i also search on you tub and your website but i did not find any thing related this….Pls provide us necessary data pls
bro make codes.as you have mention on your blog.i need this soon make a video fast
I have used Enlighter plugin.
can you provide necessary data for link other pages to pagination? it is not me my other friend waiting for this. i also search on you tub and your website but i did not find any thing related this….Pls provide us necessary data pls
bro how to create comment box like here.
bro how can i add page link.when we click page2,3,4,5 ???????
For that, you’ve to use the anchor tag inside li and do add some JavaScript codes
and please reply in my email because no notification is coming from your website….Love you bro Hope you reply soon…Email-ayans3409@gmail.com
Can you provide the javascript code