CSS3 分页样式实例

学习如何使用 CSS 创建响应式分页。


简单的分页

如果网站上有很多页面,那么您可能希望在每张页面上添加某种分页功能:

实例
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>简单分页</h1>
  18. <div class="pagination">
  19. <a href="#">&laquo;</a>
  20. <a href="#">1</a>
  21. <a href="#">2</a>
  22. <a href="#">3</a>
  23. <a href="#">4</a>
  24. <a href="#">5</a>
  25. <a href="#">6</a>
  26. <a href="#">&raquo;</a>
  27. </div>
  28. </body>
  29. </html>

活动的可悬停分页

.active 类突出显示当前页面,并在鼠标移到它们上方时使用 :hover 选择器更改每个页面链接的颜色:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. }
  14. .pagination a.active {
  15. background-color: #4CAF50;
  16. color: white;
  17. }
  18. .pagination a:hover:not(.active) {background-color: #ddd;}
  19. </style>
  20. </head>
  21. <body>
  22. <h1>活动的可悬停分页</h1>
  23. <p>请把鼠标移动到数字上:</p>
  24. <div class="pagination">
  25. <a href="#">&laquo;</a>
  26. <a href="#">1</a>
  27. <a class="active" href="#">2</a>
  28. <a href="#">3</a>
  29. <a href="#">4</a>
  30. <a href="#">5</a>
  31. <a href="#">6</a>
  32. <a href="#">&raquo;</a>
  33. </div>
  34. </body>
  35. </html>

圆角的活动可悬停分页

如果您需要圆角的 "active" 和 "hover" 按钮,请添加 border-radius 属性:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. }
  14. .pagination a.active {
  15. background-color: #4CAF50;
  16. color: white;
  17. border-radius: 5px;
  18. }
  19. .pagination a:hover:not(.active) {
  20. background-color: #ddd;
  21. border-radius: 5px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <h1>活动的可悬停的圆角分页</h1>
  27. <div class="pagination">
  28. <a href="#">&laquo;</a>
  29. <a href="#">1</a>
  30. <a href="#" class="active">2</a>
  31. <a href="#">3</a>
  32. <a href="#">4</a>
  33. <a href="#">5</a>
  34. <a href="#">6</a>
  35. <a href="#">&raquo;</a>
  36. </div>
  37. </body>
  38. </html>

可悬停的过渡效果

请将 transition 属性添加到页面链接,创建鼠标悬停时的过渡效果:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. transition: background-color .3s;
  14. }
  15. .pagination a.active {
  16. background-color: #4CAF50;
  17. color: white;
  18. }
  19. .pagination a:hover:not(.active) {background-color: #ddd;}
  20. </style>
  21. </head>
  22. <body>
  23. <h1>悬停时的过渡效果</h1>
  24. <p>请把鼠标移动到数字上。</p>
  25. <div class="pagination">
  26. <a href="#">&laquo;</a>
  27. <a href="#">1</a>
  28. <a href="#" class="active">2</a>
  29. <a href="#">3</a>
  30. <a href="#">4</a>
  31. <a href="#">5</a>
  32. <a href="#">6</a>
  33. <a href="#">&raquo;</a>
  34. </div>
  35. </body>
  36. </html>

带边框的分页

请使用 border 属性为分页添加边框:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. transition: background-color .3s;
  14. border: 1px solid #ddd;
  15. }
  16. .pagination a.active {
  17. background-color: #4CAF50;
  18. color: white;
  19. border: 1px solid #4CAF50;
  20. }
  21. .pagination a:hover:not(.active) {background-color: #ddd;}
  22. </style>
  23. </head>
  24. <body>
  25. <h1>带边框的分页</h1>
  26. <div class="pagination">
  27. <a href="#">&laquo;</a>
  28. <a href="#">1</a>
  29. <a href="#" class="active">2</a>
  30. <a href="#">3</a>
  31. <a href="#">4</a>
  32. <a href="#">5</a>
  33. <a href="#">6</a>
  34. <a href="#">&raquo;</a>
  35. </div>
  36. </body>
  37. </html>

圆角边框

提示:在分页的第一个和最后一个链接中添加圆角边框:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. border: 1px solid #ddd;
  14. }
  15. .pagination a.active {
  16. background-color: #4CAF50;
  17. color: white;
  18. border: 1px solid #4CAF50;
  19. }
  20. .pagination a:hover:not(.active) {background-color: #ddd;}
  21. .pagination a:first-child {
  22. border-top-left-radius: 5px;
  23. border-bottom-left-radius: 5px;
  24. }
  25. .pagination a:last-child {
  26. border-top-right-radius: 5px;
  27. border-bottom-right-radius: 5px;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <h1>带圆角边框的分页</h1>
  33. <div class="pagination">
  34. <a href="#">&laquo;</a>
  35. <a href="#">1</a>
  36. <a class="active" href="#">2</a>
  37. <a href="#">3</a>
  38. <a href="#">4</a>
  39. <a href="#">5</a>
  40. <a href="#">6</a>
  41. <a href="#">&raquo;</a>
  42. </div>
  43. </body>
  44. </html>

链接之间的空间

提示:如果不想组合页面链接,请添加 margin 属性:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. transition: background-color .3s;
  14. border: 1px solid #ddd;
  15. margin: 0 4px;
  16. }
  17. .pagination a.active {
  18. background-color: #4CAF50;
  19. color: white;
  20. border: 1px solid #4CAF50;
  21. }
  22. .pagination a:hover:not(.active) {background-color: #ddd;}
  23. </style>
  24. </head>
  25. <body>
  26. <h1>带外边距的分页:</h1>
  27. <div class="pagination">
  28. <a href="#">&laquo;</a>
  29. <a href="#">1</a>
  30. <a href="#" class="active">2</a>
  31. <a href="#">3</a>
  32. <a href="#">4</a>
  33. <a href="#">5</a>
  34. <a href="#">6</a>
  35. <a href="#">&raquo;</a>
  36. </div>
  37. </body>
  38. </html>

分页尺寸

请使用 font-size 属性更改分页的大小:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .pagination {
  6. display: inline-block;
  7. }
  8. .pagination a {
  9. color: black;
  10. float: left;
  11. padding: 8px 16px;
  12. text-decoration: none;
  13. transition: background-color .3s;
  14. border: 1px solid #ddd;
  15. font-size: 22px;
  16. }
  17. .pagination a.active {
  18. background-color: #4CAF50;
  19. color: white;
  20. border: 1px solid #4CAF50;
  21. }
  22. .pagination a:hover:not(.active) {background-color: #ddd;}
  23. </style>
  24. </head>
  25. <body>
  26. <h1>分页尺寸</h1>
  27. <p>更改 font-size 属性以使分页变小或变大。</p>
  28. <div class="pagination">
  29. <a href="#">&laquo;</a>
  30. <a href="#">1</a>
  31. <a href="#" class="active">2</a>
  32. <a href="#">3</a>
  33. <a href="#">4</a>
  34. <a href="#">5</a>
  35. <a href="#">6</a>
  36. <a href="#">&raquo;</a>
  37. </div>
  38. </body>
  39. </html>

居中的分页

如需居中分页,请使用设置了 text-align:center 的容器元素(如 <div>)包围它:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .center {
  6. text-align: center;
  7. }
  8. .pagination {
  9. display: inline-block;
  10. }
  11. .pagination a {
  12. color: black;
  13. float: left;
  14. padding: 8px 16px;
  15. text-decoration: none;
  16. transition: background-color .3s;
  17. border: 1px solid #ddd;
  18. margin: 0 4px;
  19. }
  20. .pagination a.active {
  21. background-color: #4CAF50;
  22. color: white;
  23. border: 1px solid #4CAF50;
  24. }
  25. .pagination a:hover:not(.active) {background-color: #ddd;}
  26. </style>
  27. </head>
  28. <body>
  29. <h1>居中分页</h1>
  30. <div class="center">
  31. <div class="pagination">
  32. <a href="#">&laquo;</a>
  33. <a href="#">1</a>
  34. <a href="#" class="active">2</a>
  35. <a href="#">3</a>
  36. <a href="#">4</a>
  37. <a href="#">5</a>
  38. <a href="#">6</a>
  39. <a href="#">&raquo;</a>
  40. </div>
  41. </div>
  42. </body>
  43. </html>

面包屑

分页的另一种形式是所谓的“面包屑”(breadcrumbs):

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. ul.breadcrumb {
  6. padding: 8px 16px;
  7. list-style: none;
  8. background-color: #eee;
  9. }
  10. ul.breadcrumb li {display: inline;}
  11. ul.breadcrumb li+li:before {
  12. padding: 8px;
  13. color: black;
  14. content: "/\00a0";
  15. }
  16. ul.breadcrumb li a {color: green;}
  17. </style>
  18. </head>
  19. <body>
  20. <h1>面包屑分页</h1>
  21. <ul class="breadcrumb">
  22. <li><a href="#">Home</a></li>
  23. <li><a href="#">Pictures</a></li>
  24. <li><a href="#">Summer 15</a></li>
  25. <li>Italy</li>
  26. </ul>
  27. </body>
  28. </html>

分类导航