1. CSS paging example
Simple paging
If your website has many pages, you need to use paging to navigate each page.
The following example demonstrates how to use HTML and CSS to create paging:
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; } </style> </head> <body> <h2>简单的分页</h2> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
If you click on the current page, you can use .active to set the current page style, and you can use the :hover selector when hovering the mouse. To modify the style
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; } ul.pagination li a.active { background-color: #4CAF50; color: white; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>点击及鼠标悬停分页样式</h2> <p>移动鼠标的分页的数字上。</p> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Rounded corner style
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; border-radius: 5px; } ul.pagination li a.active { background-color: #4CAF50; color: white; border-radius: 5px; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>圆角样式</h2> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Mouseover transition effect
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; } ul.pagination li a.active { background-color: #4CAF50; color: white; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>鼠标悬停过渡效果</h2> <p>鼠标移动到分页码上。</p> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Bordered paging
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; } ul.pagination li a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>带边框分页</h2> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Circle Corner border
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; } .pagination li:first-child a { border-top-left-radius: 5px; border-bottom-left-radius: 5px; } .pagination li:last-child a { border-top-right-radius: 5px; border-bottom-right-radius: 5px; } ul.pagination li a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>圆角边框</h2> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Page interval
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; margin: 0 4px; } ul.pagination li a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>分页间隔</h2> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Page font Size
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; font-size: 22px; } ul.pagination li a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <h2>分页字体大小</h2> <p>我们可以使用 font-size 属性来设置分页的字体大小:</p> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </body> </html>
Centered pagination
If you want the pagination to be centered, you can place it on the container element (such as <div> ;) Add text-align:center style:
<!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; } ul.pagination li a.active { background-color: #4CAF50; color: white; border: 1px solid #4CAF50; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} div.center {text-align: center;} </style> </head> <body> <h2>分页居中</h2> <div class="center"> <ul class="pagination"> <li><a href="#">«</a></li> <li><a href="#">1</a></li> <li><a class="active" href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li><a href="#">6</a></li> <li><a href="#">7</a></li> <li><a href="#">»</a></li> </ul> </div> </body> </html> 上一页,下一页按钮: <!DOCTYPE html> <html> <head> <style> ul.pagination { display: inline-block; padding: 0; margin: 0; } ul.pagination li {display: inline;} ul.pagination li a { color: black; float: left; padding: 8px 16px; text-decoration: none; transition: background-color .3s; border: 1px solid #ddd; font-size: 18px; } ul.pagination li a.active { background-color: #eee; color: black; border: 1px solid #ddd; } ul.pagination li a:hover:not(.active) {background-color: #ddd;} </style> </head> <body> <p>上一页,下一页按钮:</p> <ul class="pagination"> <li><a href="#">❮</a></li> <li><a href="#">❯</a></li> </ul> <p>分页导航:</p> <ul class="pagination"> <li><a href="#" class="active">Home</a></li> <li><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> </ul> </body> </html>
Breadcrumbs
<!DOCTYPE html> <html> <head> <style> ul.breadcrumb { padding: 8px 16px; list-style: none; background-color: #eee; } ul.breadcrumb li {display: inline;} ul.breadcrumb li+li:before { padding: 8px; color: black; content: "/rrreeea0"; } ul.breadcrumb li a {color: green;} </style> </head> <body> <h2>面包屑导航</h2> <ul class="breadcrumb"> <li><a href="#">首页 </a></li> <li><a href="#">前端 </a></li> <li><a href="#">HTML 教程 </a></li> <li>HTML 段落</li> </ul> </body> </html>