Skip to content

Theme

UnoCSS还支持您可能在Tailwind CSS/Windi CSS中熟悉的主题化系统。在用户级别,您可以在配置中指定theme属性,它将被深度合并到默认主题。

比如

ts
theme: {
  // ...
  colors: {
    'veryCool': '#0000ff', // class="text-very-cool"
    'brand': {
      'primary': 'hsl(var(--hue, 217) 78% 51%)', //class="bg-brand-primary"
    },
  },
}

TIP

在解析过程中,theme将始终存在于context中。

使用 rules

要在规则中使用主题,请执行以下操作:

ts
rules: [
  [/^text-(.*)$/, ([, c], { theme }) => {
    if (theme.colors[c])
      return { color: theme.colors[c] }
  }],
]

使用 variants

要在规则中使用主题,请执行以下操作:

ts
variants: [
  {
    name: 'variant-name',
    match(matcher, { theme }) {
      // ...
    },
  },
]

使用 shortcuts

要在动态快捷方式中使用主题,请执行以下操作:

ts
shortcuts: [
  [/^badge-(.*)$/, ([, c], { theme }) => {
    if (Object.keys(theme.colors).includes(c))
      return `bg-${c}4:10 text-${c}5 rounded`
  }],
]

断点breakpoints

WARNING

一个例外是UnoCSS将breakpoints的完全控制权交给了用户。当提供自定义breakpoints时,默认值将被覆盖,而不是合并。

通过以下示例,您将只能使用 sm: and md:断点变体:

ts
theme: {
  // ...
  breakpoints: {
    sm: '320px',
    md: '640px',
  },
}

INFO

verticalBreakpoints is same as breakpoints but for vertical layout.

此外,我们将按大小(相同单位)对屏幕点进行排序。对于不同单位的屏点,为了避免错误,请在配置中使用统一的单位。

ts
theme: {
  // ...
  breakpoints: {
    sm: '320px',
    // Because uno does not support comparison sorting of different unit sizes, please convert to the same unit.
    // md: '40rem',
    md: `${40 * 16}px`,
    lg: '960px',
  },
}

Released under the MIT License.