用于显示分页以指示跨多个页面存在一系列相关内容的文档和示例。
我们使用一大块连接的链接进行分页,使得链接很难丢失,并且很容易扩展,同时提供了较大的命中区域。分页是用列表HTML元素构建的,因此屏幕阅读器可以宣布可用链接的数量。使用包装nav元素将其标识为用于屏幕阅读器和其他辅助技术的导航部分。
此外,由于页面可能有多个这样的导航部分,建议为
aria-label="Search results pages"
<nav aria-label="Page navigation example"> <ul class="pagination"> <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="#">3</a></li> <li class="page-item"><a class="page-link" href="#">Next</a></li> </ul> </nav>
要使用图标或符号来代替某些分页链接的文本吗?确保使用aria属性提供适当的屏幕阅读器支持。
<nav aria-label="Page navigation example"> <ul class="pagination"> <li class="page-item"> <a class="page-link" href="#" aria-label="Previous"> <span aria-hidden="true">«</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> </a> </li> </ul> </nav>
分页链接可根据不同情况进行自定义。对于显示为不可单击的链接使用.disabled,对于显示为当前页的链接使用.active。
虽然.disabled类使用pointer-events: none来尝试禁用a的链接功能,但是CSS属性还没有标准化,并且不考虑键盘导航。因此,您应该始终在禁用的链接上添加tabindex="-1",并使用自定义JavaScript完全禁用其功能。
pointer-events: none
tabindex="-1"
<nav aria-label="..."> <ul class="pagination"> <li class="page-item disabled"> <a class="page-link" href="#" tabindex="-1" aria-disabled="true">Previous</a> </li> <li class="page-item"><a class="page-link" href="#">1</a></li> <li class="page-item active" aria-current="page"> <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="#">Next</a> </li> </ul> </nav>
您可以选择调出span的活动或禁用的锚定,或者在上一个/下一个箭头的情况下忽略锚定,以删除单击功能并防止键盘聚焦,同时保留所需的样式。
<nav aria-label="..."> <ul class="pagination"> <li class="page-item disabled"> <span class="page-link">Previous</span> </li> <li class="page-item"><a class="page-link" href="#">1</a></li> <li class="page-item active" aria-current="page"> <span class="page-link">2</span> </li> <li class="page-item"><a class="page-link" href="#">3</a></li> <li class="page-item"> <a class="page-link" href="#">Next</a> </li> </ul> </nav>
喜欢更大或更小的分页?加上.pagination-lg 或者 .pagination-sm,或使用其他尺寸。
<nav aria-label="..."> <ul class="pagination pagination-lg"> <li class="page-item active" aria-current="page"> <span class="page-link">1</span> </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> </ul> </nav>
<nav aria-label="..."> <ul class="pagination pagination-sm"> <li class="page-item active" aria-current="page"> <span class="page-link">1</span> </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> </ul> </nav>
使用flexbox实用程序更改分页组件的对齐方式。
<nav aria-label="Page navigation example"> <ul class="pagination justify-content-center"> <li class="page-item disabled"> <a class="page-link" href="#" tabindex="-1" aria-disabled="true">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="#">3</a></li> <li class="page-item"> <a class="page-link" href="#">Next</a> </li> </ul> </nav>
<nav aria-label="Page navigation example"> <ul class="pagination justify-content-end"> <li class="page-item disabled"> <a class="page-link" href="#" tabindex="-1" aria-disabled="true">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="#">3</a></li> <li class="page-item"> <a class="page-link" href="#">Next</a> </li> </ul> </nav>
$pagination-padding-y: .375rem; $pagination-padding-x: .75rem; $pagination-padding-y-sm: .25rem; $pagination-padding-x-sm: .5rem; $pagination-padding-y-lg: .75rem; $pagination-padding-x-lg: 1.5rem; $pagination-color: $link-color; $pagination-bg: $white; $pagination-border-width: $border-width; $pagination-border-radius: $border-radius; $pagination-margin-start: -$pagination-border-width; $pagination-border-color: $gray-300; $pagination-focus-color: $link-hover-color; $pagination-focus-bg: $gray-200; $pagination-focus-box-shadow: $input-btn-focus-box-shadow; $pagination-focus-outline: 0; $pagination-hover-color: $link-hover-color; $pagination-hover-bg: $gray-200; $pagination-hover-border-color: $gray-300; $pagination-active-color: $component-active-color; $pagination-active-bg: $component-active-bg; $pagination-active-border-color: $pagination-active-bg; $pagination-disabled-color: $gray-600; $pagination-disabled-bg: $white; $pagination-disabled-border-color: $gray-300; $pagination-transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out; $pagination-border-radius-sm: $border-radius-sm; $pagination-border-radius-lg: $border-radius-lg;
@mixin pagination-size($padding-y, $padding-x, $font-size, $border-radius) { .page-link { padding: $padding-y $padding-x; @include font-size($font-size); } .page-item { @if $pagination-margin-start == (-$pagination-border-width) { &:first-child { .page-link { @include border-start-radius($border-radius); } } &:last-child { .page-link { @include border-end-radius($border-radius); } } } @else { //Add border-radius to all pageLinks in case they have left margin .page-link { @include border-radius($border-radius); } } } }