docs/config/theme.md
UnoCSS also supports the theming system that you might be familiar with in Tailwind CSS / Windi CSS. At the user level, you can specify the theme property in your config, and it will be deep-merged to the default theme.
theme: {
// ...
colors: {
veryCool: '#0000ff', // class="text-very-cool"
brand: {
primary: 'hsl(var(--hue, 217) 78% 51%)', //class="bg-brand-primary"
DEFAULT: '#942192' //class="bg-brand"
},
},
}
::: tip
During the parsing process, theme will always exist in context.
:::
rulesTo consume the theme in rules:
rules: [
[/^text-(.*)$/, ([, c], { theme }) => {
if (theme.colors[c])
return { color: theme.colors[c] }
}],
]
variantsTo consume the theme in variants:
variants: [
{
name: 'variant-name',
match(matcher, { theme }) {
// ...
},
},
]
shortcutsTo consume the theme in dynamic shortcuts:
shortcuts: [
[/^badge-(.*)$/, ([, c], { theme }) => {
if (Object.keys(theme.colors).includes(c))
return `bg-${c}4:10 text-${c}5 rounded`
}],
]
::: warning
When a custom breakpoints object is provided the default will be overridden instead of merging.
:::
With the following example, you will be able to only use the sm: and md: breakpoint variants:
theme: {
// ...
breakpoints: {
sm: '320px',
md: '640px',
},
}
::: tip
In presetWind4 the key was changed changed to breakpoint.
For the presetWind4 theme docs see https://unocss.dev/presets/wind4#theme.
:::
If you want to inherit the original theme breakpoints, you can use the extendTheme:
extendTheme: (theme) => {
return {
...theme,
breakpoints: {
...theme.breakpoints,
sm: '320px',
md: '640px',
},
}
}
::: info
verticalBreakpoints is same as breakpoints but for vertical layout.
:::
In addition we will sort screen points by size (same unit). For screen points in different units, in order to avoid errors, please use unified units in the configuration.
<!--eslint-skip-->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',
},
}
ExtendTheme allows you to edit the deeply merged theme to get the complete theme object.
Custom functions mutate the theme object.
extendTheme: (theme) => {
theme.colors.veryCool = '#0000ff' // class="text-very-cool"
theme.colors.brand = {
primary: 'hsl(var(--hue, 217) 78% 51%)', // class="bg-brand-primary"
}
}
It's also possible to return a new theme object to completely replace the original one.
extendTheme: (theme) => {
return {
...theme,
colors: {
...theme.colors,
veryCool: '#0000ff', // class="text-very-cool"
brand: {
primary: 'hsl(var(--hue, 217) 78% 51%)', // class="bg-brand-primary"
},
},
}
}