首页 / Tailwind CSS 入门教程 / 列表样式与生成内容

Tailwind CSS 入门教程

列表样式与生成内容

本教程共 50 篇 · 第 28 篇 · 更新于 2026-07-29 · 约 5 分钟阅读

Tailwind CSSTailwind CSS 入门教程列表list-styleaccent-colorcaret-color装饰

28. 列表样式与生成内容

本节目标:掌握列表样式控制、表单控件强调色、文本选择样式等辅助性但实用的工具类,完善对页面细节的控制能力。

列表和装饰元素虽然不常用,但用到的时候 Tailwind 都有对应的工具类。

list-style-type:列表标记类型

控制 <ul><ol> 的标记样式:

类名效果
list-none无标记
list-disc实心圆点(ul 默认)
list-decimal数字编号(ol 默认)
list-square实心方块
<ul class="list-disc pl-5">
  <li>第一项</li>
  <li>第二项</li>
  <li>第三项</li>
</ul>

<ol class="list-decimal pl-5">
  <li>第一步</li>
  <li>第二步</li>
  <li>第三步</li>
</ol>

<!-- 导航菜单:去掉列表标记 -->
<ul class="list-none flex space-x-4">
  <li><a href="#">首页</a></li>
  <li><a href="#">关于</a></li>
  <li><a href="#">联系</a></li>
</ul>
Tip

导航菜单几乎总是用 list-none 去掉默认标记,然后用 flex 做水平排列。

list-style-position:标记位置

控制列表标记在列表项内容区域的内/外侧:

类名效果
list-inside标记在内容内侧(缩进进内容区)
list-outside标记在内容外侧(默认)
<ul class="list-disc list-inside">
  <li>标记在文字内侧,文字会环绕标记</li>
  <li>第二项</li>
</ul>

accent-color:表单强调色

accent-color 控制表单控件(checkbox、radio、range)的强调色:

类名效果
accent-blue-500蓝色
accent-red-500红色
accent-green-500绿色
accent-gray-500灰色
accent-auto浏览器默认
<label class="flex items-center gap-2">
  <input type="checkbox" class="accent-blue-500" checked>
  <span>同意条款</span>
</label>

<input type="range" class="accent-purple-500 w-full">
Note

accent-color 是 CSS 原生属性,v4 直接映射成工具类。它只影响 checkbox、radio、range 和 progress 元素的颜色。

caret-color:光标颜色

caret-color 控制输入框光标的颜色:

类名效果
caret-blue-500蓝色光标
caret-red-500红色光标
caret-transparent隐藏光标
caret-auto默认
<input type="text" class="caret-blue-500 border rounded px-3 py-2">

text-decoration-thickness:装饰线粗细

控制下划线、删除线的粗细:

类名效果
decoration-auto自动(默认)
decoration-from-font字体建议的粗细
decoration-11px
decoration-22px
decoration-44px
decoration-88px
<p class="underline decoration-2">2px 下划线</p>
<p class="line-through decoration-1">1px 删除线</p>

text-underline-offset:下划线偏移

控制下划线离文字的距离:

类名效果
underline-offset-auto自动
underline-offset-00px
underline-offset-11px
underline-offset-22px
underline-offset-44px
underline-offset-88px
<p class="underline underline-offset-4 decoration-2 decoration-blue-500">
  蓝色、2px 粗、偏移 4px 的下划线
</p>

text-indent:首行缩进

控制段落首行缩进:

类名效果
indent-0无缩进
indent-41rem
indent-82rem
indent-123rem
<p class="indent-8">
  中文文章的首行缩进两个字符是传统排版规范...
</p>

text-decoration-color:装饰线颜色

单独设置装饰线的颜色:

<p class="underline decoration-red-500">红色下划线</p>
<p class="line-through decoration-blue-300">蓝色删除线</p>

placeholder:占位符样式

控制 input/textarea 的 placeholder 文字样式:

<input 
  type="text" 
  placeholder="请输入..."
  class="placeholder-gray-400 placeholder:text-sm placeholder:italic"
>
类名效果
placeholder:text-{color}占位符文字颜色
placeholder:text-{size}占位符字号
placeholder:italic占位符斜体
placeholder:font-bold占位符加粗

selection:选中文本样式

控制用户选中文本时的样式:

<p class="selection:bg-blue-200 selection:text-blue-900">
  选中我看看效果
</p>
类名效果
selection:bg-{color}选中文本的背景色
selection:text-{color}选中文字的颜色
selection:no-underline选中文字去掉下划线
Tip

选中文本的颜色搭配很重要。一般选中的背景色用品牌色浅色版,文字颜色用深色版,确保对比度。

marker:列表标记颜色

单独设置列表标记的颜色(不影响文字):

<ul class="list-disc marker:text-blue-500 pl-5">
  <li>蓝色圆点,黑色文字</li>
  <li>第二项</li>
</ul>

实际应用:自定义复选框

<label class="flex items-center gap-3 cursor-pointer">
  <input 
    type="checkbox" 
    class="size-5 accent-blue-500 rounded border-gray-300"
  >
  <span class="text-gray-700">我已阅读并同意服务条款</span>
</label>

实际应用:文章排版

<article class="prose max-w-2xl mx-auto">
  <h1 class="text-3xl font-bold">文章标题</h1>
  
  <p class="indent-8 text-gray-700 leading-relaxed mt-4">
    中文文章的首行缩进是传统排版规范,两个字符的缩进让段落起始更清晰...
  </p>
  
  <ul class="list-disc marker:text-blue-500 pl-5 mt-4 space-y-2 text-gray-700">
    <li>列表项一</li>
    <li>列表项二</li>
    <li>列表项三</li>
  </ul>
  
  <p class="text-gray-700 leading-relaxed mt-4">
    更多内容...
  </p>
</article>

本节小结

  • list-none / list-disc / list-decimal → 列表标记类型
  • list-inside / list-outside → 标记位置
  • accent-{color} → 表单控件强调色
  • caret-{color} → 输入框光标颜色
  • decoration-{thickness} → 装饰线粗细
  • underline-offset-{n} → 下划线偏移
  • selection:bg-{color} → 选中文本样式
  • marker:text-{color} → 列表标记颜色