Skip to content

Tailwind CSS - 列表样式类型

在现代 UI 开发中,列表是显示信息的基础,从导航菜单到功能列表。Tailwind CSS 提供了一套功能类,可以声明式地控制列表标记(如项目符号和数字)的外观,使您能够构建与设计系统对齐的清晰、一致的列表样式。

虽然您可以使用 Tailwind CDN 进行快速演示,但实际项目通过 PostCSS 构建过程可以充分发挥 Tailwind CSS 的强大功能。这允许自定义、更好的性能并访问所有功能。所有示例都假定您已设置了一个标准的 Tailwind 项目。

# 1. 安装 Tailwind CSS
npm install -D tailwindcss
npx tailwindcss init
# 2. 在 tailwind.config.js 中配置模板路径
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}
# 3. 将 Tailwind 指令添加到您的主 CSS 文件
@tailwind base;
@tailwind components;
@tailwind utilities;
# 4. 启动构建过程
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

Tailwind 包含三个主要类来控制列表标记样式。这些类直接对应于 list-style-type CSS 属性。

类CSS 属性
list-nonelist-style-type: none;
list-disclist-style-type: disc;
list-decimallist-style-type: decimal;

除了标记样式,您通常还需要控制其相对于内容的位置。为此,请使用 list-inside 或 list-outside。list-outside(默认值)将标记放置在文本流之外,而 list-inside 则将其放置在文本流之内,这可能会影响文本对齐。

此示例演示了如何应用不同的列表样式来创建结构良好的食谱卡片。请注意,与 list-outside 一起使用内边距 (pl-5),以防止标记被裁剪。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipe Card</title>
<link href="/dist/output.css" rel="stylesheet">
</head>
<body class="bg-gray-100 p-8 flex justify-center">
<div class="max-w-md bg-white rounded-lg shadow-lg p-6">
<h2 class="text-2xl font-bold mb-4 text-gray-800">Classic Pancakes</h2>
<h3 class="text-lg font-semibold mt-6 mb-2 text-gray-700">Ingredients</h3>
<ul class="list-disc list-outside pl-5 space-y-2 text-gray-600">
<li>1 ½ cups all-purpose flour</li>
<li>3 ½ teaspoons baking powder</li>
<li>1 tablespoon white sugar</li>
<li>¼ cup melted butter</li>
<li>1 egg</li>
</ul>
<h3 class="text-lg font-semibold mt-6 mb-2 text-gray-700">Instructions</h3>
<ol class="list-decimal list-outside pl-5 space-y-2 text-gray-600">
<li>In a large bowl, sift together the flour, baking powder, salt and sugar.</li>
<li>Make a well in the center and pour in the milk, egg and melted butter; mix until smooth.</li>
<li>Heat a lightly oiled griddle or frying pan over medium-high heat.</li>
<li>Pour or scoop the batter onto the griddle, using approximately 1/4 cup for each pancake.</li>
</ol>
<h3 class="text-lg font-semibold mt-6 mb-2 text-gray-700">Notes</h3>
<ul class="list-none space-y-2 text-gray-500 italic">
<li>For fluffier pancakes, separate the egg yolk and white. Beat the white until stiff and fold it in at the end.</li>
</ul>
</div>
</body>
</html>

Tailwind 的默认集合是最小的,但您可以通过将自己的列表样式添加到 tailwind.config.js 文件中来轻松扩展它。这非常适合添加罗马数字、字母甚至自定义符号等样式。

tailwind.config.js
module.exports = {
// ...
theme: {
extend: {
listStyleType: {
'upper-roman': 'upper-roman',
'lower-alpha': 'lower-alpha',
'square': 'square',
}
}
}
}

将这些添加到您的配置并重新构建 CSS 后,您可以在 HTML 中使用它们:

<ol class="list-upper-roman">
<!-- ... -->
</ol>
<ul class="list-square">
<!-- ... -->
</ul>
  • 内边距是关键: 当使用 list-outside(默认值)时,始终为 <ul> 或 <ol> 元素添加左内边距(例如 pl-5)或外边距(ml-5)。否则,标记可能会在元素的边框盒(bounding box)之外渲染而变得不可见。
  • 响应式设计: 使用响应式变体在不同屏幕尺寸上更改列表样式。例如,移动端上的 list-none 在大屏幕上可能会变为 md:list-disc 以节省空间。
  • 可访问性: 列表标记提供重要的语义上下文。除非您提供替代的视觉提示,否则请避免在列表上使用 list-none。对于装饰性列表,请确保它们对于理解内容不是必不可少的。