1. 定制
  2. 主题配置

您可以在 tailwind.config.js 文件的 theme 部分定义项目的调色板、类型比例、字体、断点、边框半径值等。

tailwind.config.js
/** @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',
      }
    }
  }
}

我们提供了一个合理的 default theme 和一组非常慷慨的价值观来帮助您入门,但不要害怕改变或扩展它;我们鼓励您根据需要尽可能地对其进行自定义,以适合您的设计目标。


主题结构

theme 对象包含 screenscolorsspacing 的键,以及每个可自定义的 core plugin 的键。

请参阅 theme configuration referencedefault theme 以获取主题选项的完整列表。

屏幕

screens 键允许您自定义项目中的响应断点。

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    screens: {
      'sm': '640px',
      'md': '768px',
      'lg': '1024px',
      'xl': '1280px',
      '2xl': '1536px',
    }
  }
}

要了解更多信息,请参阅breakpoint customization documentation

颜色

colors 键允许您自定义项目的全局调色板。

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    colors: {
      transparent: 'transparent',
      black: '#000',
      white: '#fff',
      gray: {
        100: '#f7fafc',
        // ...
        900: '#1a202c',
      },

      // ...
    }
  }
}

默认情况下,这些颜色被所有与颜色相关的核心插件继承,如backgroundColorborderColortextColor等。

要了解更多信息,请参阅color customization documentation

间距

spacing 键允许您自定义项目的全局间距和尺寸比例。

tailwind.config.js
/** @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',
      6: '1.5rem',
      7: '1.75rem',
      8: '2rem',
      9: '2.25rem',
      10: '2.5rem',
      11: '2.75rem',
      12: '3rem',
      14: '3.5rem',
      16: '4rem',
      20: '5rem',
      24: '6rem',
      28: '7rem',
      32: '8rem',
      36: '9rem',
      40: '10rem',
      44: '11rem',
      48: '12rem',
      52: '13rem',
      56: '14rem',
      60: '15rem',
      64: '16rem',
      72: '18rem',
      80: '20rem',
      96: '24rem',
    },
  }
}

默认情况下,这些值由 paddingmarginwidthheightmaxHeightflex-basisgapinsetspacetranslatescrollMarginscrollPaddingtextIndent 核心插件继承。

要了解更多信息,请参阅spacing customization documentation

核心插件

theme 部分的其余部分用于配置每个单独核心插件可用的值。

例如,borderRadius 键可让您自定义将生成哪些边框半径实用程序:

module.exports = {
  theme: {
    borderRadius: {
      'none': '0',
      'sm': '.125rem',
      DEFAULT: '.25rem',
      'lg': '.5rem',
      'full': '9999px',
    },
  }
}

键决定生成的类的后缀,值决定实际 CSS 声明的值。

上面的示例 borderRadius 配置将生成以下 CSS 类:

.rounded-none { border-radius: 0 }
.rounded-sm   { border-radius: .125rem }
.rounded      { border-radius: .25rem }
.rounded-lg   { border-radius: .5rem }
.rounded-full { border-radius: 9999px }

您会注意到,在主题配置中使用 DEFAULT 键会创建没有后缀的类 rounded。这是 Tailwind 中的常见惯例,并且所有核心插件都支持。

要了解有关定制特定核心插件的更多信息,请访问该插件的文档。

有关可用主题属性及其默认值的完整参考,请参阅see the default theme configuration


自定义默认主题

开箱即用,您的项目将自动继承 the default theme configuration 的值。如果您想自定义默认主题,您可以根据自己的目标选择几种不同的选项。

扩展默认主题

如果您希望保留主题选项的默认值,但又想添加新值,请在配置文件中的 theme.extend 键下添加扩展。此键下的值将与现有的 theme 值合并,并自动变为可供您使用的新类。

举个例子,这里我们扩展 fontFamily 属性来添加 font-display 类,它可以改变元素上使用的字体:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      fontFamily: {
        display: 'Oswald, ui-serif', // Adds a new `font-display` class
      }
    }
  }
}

将其添加到主题后,您可以像使用任何其他字体系列实用程序一样使用它:

<h1 class="font-display">
  This uses the Oswald font
</h1>

在某些情况下,属性会映射到 variants,可将其放置在实用程序前面,以有条件地应用其样式。例如,要添加与现有响应式屏幕一样工作的 3xl 屏幕尺寸,请在 screens 键下添加一个属性:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    extend: {
      screens: {
        '3xl': '1600px', // Adds a new `3xl:` screen variant
      }
    }
  }
}

通过这一添加,除了现有的响应式变体(如 smmdlg 等)外,还提供了新的 3xl 屏幕尺寸。您可以将这个新变体放在实用程序类之前来使用它:

<blockquote class="text-base md:text-md 3xl:text-lg">
  Oh I gotta get on that internet, I'm late on everything!
</blockquote>

覆盖默认主题

要覆盖默认主题中的选项,请直接在 tailwind.config.jstheme 部分下添加覆盖:

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',
    }
  }
}

这将完全取代 Tailwind 对该键的默认配置,因此在上面的示例中,不会生成任何默认不透明度实用程序。

任何您提供的键都将从默认主题继承,因此在上面的例子中,颜色、间距、边框半径、背景位置等的默认主题配置将被保留。

当然,您可以在同一配置中覆盖默认主题的某些部分并扩展默认主题的其他部分:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    opacity: {
      '0': '0',
      '20': '0.2',
      '40': '0.4',
      '60': '0.6',
      '80': '0.8',
      '100': '1',
    },
    extend: {
      screens: {
        '3xl': '1600px',
      }
    }
  }
}

引用其他值

如果您需要引用主题中的另一个值,您可以通过提供闭包而不是静态值来实现。闭包将接收一个包含 theme() 函数的对象,您可以使用该函数通过点符号查找主题中的其他值。

例如,您可以通过在 backgroundSize 配置中引用 theme('spacing'),为间距比例中的每个值生成 background-size 实用程序:

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    spacing: {
      // ...
    },
    backgroundSize: ({ theme }) => ({
      auto: 'auto',
      cover: 'cover',
      contain: 'contain',
      ...theme('spacing')
    })
  }
}

theme() 函数会尝试从完全合并的主题对象中找到您要查找的值,因此它可以引用您自己的自定义项以及默认主题值。它还可以递归工作,因此只要链末尾有一个静态值,它就能够解析您要查找的值。

请注意,您只能将这种闭包与顶级主题键一起使用,而不能与每个部分内的键一起使用。

您不能对单个值使用函数

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    fill: {
      gray: ({ theme }) => theme('colors.gray')
    }
  }
}

使用函数作为顶级主题键

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    fill: ({ theme }) => ({
      gray: theme('colors.gray')
    })
  }
}

引用默认主题

如果您出于任何原因想要引用默认主题中的值,您可以从 tailwindcss/defaultTheme 导入它。

举个例子,如果你想将字体系列添加到 Tailwind 的默认字体堆栈之一中,这个功能就会很有用:

tailwind.config.js
const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  theme: {
    extend: {
      fontFamily: {
        sans: [
          'Lato',
          ...defaultTheme.fontFamily.sans,
        ]
      }
    }
  }
}

禁用整个核心插件

如果您不想为某个核心插件生成任何类,最好在 corePlugins 配置中将该插件设置为 false,而不是在 theme 配置中为该键提供一个空对象。

不要在主题配置中分配空对象

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    opacity: {},
  }
}

请在 corePlugins 配置中禁用该插件

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  corePlugins: {
    opacity: false,
  }
}

最终结果是相同的,但由于许多核心插件没有公开配置,因此只能使用 corePlugins 来禁用它们,因此最好保持一致。


配置参考

除了 screenscolorsspacing 之外,theme 对象中的所有键都映射到 Tailwind 的 core plugins 之一。由于许多插件负责仅接受一组静态值的 CSS 属性(例如 float),请注意并非每个插件在 theme 对象中都有相应的键。

所有这些键也可在 theme.extend 键下使用,以启用 extending the default theme

KeyDescription
accentColorValues for the accent-color property
animationValues for the animation property
ariaValues for the aria property
aspectRatioValues for the aspect-ratio property
backdropBlurValues for the backdropBlur plugin
backdropBrightnessValues for the backdropBrightness plugin
backdropContrastValues for the backdropContrast plugin
backdropGrayscaleValues for the backdropGrayscale plugin
backdropHueRotateValues for the backdropHueRotate plugin
backdropInvertValues for the backdropInvert plugin
backdropOpacityValues for the backdropOpacity plugin
backdropSaturateValues for the backdropSaturate plugin
backdropSepiaValues for the backdropSepia plugin
backgroundColorValues for the background-color property
backgroundImageValues for the background-image property
backgroundOpacityValues for the background-opacity property
backgroundPositionValues for the background-position property
backgroundSizeValues for the background-size property
blurValues for the blur plugin
borderColorValues for the border-color property
borderOpacityValues for the borderOpacity plugin
borderRadiusValues for the border-radius property
borderSpacingValues for the border-spacing property
borderWidthValues for the borderWidth plugin
boxShadowValues for the box-shadow property
boxShadowColorValues for the boxShadowColor plugin
brightnessValues for the brightness plugin
caretColorValues for the caret-color property
colorsYour project's color palette
columnsValues for the columns property
containerConfiguration for the container plugin
contentValues for the content property
contrastValues for the contrast plugin
cursorValues for the cursor property
divideColorValues for the divideColor plugin
divideOpacityValues for the divideOpacity plugin
divideWidthValues for the divideWidth plugin
dropShadowValues for the dropShadow plugin
fillValues for the fill plugin
flexValues for the flex property
flexBasisValues for the flex-basis property
flexGrowValues for the flex-grow property
flexShrinkValues for the flex-shrink property
fontFamilyValues for the font-family property
fontSizeValues for the font-size property
fontWeightValues for the font-weight property
gapValues for the gap property
gradientColorStopsValues for the gradientColorStops plugin
gradientColorStopPositionsValues for the gradient-color-stop-positions property
grayscaleValues for the grayscale plugin
gridAutoColumnsValues for the grid-auto-columns property
gridAutoRowsValues for the grid-auto-rows property
gridColumnValues for the grid-column property
gridColumnEndValues for the grid-column-end property
gridColumnStartValues for the grid-column-start property
gridRowValues for the grid-row property
gridRowEndValues for the grid-row-end property
gridRowStartValues for the grid-row-start property
gridTemplateColumnsValues for the grid-template-columns property
gridTemplateRowsValues for the grid-template-rows property
heightValues for the height property
hueRotateValues for the hueRotate plugin
insetValues for the top, right, bottom, and left properties
invertValues for the invert plugin
keyframesKeyframe values used in the animation plugin
letterSpacingValues for the letter-spacing property
lineHeightValues for the line-height property
listStyleTypeValues for the list-style-type property
listStyleImageValues for the list-style-image property
marginValues for the margin property
lineClampValues for the line-clamp property
maxHeightValues for the max-height property
maxWidthValues for the max-width property
minHeightValues for the min-height property
minWidthValues for the min-width property
objectPositionValues for the object-position property
opacityValues for the opacity property
orderValues for the order property
outlineColorValues for the outline-color property
outlineOffsetValues for the outline-offset property
outlineWidthValues for the outline-width property
paddingValues for the padding property
placeholderColorValues for the placeholderColor plugin
placeholderOpacityValues for the placeholderOpacity plugin
ringColorValues for the ringColor plugin
ringOffsetColorValues for the ringOffsetColor plugin
ringOffsetWidthValues for the ringOffsetWidth plugin
ringOpacityValues for the ringOpacity plugin
ringWidthValues for the ringWidth plugin
rotateValues for the rotate plugin
saturateValues for the saturate plugin
scaleValues for the scale plugin
screensYour project's responsive breakpoints
scrollMarginValues for the scroll-margin property
scrollPaddingValues for the scroll-padding property
sepiaValues for the sepia plugin
skewValues for the skew plugin
spaceValues for the space plugin
spacingYour project's spacing scale
strokeValues for the stroke property
strokeWidthValues for the stroke-width property
supportsValues for the supports property
dataValues for the data property
textColorValues for the text-color property
textDecorationColorValues for the text-decoration-color property
textDecorationThicknessValues for the text-decoration-thickness property
textIndentValues for the text-indent property
textOpacityValues for the textOpacity plugin
textUnderlineOffsetValues for the text-underline-offset property
transformOriginValues for the transform-origin property
transitionDelayValues for the transition-delay property
transitionDurationValues for the transition-duration property
transitionPropertyValues for the transition-property property
transitionTimingFunctionValues for the transition-timing-function property
translateValues for the translate plugin
sizeValues for the size property
widthValues for the width property
willChangeValues for the will-change property
zIndexValues for the z-index property