Tailwind 可用工具类快速构建语义清晰、响应式且可访问的分页按钮组。核心是 flex + space-x-1 布局,px-3 py-1.5 text-sm rounded-md 控制基础样式,当前页用 z-10 bg-blue-600 text-white aria-current="page",禁用态加 opacity-50 cursor-not-allowed;省略号用 span.text-gray-500,输入框固定宽并强化 focus 状态;移动端用 sm:flex-wrap 和 sm:space-y-1 适配,全组件需 role="navigation" 和 aria-label="Pagination"。

直接用 Tailwind 的工具类组合出分页按钮,别写自定义 CSS
Tailwind 本身没有 pagination 组件,但用现成的间距、边框、颜色、圆角、悬停等工具类,30 秒就能搭出语义清晰、响应式友好的分页按钮组。关键不是“找 pagination 类”,而是理解分页结构怎么拆解。
分页容器和按钮的最小必要类组合
一个典型的分页结构是 <nav> 包裹 <ul>,每个页码项是 <li> 里的 <a> 或 <button>。需要控制:对齐、间距、尺寸、状态样式、焦点可访问性。
-
flex+space-x-1实现按钮水平排列与间隙(space-x-1比mx-1更可靠,避免首尾多出外边距) - 每个按钮用
px-3py-1.5text-smrounded-md控制基础尺寸和圆角 - 默认态用
text-gray-700bg-whiteborder border-gray-300 - 当前页用
z-10bg-blue-600text-whiteborder-blue-600(z-10防止被相邻按钮边框遮盖) - 禁用态(如「上一页」在第一页时)加
opacity-50cursor-not-allowed
处理「省略号」和「跳转输入框」这类非数字按钮
分页里常有 <span>…</span> 或 <input type="number">,它们不能套用链接样式,需单独适配。
-
<span class="px-3 py-1.5 text-sm text-gray-500">…</span>—— 保持垂直对齐,用text-gray-500区分语义 -
<input class="w-16 px-2 py-1.5 text-sm border border-gray-300 rounded-md focus:ring-1 focus:ring-blue-500 focus:border-blue-500">—— 宽度固定防跳动,聚焦态必须有明确视觉反馈 - 如果用
<button>做「GO」,务必加type="button",避免表单意外提交
响应式断点和可访问性容易漏的细节
小屏下按钮挤在一起、键盘用户无法看到焦点、屏幕阅读器读不出当前页——这些不是“锦上添花”,是分页功能可用的前提。
立即学习“前端免费学习笔记(深入)”;
- 移动端用
sm:flex-wrap让按钮换行,配合sm:space-x-0+sm:space-y-1重置间隙方向 - 所有可交互元素必须有
focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2,且focus:ring-offset-2要配合父容器focus:ring-offset-white(否则白底上环偏移不生效) - 当前页按钮加
aria-current="page"属性,这是屏幕阅读器识别“这是当前页”的唯一标准方式 - 整个分页区域加
role="navigation"和aria-label="Pagination",让辅助技术知道这是导航区块
<nav class="inline-flex items-center justify-center" role="navigation" aria-label="Pagination">
<ul class="flex flex-wrap items-center -space-x-px sm:space-x-0 sm:space-y-1 sm:flex-col">
<li>
<a href="/?page=1" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">«</a>
</li>
<li>
<a href="/?page=1" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">1</a>
</li>
<li>
<span class="px-3 py-1.5 text-sm text-gray-500">…</span>
</li>
<li>
<a href="/?page=5" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">5</a>
</li>
<li>
<a href="/?page=6" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">6</a>
</li>
<li>
<a href="/?page=7" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">7</a>
</li>
<li>
<span class="px-3 py-1.5 text-sm text-gray-500">…</span>
</li>
<li>
<a href="/?page=10" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">10</a>
</li>
<li>
<a href="/?page=2" aria-current="page" class="z-10 px-3 py-1.5 text-sm font-medium text-white bg-blue-600 border border-blue-600 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">2</a>
</li>
<li>
<a href="/?page=3" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">3</a>
</li>
<li>
<a href="/?page=10" class="px-3 py-1.5 text-sm text-gray-700 bg-white border border-gray-300 hover:bg-gray-50 focus:z-10 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:ring-offset-white">»</a>
</li>
</ul>
</nav>
真正卡住人的往往不是类名记不住,而是忘了 aria-current="page" 这种语义标记,或者在响应式切换时没重置 space-x 导致小屏按钮重叠。样式可以调,但可访问性和语义一旦漏掉,后期补成本远高于一开始写对。










