Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Configure and use UnoCSS atomic CSS engine with presets like Wind (Tailwind-compatible), Icons, and Attributify.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/core-variants.md
1---2name: unocss-variants3description: Apply variations like hover:, dark:, responsive to rules4---56# UnoCSS Variants78Variants apply modifications to utility rules, like `hover:`, `dark:`, or responsive prefixes.910## How Variants Work1112When matching `hover:m-2`:13141. `hover:m-2` is extracted from source152. Sent to all variants for matching163. `hover:` variant matches and returns `m-2`174. Result `m-2` continues to next variants185. Finally matches the rule `.m-2 { margin: 0.5rem; }`196. Variant transformation applied: `.hover\:m-2:hover { margin: 0.5rem; }`2021## Creating Custom Variants2223```ts24variants: [25// hover: variant26(matcher) => {27if (!matcher.startsWith('hover:'))28return matcher29return {30// Remove prefix, pass to next variants/rules31matcher: matcher.slice(6),32// Modify the selector33selector: s => `${s}:hover`,34}35},36],37rules: [38[/^m-(\d)$/, ([, d]) => ({ margin: `${d / 4}rem` })],39]40```4142## Variant Return Object4344- `matcher` - The processed class name to pass forward45- `selector` - Function to customize the CSS selector46- `parent` - Wrapper like `@media`, `@supports`47- `layer` - Specify output layer48- `sort` - Control ordering4950## Built-in Variants (preset-wind3)5152### Pseudo-classes53- `hover:`, `focus:`, `active:`, `visited:`54- `first:`, `last:`, `odd:`, `even:`55- `disabled:`, `checked:`, `required:`56- `focus-within:`, `focus-visible:`5758### Pseudo-elements59- `before:`, `after:`60- `placeholder:`, `selection:`61- `marker:`, `file:`6263### Responsive64- `sm:`, `md:`, `lg:`, `xl:`, `2xl:`65- `lt-sm:` (less than sm)66- `at-lg:` (at lg only)6768### Dark Mode69- `dark:` - Class-based dark mode (default)70- `@dark:` - Media query dark mode7172### Group/Peer73- `group-hover:`, `group-focus:`74- `peer-checked:`, `peer-focus:`7576### Container Queries77- `@container`, `@sm:`, `@md:`787980- `print:`8182### Supports83- `supports-[display:grid]:`8485### Aria86- `aria-checked:`, `aria-disabled:`8788### Data Attributes89- `data-[state=open]:`9091## Dark Mode Configuration9293### Class-based (default)94```ts95presetWind3({96dark: 'class'97})98```99100```html101<div class="dark:bg-gray-800">102```103104Generates: `.dark .dark\:bg-gray-800 { ... }`105106### Media Query107```ts108presetWind3({109dark: 'media'110})111```112113Generates: `@media (prefers-color-scheme: dark) { ... }`114115### Opt-in Media Query116Use `@dark:` regardless of config:117118```html119<div class="@dark:bg-gray-800">120```121122### Custom Selectors123```ts124presetWind3({125dark: {126light: '.light-mode',127dark: '.dark-mode',128}129})130```131132## CSS @layer Variant133134Native CSS `@layer` support:135136```html137<div class="layer-foo:p-4" />138```139140Generates:141```css142@layer foo {143.layer-foo\:p-4 { padding: 1rem; }144}145```146147## Breakpoint Differences from Windi CSS148149| Windi CSS | UnoCSS |150|-----------|--------|151| `<sm:p-1` | `lt-sm:p-1` |152| `@lg:p-1` | `at-lg:p-1` |153| `>xl:p-1` | `xl:p-1` |154155## Media Hover (Experimental)156157Addresses sticky hover on touch devices:158159```html160<div class="@hover-text-red">161```162163Generates:164```css165@media (hover: hover) and (pointer: fine) {166.\@hover-text-red:hover { color: rgb(248 113 113); }167}168```169170<!--171Source references:172- https://unocss.dev/config/variants173- https://unocss.dev/presets/wind3174- https://unocss.dev/presets/mini175-->176