Tailwind CSS 自定义配置 #
配置文件 #
创建配置文件 #
bash
npx tailwindcss init
# 或创建完整配置
npx tailwindcss init --full
配置文件结构 #
javascript
/** @type {import('tailwindcss').Config} */
module.exports = {
// 内容扫描路径
content: ['./src/**/*.{html,js}'],
// 主题配置
theme: {
extend: {},
},
// 插件
plugins: [],
// 核心插件
corePlugins: {},
// 变体
variants: {},
// 前缀
prefix: '',
// 重要选择器
important: false,
// 分隔符
separator: ':',
}
主题扩展 #
扩展 vs 覆盖 #
javascript
module.exports = {
theme: {
// 扩展:保留默认值,添加新值
extend: {
colors: {
brand: '#1da1f2',
},
},
// 覆盖:完全替换默认值(不推荐)
// colors: {
// brand: '#1da1f2',
// },
},
}
颜色配置 #
添加颜色 #
javascript
module.exports = {
theme: {
extend: {
colors: {
// 单个颜色
brand: '#1da1f2',
// 颜色调色板
brand: {
50: '#f0f9ff',
100: '#e0f2fe',
200: '#bae6fd',
300: '#7dd3fc',
400: '#38bdf8',
500: '#0ea5e9',
600: '#0284c7',
700: '#0369a1',
800: '#075985',
900: '#0c4a6e',
950: '#082f49',
},
},
},
},
}
html
<div class="bg-brand">品牌色</div>
<div class="bg-brand-500">品牌主色</div>
覆盖默认颜色 #
javascript
module.exports = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
white: '#ffffff',
black: '#000000',
primary: '#3b82f6',
secondary: '#6b7280',
},
},
}
间距配置 #
javascript
module.exports = {
theme: {
extend: {
spacing: {
'128': '32rem',
'144': '36rem',
},
},
},
}
断点配置 #
自定义断点 #
javascript
module.exports = {
theme: {
screens: {
'xs': '475px',
'sm': '640px',
'md': '768px',
'lg': '1024px',
'xl': '1280px',
'2xl': '1536px',
},
},
}
断点名称 #
javascript
module.exports = {
theme: {
screens: {
'tablet': '640px',
'laptop': '1024px',
'desktop': '1280px',
},
},
}
html
<div class="tablet:text-lg">平板文字</div>
字体配置 #
javascript
module.exports = {
theme: {
extend: {
fontFamily: {
'display': ['Helvetica Neue', 'sans-serif'],
'body': ['Georgia', 'serif'],
'mono': ['Fira Code', 'monospace'],
},
fontSize: {
'2xs': ['0.625rem', { lineHeight: '0.75rem' }],
'10xl': ['10rem', { lineHeight: '1' }],
},
},
},
}
边框圆角配置 #
javascript
module.exports = {
theme: {
extend: {
borderRadius: {
'4xl': '2rem',
'5xl': '2.5rem',
},
},
},
}
阴影配置 #
javascript
module.exports = {
theme: {
extend: {
boxShadow: {
'inner-lg': 'inset 0 2px 4px 0 rgb(0 0 0 / 0.1)',
'brand': '0 0 20px rgba(59, 130, 246, 0.5)',
},
},
},
}
动画配置 #
javascript
module.exports = {
theme: {
extend: {
animation: {
'wiggle': 'wiggle 1s ease-in-out infinite',
'fade-in': 'fadeIn 0.5s ease-in-out',
},
keyframes: {
wiggle: {
'0%, 100%': { transform: 'rotate(-3deg)' },
'50%': { transform: 'rotate(3deg)' },
},
fadeIn: {
'0%': { opacity: '0' },
'100%': { opacity: '1' },
},
},
},
},
}
间距比例 #
javascript
module.exports = {
theme: {
extend: {
spacing: {
'18': '4.5rem',
'22': '5.5rem',
},
},
},
}
容器配置 #
javascript
module.exports = {
theme: {
container: {
center: true,
padding: {
DEFAULT: '1rem',
sm: '2rem',
lg: '4rem',
xl: '5rem',
},
},
},
}
核心插件 #
禁用核心插件 #
javascript
module.exports = {
corePlugins: {
preflight: false, // 禁用基础样式重置
opacity: false, // 禁用透明度工具
},
}
前缀 #
javascript
module.exports = {
prefix: 'tw-',
}
html
<div class="tw-bg-blue-500 tw-text-white">
带前缀的类名
</div>
重要选择器 #
javascript
module.exports = {
important: true,
// 或指定选择器
important: '#app',
}
内容配置 #
基本配置 #
javascript
module.exports = {
content: [
'./src/**/*.{html,js}',
'./pages/**/*.{html,js}',
'./components/**/*.{html,js}',
],
}
Safelist #
javascript
module.exports = {
safelist: [
'bg-blue-500',
'text-center',
{
pattern: /bg-(red|green|blue)-(100|200|300)/,
},
],
}
插件配置 #
javascript
const plugin = require('tailwindcss/plugin')
module.exports = {
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
plugin(function({ addUtilities, addComponents, theme }) {
// 添加工具类
addUtilities({
'.text-shadow': {
'text-shadow': '2px 2px 4px rgba(0, 0, 0, 0.1)',
},
})
// 添加组件
addComponents({
'.btn': {
padding: '.5rem 1rem',
borderRadius: '.25rem',
fontWeight: '600',
},
})
}),
],
}
完整配置示例 #
javascript
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
darkMode: 'class',
theme: {
screens: {
'sm': '640px',
'md': '768px',
'lg': '1024px',
'xl': '1280px',
'2xl': '1536px',
},
colors: {
transparent: 'transparent',
current: 'currentColor',
white: '#ffffff',
black: '#000000',
primary: {
50: '#eff6ff',
100: '#dbeafe',
200: '#bfdbfe',
300: '#93c5fd',
400: '#60a5fa',
500: '#3b82f6',
600: '#2563eb',
700: '#1d4ed8',
800: '#1e40af',
900: '#1e3a8a',
},
},
fontFamily: {
sans: ['Inter', 'sans-serif'],
mono: ['Fira Code', 'monospace'],
},
extend: {
spacing: {
'128': '32rem',
},
borderRadius: {
'4xl': '2rem',
},
animation: {
'fade-in': 'fadeIn 0.5s ease-in-out',
},
keyframes: {
fadeIn: {
'0%': { opacity: '0' },
'100%': { opacity: '1' },
},
},
},
},
plugins: [
require('@tailwindcss/forms'),
require('@tailwindcss/typography'),
],
}
下一步 #
掌握自定义配置后,继续学习 插件开发 了解如何创建插件!
最后更新:2026-03-28