Tailwind CSS - 自定义颜色
Tailwind CSS 颜色允许您为项目指定默认调色板。
如果您还没有确定自己的特定品牌,Tailwind 附带一个预制调色板来帮助您入门。
#f8fafc
#f1f5f9
#e2e8f0
#cbd5e1
#94a3b8
#64748b
#475569
#334155
#1e293b
#0f172a
#020617
#f9fafb
#f3f4f6
#e5e7eb
#d1d5db
#9ca3af
#6b7280
#4b5563
#374151
#1f2937
#1f2937
#030712;
#fafafa
#f4f4f5
#e4e4e7
#d4d4d8
#a1a1aa
#71717a
#52525b
#3f3f46
#27272a
#18181b
#09090b
#fafafa
#f5f5f5
#e5e5e5
#d4d4d4
#a3a3a3
#737373
#525252
#404040
#262626
#171717
#0a0a0a
#fafaf9
#f5f5f4
#e7e5e4
#d6d3d1
#a8a29e
#78716c
#57534e
#44403c
#292524
#1c1917
#0c0a09
#fef2f2
#fee2e2
#fecaca
#fca5a5
#f87171
#ef4444
#dc2626
#b91c1c
#991b1b
#7f1d1d
#450a0a
#fff7ed
#ffedd5
#fed7aa
#fdba74
#fb923c
#f97316
#ea580c
#c2410c
#9a3412
#7c2d12
#431407
#fffbeb
#fef3c7
#fde68a
#fcd34d
#fbbf24
#f59e0b
#d97706
#b45309
#92400e
#78350f
#451a03
#fefce8
#fef9c3
#fef08a
#fde047
#facc15
#eab308
#ca8a04
#a16207
#854d0e
#713f12
#422006
#f7fee7
#ecfccb
#d9f99d
#bef264
#a3e635
#84cc16
#65a30d
#4d7c0f
#3f6212
#365314
#1a2e05
#f0fdf4
#dcfce7
#bbf7d0
#86efac
#4ade80
#22c55e
#16a34a
#15803d
#166534
#14532d
#052e16
#ecfdf5
#d1fae5
#a7f3d0
#6ee7b7
#34d399
#10b981
#059669
#047857
#065f46
#064e3b
#022c22
#f0fdfa
#ccfbf1
#99f6e4
#5eead4
#2dd4bf
#14b8a6
#0d9488
#0f766e
#115e59
#134e4a
#042f2e
#ecfeff
#cffafe
#a5f3fc
#67e8f9
#22d3ee
#06b6d4
#0891b2
#0e7490
#155e75
#164e63
#083344
#f0f9ff
#e0f2fe
#bae6fd
#7dd3fc
#38bdf8
#0ea5e9
#0284c7
#0369a1
#075985
#0c4a6e
#082f49
#eff6ff
#dbeafe
#bfdbfe
#93c5fd
#60a5fa
#3b82f6
#2563eb
#1d4ed8
#1e40af
#1e3a8a
#172554
#eef2ff
#e0e7ff
#c7d2fe
#a5b4fc
#818cf8
#6366f1
#4f46e5
#4338ca
#3730a3
#312e81
#1e1b4b
#f5f3ff
#ede9fe
#ddd6fe
#c4b5fd
#a78bfa
#8b5cf6
#7c3aed
#6d28d9
#5b21b6
#4c1d95
#2e1065
#faf5ff
#f3e8ff
#e9d5ff
#d8b4fe
#c084fc
#a855f7
#9333ea
#7e22ce
#6b21a8
#581c87
#3b0764
#fdf4ff
#fae8ff
#f5d0fe
#f0abfc
#e879f9
#d946ef
#c026d3
#a21caf
#86198f
#701a75
#4a044e
#fdf2f8
#fce7f3
#fbcfe8
#f9a8d4
#f472b6
#ec4899
#db2777
#be185d
#9d174d
#831843
#500724
#fff1f2
#ffe4e6
#fecdd3
#fda4af
#fb7185
#f43f5e
#e11d48
#be123c
#9d174d
#881337
#4c0519
使用自定义颜色
您可以直接在配置文件的"theme.colors"部分下添加自定义颜色,将默认颜色更改为您自己的选择。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
'white': '#ffffff',
'purple': '#3f3cbb',
'midnight': '#121063',
},
},
}
这些颜色可用于各种方面,例如文本、边框、背景等。您还可以在项目中的特定情况下使用"transparent"和"currentColor"。
<div class="bg-midnight text-tahiti"> <!-- ... --> </div>
颜色对象语法
当您的调色板包含相同颜色的多种色调时,使用嵌套颜色对象语法将它们组合在一起会很方便。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
'white': '#ffffff',
'tahiti': {
100: '#cffafe',
200: '#a5f3fc',
300: '#67e8f9',
400: '#22d3ee',
500: '#06b6d4',
600: '#0891b2',
700: '#0e7490',
800: '#155e75',
900: '#164e63',
},
// ...
},
},
}
嵌套键将与父键组合形成类名,例如 bg-tahiti-400。
如果您不想在值中添加任何额外的单词,请使用"DEFAULT"键。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
colors: {
// ...
'tahiti': {
light: '#67e8f9',
DEFAULT: '#06b6d4',
dark: '#0e7490',
},
// ...
},
},
}
这将创建类似 bg-tahiti、bg-tahiti-light 和 bg-tahiti-dark 的类。
任意值
使用任意值表示法创建一次性自定义颜色类,而不是将其添加到主题中。
<button class="bg-[#1da1f2] text-white ..."> <svg><!-- ... --></svg> Share on Twitter </button>
生成颜色
Tailwind 的默认颜色是经过精心挑选以获得最佳效果的。如需自定义颜色,请使用 UI Colors、Palettte 或 ColorBox 等工具来创建均衡的调色板。
使用默认颜色
您可以通过导入"tailwindcss/colors"并选择所需的颜色来使用 Tailwind 的默认颜色。
const colors = require('tailwindcss/colors')
module.exports = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: colors.black,
white: colors.white,
gray: colors.gray,
emerald: colors.emerald,
indigo: colors.indigo,
yellow: colors.yellow,
},
},
}
颜色名称别名
您还可以将默认颜色重命名为更简单、更容易记住的名称。
const colors = require('tailwindcss/colors')
module.exports = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: colors.black,
white: colors.white,
gray: colors.slate,
green: colors.emerald,
purple: colors.violet,
yellow: colors.amber,
pink: colors.fuchsia,
},
},
}
因为您通常在任何给定项目中只使用一组颜色。对于灰色来说,这种情况尤其常见,因为您通常在任何给定项目中只使用一组颜色。例如,能够输入 bg-gray-300 而不是 bg-neutral-300 就很不错了。
添加其他颜色
如果您想在默认调色板中添加一种全新的颜色,请在设置文件的"theme.extend.colors"部分中添加新颜色。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
colors: {
brown: {
50: '#fdf8f6',
100: '#f2e8e5',
200: '#eaddd7',
300: '#e0cec7',
400: '#d2bab0',
500: '#bfa094',
600: '#a18072',
700: '#977669',
800: '#846358',
900: '#43302b',
},
}
},
},
}
如果您的设计需要,您还可以使用 theme.extend.colors 为现有颜色添加其他色调。
禁用默认颜色
如果您想禁用任何默认颜色,最好的方法是覆盖默认调色板并仅包含您想要的颜色。
const colors = require('tailwindcss/colors')
module.exports = {
theme: {
colors: {
transparent: 'transparent',
current: 'currentColor',
black: colors.black,
white: colors.white,
gray: colors.gray,
emerald: colors.emerald,
indigo: colors.indigo,
yellow: colors.yellow,
},
},
}
命名颜色
Tailwind 默认使用简单的颜色名称和从亮到暗的色阶,但您可以自定义颜色名称以满足项目需求,例如对多个主题使用抽象名称。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
colors: {
primary: '#5c6ac4',
secondary: '#ecc94b',
// ...
}
}
}
您可以像上面一样明确配置这些颜色,也可以从我们的默认调色板中提取颜色并为它们添加别名:
const colors = require('tailwindcss/colors')
module.exports = {
theme: {
colors: {
primary: colors.indigo,
secondary: colors.yellow,
neutral: colors.gray,
}
}
}
使用 CSS 变量
如果您想使用具有不透明度的自定义颜色,请将它们定义为简单的颜色值(例如红色、蓝色等),而不是使用 CSS 变量。
将您的 CSS 变量定义为不带颜色空间函数的通道
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
--color-primary: 255 115 179;
--color-secondary: 111 114 185;
/* ... */
}
}
不要包含颜色空间函数,否则不透明度修改器将不起作用。
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
--color-primary: rgb(255 115 179);
--color-secondary: rgb(111 114 185);
/* ... */
}
}
然后在配置文件中定义颜色,确保包含您正在使用的颜色空间以及特殊的 然后在配置文件中定义颜色,确保包含您正在使用的颜色空间以及特殊的 <alpha-value> 占位符,Tailwind 将在使用不透明度修改器时使用该占位符注入 alpha 值。/p>
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
colors: {
// Using modern `rgb`
primary: 'rgb(var(--color-primary) / <alpha-value>)',
secondary: 'rgb(var(--color-secondary) / <alpha-value>)',
// Using modern `hsl`
primary: 'hsl(var(--color-primary) / <alpha-value>)',
secondary: 'hsl(var(--color-secondary) / <alpha-value>)',
// Using legacy `rgba`
primary: 'rgba(var(--color-primary), <alpha-value>)',
secondary: 'rgba(var(--color-secondary), <alpha-value>)',
}
}
}
定义自定义颜色时,现代语法请使用空格,而传统函数(如 rgba 或 hsla)请使用逗号。
@tailwind base;
@tailwind components;
@tailwind utilities;
@layer base {
:root {
/* For rgb(255 115 179 / <alpha-value>) */
--color-primary: 255 115 179;
/* For hsl(198deg 93% 60% / <alpha-value>) */
--color-primary: 198deg 93% 60%;
/* For rgba(255, 115, 179, <alpha-value>) */
--color-primary: 255, 115, 179;
}
}

