Skip to content

主题

为你的项目定制默认主题。

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

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

我们提供了一个合理的默认主题,具有非常丰富的值集,让您可以开始使用,但不要害怕更改它或扩展它;鼓励您根据设计目标自定义它。

主题结构

theme 对象包含 screenscolorsspacing 的键,以及每个可定制核心插件的键。

请参阅主题配置参考默认主题,获取主题选项的完整列表。

屏幕

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

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

To learn more, see the breakpoint customization documentation. 要了解更多信息,请参阅断点自定义文档

颜色

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

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

      // ...
    }
  }
}

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

了解更多,请参阅颜色自定义文档

间距

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

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

默认情况下,这些值会被 padding , margin , width , height , maxHeight , flex-basis , gap , inset , space , translate , scrollMargin , scrollPadding , 和 textIndent 核心插件继承。

了解更多,请参阅间距自定义文档

核心插件

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

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

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

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

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

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 中的常见约定,并受所有核心插件支持。

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

要获取可用主题属性及其默认值的完整参考,请参阅默认主题配置

自定义默认主题

在开箱即用的情况下,您的项目将自动继承默认主题配置的值。如果您想自定义默认主题,根据您的目标,您有几种不同的选择。

扩展默认主题

如果您想保留主题选项的默认值,同时还要添加新值,请在配置文件中的 theme.extend 键下添加您的扩展。该键下的值将与现有的 theme 值合并,并自动作为新类可用,您可以使用。

例如,在这里我们扩展 fontFamily 属性以添加 font-display 类,该类可以更改元素上使用的字体:

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

将此添加到您的主题后,您可以像使用任何其他 font-{family} 实用程序一样使用它:

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

在某些情况下,属性映射到可以放置在实用程序前面以有条件地应用其样式的变体。例如,要添加一个与现有响应式屏幕完全相同的 3xl 屏幕大小,请在 screens 键下添加一个属性:

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

通过这个新增功能,除了现有的响应式变体如 smmdlg 等,还提供了一个新的 3xl 屏幕尺寸。您可以通过在实用类之前放置它来使用这个新变体。

html
<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 部分下添加您的覆盖。

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 对该键的默认配置,因此在上面的示例中,不会生成任何默认的不透明度实用程序。

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

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

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 实用程序:

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

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

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

✘ 你不能使用函数来处理单个数值

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

✓ 使用函数来设置顶层主题键

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

引用默认主题

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

这是有用的一个例子,如果你想要将字体系列添加到Tailwind默认的字体堆栈之一:

js
const defaultTheme = require('tailwindcss/defaultTheme')

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

禁用整个核心插件

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

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

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

✓ 在您的 corePlugins 配置中禁用插件

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

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

配置参考

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

所有这些键也都可以在 theme.extend 键下使用,以便扩展默认主题

KeyDescription
accentColoraccent-color 属性的值
animationanimation 属性的值
ariaaria 属性的值
aspectRatioaspect-ratio 属性的值
backdropBlurbackdropBlur 插件的值
backdropBrightnessbackdropBrightness 插件的值
backdropContrastbackdropContrast 插件的值
backdropGrayscalebackdropGrayscale 插件的值
backdropHueRotatebackdropHueRotate 插件的值
backdropInvertbackdropInvert 插件的值
backdropOpacitybackdropOpacity 插件的值
backdropSaturatebackdropSaturate 插件的值
backdropSepiabackdropSepia 插件的值
backgroundColorbackground-color 属性的值
backgroundImagebackground-image 属性的值
backgroundOpacitybackground-opacity 属性的值
backgroundPositionbackground-position 属性的值
backgroundSizebackground-size 属性的值
blurblur 插件的值
borderColorborder-color 属性的值
borderOpacityborderOpacity 插件的值
borderRadiusborder-radius 属性的值
borderSpacingborder-spacing 属性的值
borderWidthborderWidth 插件的值
boxShadowbox-shadow 属性的值
boxShadowColorboxShadowColor 插件的值
brightnessbrightness 插件的值
caretColorcaret-color 属性的值
colors项目的配色方案
columnscolumns 属性的值
containercontainer 插件的配置
contentcontent 属性的值
contrastcontrast 插件的值
cursorcursor 属性的值
divideColordivideColor 插件的值
divideOpacitydivideOpacity 插件的值
divideWidthdivideWidth 插件的值
dropShadowdropShadow 插件的值
fillfill 插件的值
flexflex 属性的值
flexBasisflex-basis 属性的值
flexGrowflex-grow 属性的值
flexShrinkflex-shrink 属性的值
fontFamilyfont-family 属性的值
fontSizefont-size 属性的值
fontWeightfont-weight 属性的值
gapgap 属性的值
gradientColorStopsgradientColorStops 插件的值
gradientColorStopPositionsgradient-color-stop-positions 属性的值
grayscalegrayscale 插件的值
gridAutoColumnsgrid-auto-columns 属性的值
gridAutoRowsgrid-auto-rows 属性的值
gridColumngrid-column 属性的值
gridColumnEndgrid-column-end 属性的值
gridColumnStartgrid-column-start 属性的值
gridRowgrid-row 属性的值
gridRowEndgrid-row-end 属性的值
gridRowStartgrid-row-start 属性的值
gridTemplateColumnsgrid-template-columns 属性的值
gridTemplateRowsgrid-template-rows 属性的值
heightheight 属性的值
hueRotatehueRotate 插件的值
insettop, right, bottom, 和 left 属性的值
invertinvert 插件的值
keyframesanimation 插件中使用的关键帧值
letterSpacingletter-spacing 属性的值
lineHeightline-height 属性的值
listStyleTypelist-style-type 属性的值
listStyleImagelist-style-image 属性的值
marginmargin 属性的值
lineClampline-clamp 属性的值
maxHeightmax-height 属性的值
maxWidthmax-width 属性的值
minHeightmin-height 属性的值
minWidthmin-width 属性的值
objectPositionobject-position 属性的值
opacityopacity 属性的值
orderorder 属性的值
outlineColoroutline-color 属性的值
outlineOffsetoutline-offset 属性的值
outlineWidthoutline-width 属性的值
paddingpadding 属性的值
placeholderColorplaceholderColor 插件的值
placeholderOpacityplaceholderOpacity 插件的值
ringColorringColor 插件的值
ringOffsetColorringOffsetColor 插件的值
ringOffsetWidthringOffsetWidth 插件的值
ringOpacityringOpacity 插件的值
ringWidthringWidth 插件的值
rotaterotate 插件的值
saturatesaturate 插件的值
scalescale 插件的值
screens项目的响应式断点
scrollMarginscroll-margin 属性的值
scrollPaddingscroll-padding 属性的值
sepiasepia 插件的值
skewskew 插件的值
spacespace 插件的值
spacing项目的间距比例
strokestroke 属性的值
strokeWidthstroke-width 属性的值
supportssupports 属性的值
datadata 属性的值
textColortext-color 属性的值
textDecorationColortext-decoration-color 属性的值
textDecorationThicknesstext-decoration-thickness 属性的值
textIndenttext-indent 属性的值
textOpacitytextOpacity 插件的值
textUnderlineOffsettext-underline-offset 属性的值
transformOrigintransform-origin 属性的值
transitionDelaytransition-delay 属性的值
transitionDurationtransition-duration 属性的值
transitionPropertytransition-属性的值 属性的值
transitionTimingFunctiontransition-timing-function 属性的值
translatetranslate 插件的值
sizesize 属性的值
widthwidth 属性的值
willChangewill-change 属性的值
zIndexz-index 属性的值

Released under the MIT License.