Home > Article > Web Front-end > How to align pagination in Bootstrap 4?
Bootstrap 4 is a popular front-end programming framework for creating responsive, mobile-first websites. It provides various CSS and JavaScript elements such as navigation bars, forms, buttons, modals, etc. that may be used to quickly build a website with a modern and beautiful look.
Pagination alignment in Bootstrap 4 refers to the way the pagination components of a web page are positioned. Pagination is usually centered, however. The justify-content-* classes allow left or right alignment.
There are many possible ways to align pagination in Bootstrap 4 -
Use .justify-content-* classes
Use text-* classes
Now let us understand each method in detail with examples.
The first way to align pagination in Bootstrap 4 is "using the .justify-content-* classes".
Here we will implement this method with a step-by-step example -
Step 1 - Make sure that the Bootstrap 4 JavaScript and CSS files are included in the head of the HTML document.
Step 2 - To build the pagination component, create the ff6d136ddc5fdfeffaf53ff6ee95f185 element using the pagination class. For each page, add an 3499910bf9dac5ae3c52d5ede7383485 element with a page-like link and an 25edfb22a4f469ecb59f1190150159c6 element with a page-like item inside the ff6d136ddc5fdfeffaf53ff6ee95f185 element.
Step 3 - You must add one of the following classes to the ff6d136ddc5fdfeffaf53ff6ee95f185 element to align the pagination component left, center, or right -
The paging component uses the .justify-content-start style to align left.
The pagination component is centered when using the justify-content-center style.
align-pagination-end: Move the pagination element to the right
Step 4 - This is an example of what the HTML code will look like when pagination is left aligned -
<ul class="pagination justify-content-start"> <li class="page-item"> <a class="page-link" href="#">Previous</a> </li> <li class="page-item"> <a class="page-link" href="#">1</a> </li> <li class="page-item"> <a class="page-link" href="#">2</a> </li> <li class="page-item"> <a class="page-link" href="#">Next</a> </li> </ul>
Step 5 - After adding the required classes, the pagination component should align as you expect.
Step 6 - The final code looks like this -
<!DOCTYPE html> <html> <head> <link rel= "stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="row justify-content-center"> <nav aria-label="Page navigation example"> <ul class="pagination justify-content-center"> <li class="page-item"> <a class="page-link" href="#" aria-label="Previous"> <span aria-hidden="true">«</span> <span class="sr-only">Previous</span> </a> </li> <li class="page-item"><a class="page-link" href="#">1</a></li> <li class="page-item"><a class="page-link" href="#">2</a></li> <li class="page-item"><a class="page-link" href="#">3</a></li> <li class="page-item"> <a class="page-link" href="#" aria-label="Next"> <span aria-hidden="true">»</span> <span class="sr-only">Next</span> </a> </li> </ul> </nav> </div> </div> </body> </html>
To align the pagination components to the left, center, or right respectively, use . Text Left, Text Center and. Text right class.
We will now look at a step-by-step example of implementing this method -
Step 1 - Create a container div element and assign it the class justify-content-center. As a result, the pagination will be centered within the container.
<div class="justify-content-center">
Step 2 - Create an ff6d136ddc5fdfeffaf53ff6ee95f185 element within the container div and give it class pagination and align-pagination-links-to-desired-side (e.g. text-center, text- left, textright) )
<ul class="pagination text-center">
Step 3 - Create 25edfb22a4f469ecb59f1190150159c6 elements for each page number or previous and next buttons within a ff6d136ddc5fdfeffaf53ff6ee95f185 element. Specify the "page-item" class for each li element.
<li class="page-item">
Step 4 - Create an 3499910bf9dac5ae3c52d5ede7383485 element for the button or page number within each 25edfb22a4f469ecb59f1190150159c6 element. Assign the class "page-link" to the 3499910bf9dac5ae3c52d5ede7383485 element.
<a class="page-link" href="#">1</a>
Step 5 - The final code looks like this -
<div class="justify-content-center"> <ul class="pagination text-center"> <li class="page-item"> Previous <li class="page-item"> <a class="page-link" href="#">1</a> <li class="page-item"> 2 <li class="page-item"> 3 <li class="page-item"> Next
Using the built-in classes provided by the framework will make page alignment in Bootstrap 4 easy. By using the above step-by-step process, you can make a fully functional and visually page-centric pagination component. Include the Bootstrap CSS and JavaScript files in the project, create a nav element with the aria-label attribute, and provide an unordered list element containing pagination links. The textcenter class will do this. This strategy makes it easy to change the color and direction of pagination without having to create a lot of unique CSS.
The above is the detailed content of How to align pagination in Bootstrap 4?. For more information, please follow other related articles on the PHP Chinese website!