Skip to content

Tailwind CSS - 项目对齐

Tailwind CSS 中的 align-items 工具类用于控制弹性(flex)或网格(grid)容器中项目沿交叉轴的对齐方式。这对于创建垂直对齐的布局至关重要。

虽然你可以使用 Tailwind Play CDN 进行快速演示,但专业的设置能提供更强大的功能和性能。以下是标准的工作流程:

# 1. 通过 npm 安装 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 集成到 Next.js、Vite 或 Laravel 等框架中会更容易。请查阅官方的 Tailwind CSS 安装指南 获取针对特定框架的说明。

这些类应用于弹性(flex)或网格(grid)容器(例如,带有 flex 或 grid 类的元素)。

类名CSS 属性使用场景
items-startalign-items: flex-start;将项目对齐到容器的顶部。
items-endalign-items: flex-end;将项目对齐到容器的底部。
items-centeralign-items: center;将项目对齐到容器的垂直中心。非常常用。
items-baselinealign-items: baseline;根据其文本的基线对齐项目。适用于对齐不同大小的文本。
items-stretchalign-items: stretch;拉伸项目以填充容器的高度(默认行为)。

让我们通过一些实际组件来看看这些工具类的作用。

items-center 类非常适合在功能列表中对齐图标和文本。我们还使用响应式前缀(md:)来在不同的屏幕尺寸上改变布局。

<!-- Assumes you have a modern setup or are using the Play CDN -->
<div class="font-sans p-8 bg-slate-50">
<h3 class="text-2xl font-bold mb-4 text-slate-800">Plan Features</h3>
<div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden">
<div class="p-8">
<ul class="space-y-4">
<!-- Using flex and items-center for vertical alignment -->
<li class="flex items-center">
<svg class="h-6 w-6 text-green-500 mr-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><!-- Checkmark Icon --><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg>
<span class="text-slate-700">Unlimited Projects</span>
</li>
<li class="flex items-center">
<svg class="h-6 w-6 text-green-500 mr-3" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg>
<span class="text-slate-700">Team Collaboration</span>
</li>
<!-- Using items-start for a different alignment style -->
<li class="flex items-start">
<svg class="h-6 w-6 text-green-500 mr-3 mt-1 flex-shrink-0" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" /></svg>
<span class="text-slate-700">24/7 Support with multi-line description to demonstrate top alignment.</span>
</li>
</ul>
</div>
</div>
</div>

默认情况下,网格项目会拉伸以填充行高(items-stretch)。如果我们将此更改为 items-start,则每张卡片的高度将只与其内容的高度相同。

<div class="font-sans p-8 bg-slate-50">
<h3 class="text-2xl font-bold mb-1 text-slate-800">Our Services</h3>
<p class="text-slate-600 mb-6">Using `items-start` to align cards to the top.</p>
<!-- The 'h-auto' on the container is important for demonstration -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 items-start">
<!-- Card 1 -->
<div class="bg-white p-6 rounded-lg shadow-md">
<h4 class="text-xl font-semibold text-sky-600 mb-2">Web Development</h4>
<p class="text-slate-700">Creating responsive and fast websites.</p>
</div>
<!-- Card 2 (with more content) -->
<div class="bg-white p-6 rounded-lg shadow-md">
<h4 class="text-xl font-semibold text-sky-600 mb-2">Mobile Apps</h4>
<p class="text-slate-700">Building native and cross-platform mobile applications that users love to use and engage with daily.</p>
</div>
<!-- Card 3 -->
<div class="bg-white p-6 rounded-lg shadow-md">
<h4 class="text-xl font-semibold text-sky-600 mb-2">UI/UX Design</h4>
<p class="text-slate-700">Designing intuitive user interfaces.</p>
</div>
</div>
</div>
  • items-stretch 不起作用: 这通常发生在弹性/网格项目具有固定高度(例如 h-24)时。要使 items-stretch 生效,项目的高度必须为 auto(默认值)。
  • items-center 不起作用: 确保父容器具有定义的高度或包含能赋予其高度的内容。你无法在零高度的容器内垂直居中项目。为了测试,可以给父元素添加背景颜色(bg-red-200)和高度类(h-64)以可视化其边界。
  • items-baseline 看起来像 items-start: 如果所有子元素中的文本具有相同的字体大小和行高,则可能会发生这种情况。items-baseline 的效果在对齐不同大小的文本时最为明显。

你已经掌握了交叉轴对齐!要完全控制布局,接下来应该学习主轴对齐。请查阅我们的 Justify Content 指南。