实用优先的 CSS ss框架
Tailwind CSS 是一个功能类优先的 CSS 框架,它提供了低级别的实用工具类,让你直接在 HTML 中构建快速的自定义设计。
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
在 tailwind.config.js 中配置:
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
theme: {
extend: {}
},
plugins: []
};
<!-- Flexbox -->
<div class="flex items-center justify-between gap-4">
<div>内容 1</div>
<div>内容 2</div>
</div>
<!-- Grid -->
<div class="grid grid-cols-3 gap-4">
<div>网格项 1</div>
<div>网格项 2</div>
<div>网格项 3</div>
</div>
<!-- Padding -->
<div class="p-4">padding: 1rem</div>
<div class="px-6 py-2">水平 1.5rem, 垂直 0.5rem</div>
<!-- Margin -->
<div class="m-4">margin: 1rem</div>
<div class="mt-2 mb-4">上边距 0.5rem, 下边距 1rem</div>
<div class="bg-blue-500 text-white">蓝色背景,白色文字</div>
<div class="text-gray-700">灰色文字</div>
<div class="border border-red-300">红色边框</div>
<!-- 移动端默认 1 列,中等屏幕 2 列,大屏幕 3 列 -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
<!-- 内容 -->
</div>
<div class="max-w-sm rounded-lg border border-gray-200 bg-white p-6 shadow-md">
<img class="mb-4 h-48 w-full rounded-t-lg object-cover" src="/image.jpg" alt="图片" />
<h3 class="mb-2 text-xl font-bold text-gray-900">卡片标题</h3>
<p class="mb-4 text-gray-600">卡片描述内容</p>
<a href="#" class="rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600"> 查看详情 </a>
</div>
<nav class="flex items-center justify-between bg-white px-6 py-4 shadow-md">
<div class="text-xl font-bold text-gray-800">Logo</div>
<div class="hidden space-x-6 md:flex">
<a href="#" class="text-gray-600 hover:text-blue-500">首页</a>
<a href="#" class="text-gray-600 hover:text-blue-500">关于</a>
<a href="#" class="text-gray-600 hover:text-blue-500">联系</a>
</div>
<button class="rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600">登录</button>
</nav>
对于重复使用的样式,可以创建可复用组件:
<template>
<button :class="['rounded px-4 py-2', variants[variant]]">
{{ label }}
</button>
</template>
<script setup>
const props = defineProps({
variant: {
type: String,
default: 'primary'
}
});
const variants = {
primary: 'bg-blue-500 text-white hover:bg-blue-600',
secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300',
danger: 'bg-red-500 text-white hover:bg-red-600'
};
</script>
对于复杂的组件,可以使用 @apply 指令:
.btn-primary {
@apply rounded px-4 py-2 bg-blue-500 text-white hover:bg-blue-600;
}
module.exports = {
theme: {
extend: {
colors: {
primary: '#3b82f6',
secondary: '#6b7280'
},
spacing: {
128: '32rem'
}
}
}
};
Tailwind CSS 通过扫描文件自动生成包含实际使用样式的 CSS 文件。确保 content 配置正确:
module.exports = {
content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}']
};
npm run build
Tailwind 会自动清除所有未使用的样式,生成最小的 CSS 文件。
Tailwind CSS 提供了一种快速构建现代 Web 界面的方法。通过掌握工具类的使用,你可以:
开始使用 Tailwind CSS,提升你的前端开发效率!