Tailwind CSS - 主题配置
Tailwind CSS 主题配置用于指定项目的主题。
在"tailwind.config.js"中,"Theme"部分允许您定义项目的调色板、字体比例、字体、断点、边框半径值等。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
screens: {
sm: '480px',
md: '768px',
lg: '976px',
xl: '1440px',
},
colors: {
'blue': '#1fb6ff',
'purple': '#7e5bef',
'pink': '#ff49db',
'orange': '#ff7849',
'green': '#13ce66',
'yellow': '#ffc82c',
'gray-dark': '#273444',
'gray': '#8492a6',
'gray-light': '#d3dce6',
},
fontFamily: {
sans: ['Graphik', 'sans-serif'],
serif: ['Merriweather', 'serif'],
},
extend: {
spacing: {
'128': '32rem',
'144': '36rem',
},
borderRadius: {
'4xl': '2rem',
}
}
}
}
主题结构
"tailwind.config.js"的"Theme"部分包含一系列键,例如"screens"、"colors"、"spacing"和"core plugin",可用于自定义。
Screens
"screens"键可用于在项目中设置响应式断点。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
screens: {
'sm': '640px',
'md': '768px',
'lg': '1024px',
'xl': '1280px',
'2xl': '1536px',
}
}
}
颜色
"colors"键可让您为项目设置全局调色板。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
colors: {
transparent: 'transparent',
black: '#000',
white: '#fff',
gray: {
100: '#f7fafc',
// ...
900: '#1a202c',
},
// ...
}
}
}
Spacing
"Spacing"键可让您设置项目的全局间距和尺寸比例。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
spacing: {
px: '1px',
0: '0',
0.5: '0.125rem',
1: '0.25rem',
1.5: '0.375rem',
2: '0.5rem',
2.5: '0.625rem',
3: '0.75rem',
3.5: '0.875rem',
4: '1rem',
5: '1.25rem',
},
}
}
核心插件
主题设置允许您为每个插件选择选项。例如,borderRadius 用于设置圆角样式。
module.exports = {
theme: {
borderRadius: {
'none': '0',
'sm': '.125rem',
DEFAULT: '.25rem',
'lg': '.5rem',
'full': '9999px',
},
}
}
名称将成为类名,值将设置样式。这将创建类似 rounded 的类。Tailwind 的所有插件都采用此方式。
.rounded-none { border-radius: 0 }
.rounded-sm { border-radius: .125rem }
.rounded { border-radius: .25rem }
.rounded-lg { border-radius: .5rem }
.rounded-full { border-radius: 9999px }
自定义默认主题
您的项目将自动使用默认主题设置。如果您想更改它们,您可以选择自定义主题以满足您的需求。
扩展默认主题
"theme.extend"选项允许您在保留默认值的同时向主题添加新值。这会将新值和现有值结合起来,为您创建新的类。
例如,我们扩展了 fontFamily 属性,添加了 font-display 类,该类可以更改元素上使用的字体:
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
fontFamily: {
display: 'Oswald, ui-serif', // Adds a new `font-display` class
}
}
}
}
将其添加到主题后,您可以像使用其他字体系列实用程序一样使用它:
<h1 class="font-display"> 此脚本使用 Oswald 字体 </h1>
覆盖默认主题
您可以通过在"tailwind.config.js"中的主题部分直接添加覆盖来更改默认主题选项。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
// Replaces all of the default `opacity` values
opacity: {
'0': '0',
'20': '0.2',
'40': '0.4',
'60': '0.6',
'80': '0.8',
'100': '1',
}
}
}
引用其他值
使用闭包引用其他主题值。它提供了一个 theme() 函数,可以使用点符号访问其他值。
例如,您可以通过在 backgroundSize 配置中引用 theme('spacing') 来为间距比例中的每个值生成 background-size 实用程序。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
spacing: {
// ...
},
backgroundSize: ({ theme }) => ({
auto: 'auto',
cover: 'cover',
contain: 'contain',
...theme('spacing')
})
}
}
注意:使用函数作为顶级主题键。
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
fill: ({ theme }) => ({
gray: theme('colors.gray')
})
}
}
引用默认主题
要使用默认主题的值,请从 tailwindcss/defaultTheme 导入。
const defaultTheme = require('tailwindcss/defaultTheme')
module.exports = {
theme: {
extend: {
fontFamily: {
sans: [
'Lato',
...defaultTheme.fontFamily.sans,
]
}
}
}
}
禁用整个核心插件
要禁用核心插件,请在 corePlugins 中将其设置为"false",而不是在主题配置中将其设置为空对象。
/** Don't Do */
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
opacity: {},
}
}
/** Always Do */
/** @type {import('tailwindcss').Config} */
module.exports = {
corePlugins: {
opacity: false,
}
}
配置参考
除屏幕、颜色和间距外,大多数主题对象键与 Tailwind 的核心插件匹配。某些插件没有主题键,因为它们只接受固定值。您还可以使用 theme.extend 键扩展默认主题。
所有这些键也可在 theme.extend 键下使用,以启用扩展默认主题的功能。
| Class | CSS 属性 |
|---|---|
| accentColor | accent-color 属性的值 |
| animation | animation 属性的值 |
| aria | aria 属性的值 |
| aspectRatio | aspect-ratio 属性的值 |
| backdropBlur | backgroundBlur 插件的值 |
| backdropBrightness | backgroundBrightness 插件的值 |
| backdropContrast | backgroundContrast 插件的值 |
| backdropGrayscale | backgroundGrayscale 插件的值 |
| backdropHueRotate | backgroundHueRotate 的值插件 |
| backdropInvert | backdropInvert 插件的值 |
| backdropOpacity | backdropOpacity 插件的值 |
| backdropSaturate | backdropSaturate 插件的值 |
| backdropSepia | backgroundSepia 插件的值 |
| backgroundColor | background-color 属性的值 |
| backgroundImage | background-image 属性的值 |
| backgroundOpacity | background-opacity 属性的值 |
| backgroundPosition | background-position 属性的值 |
| backgroundSize | background-size 属性的值 |
| blur | blur 插件的值 |
| borderColor | border-color 属性的值 |
| borderOpacity | borderOpacity 插件的值 |
| borderRadius | border-radius 属性的值 |
| borderSpacing | border-spacing 属性的值 |
| borderWidth | borderWidth 插件的值 |
| boxShadow | box-shadow 属性的值 |
| boxShadowColor | boxShadowColor 插件的值 |
| brightness | brightness 插件的值 |
| caretColor | caret-color 属性的值 |
| colors | 项目的调色板 |
| columns | columns 属性的值 |
| container | container 插件的配置 |
| content | content 属性的值 |
| contrast | contrast 插件的值 |
| cursor | 值光标属性的值 |
| divideColor | divideColor 插件的值 |
| divideOpacity | divideOpacity 插件的值 |
| divideWidth | divideWidth 插件的值 |
| dropShadow | dropShadow 插件的值 |
| fill | fill 插件的值 |
| flex | flex 属性的值 |
| flexBasis | flex-basis 属性的值 |
| flexGrow | flex-grow 属性的值 |
| flexShrink | flex-shrink 属性的值 |
| fontFamily | font-family 属性的值 |
| fontSize | font-size 属性的值 |
| fontWeight | font-weight 属性的值 |
| gap | gap 属性的值 |
| gradientColorStops | gradientColorStops 插件的值 |
| gradientColorStopPositions | gradient-color-stop-positions 属性的值 |
| grayscale | grayscale 插件的值 |
| gridAutoColumns | grid-auto-columns 属性的值 |
| gridAutoRows | grid-auto-rows 的值属性 |
| gridColumn | grid-column 属性的值 |
| gridColumnEnd | grid-column-end 属性的值 |
| gridColumnStart | grid-column-start 属性的值 |
| gridRow | grid-row 属性的值 |
| gridRowEnd | grid-row-end 属性的值 |
| gridRowStart | grid-row-start 属性的值 |
| gridTemplateColumns | grid-template-columns 属性的值 |
| gridTemplateRows | grid-template-rows 属性的值 |
| height | height 的值属性 |
| hueRotate | hueRotate插件的值 |
| inset | top、right、bottom 和 left 属性的值 |
| invert | invert 插件的值 |
| keyframes | 动画插件中使用的关键帧值 |
| letterSpacing | letter-spacing 属性的值 |
| lineHeight | line-height 属性的值 |
| listStyleType | list-style-type 属性的值 |
| listStyleImage | list-style-image 属性的值 |
| margin | margin 属性的值 |
| lineClamp | line-clamp 属性的值 |
| maxHeight | max-height 属性的值 |
| maxWidth | max-width 属性的值 |
| minHeight | min-height 属性的值 |
| minWidth | min-width 属性的值 |
| objectPosition | object-position 属性的值 |
| opacity | opacity 属性的值 |
| order | order 属性的值 |
| outlineColor | outline-color 属性的值 |
| outlineOffset | outline-offset 属性的值 |
| outlineWidth | outline-width 属性的值 |
| padding | padding 属性的值 |
| placeholderColor | placeholderColor 插件的值 |
| placeholderOpacity | placeholderOpacity 插件的值 |
| ringColor | ringColor 插件的值 |
| ringOffsetColor | ringOffsetColor 插件的值 |
| ringOffsetWidth | ringOffsetWidth 插件的值 |
| ringOpacity | ringOpacity 插件的值 |
| ringWidth | ringWidth 插件的值 |
| rotate | rotate 插件的值 |
| saturate | saturate 插件的值 |
| scale | scale 插件的值 |
| screens | 项目的响应式断点 |
| scrollMargin | scroll-margin 属性的值 |
| scrollPadding | scroll-padding 属性的值 |
| sepia | sepia 插件的值 |
| skew | skew 插件的值 |
| space | space 插件的值 |
| spacing | 项目的间距比例 |
| stroke | stroke 属性的值 |
| strokeWidth | stroke-width 属性的值 |
| supports | supports 属性的值 |
| data | data 属性的值 |
| textColor | color 属性的值 |
| textDecorationColor | text-decoration-color 属性的值 |
| textDecorationThickness | text-decoration-thickness 属性的值 |
| textIndent | text-indent 属性的值 |
| textOpacity | textOpacity 插件的值 |
| textUnderlineOffset | text-underline-offset 属性的值 |
| transformOrigin | transform-origin 属性 |
| transitionDelay | transition-delay 属性的值 |
| transitionDuration | transition-duration 属性的值 |
| transitionProperty | transition-property 属性的值 |
| transitionTimingFunction | transition-timing-function 属性的值 |
| translate | translate 插件的值 |
| size | size 的值属性 |
| width | width 属性的值 |
| willChange | will-change 属性的值 |
| zIndex | z-index 属性的值 |

