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-rules.md
1---2name: unocss-rules3description: Static and dynamic rules for generating CSS utilities in UnoCSS4---56# UnoCSS Rules78Rules define utility classes and the CSS they generate. UnoCSS has many built-in rules via presets and allows custom rules.910## Static Rules1112Simple mapping from class name to CSS properties:1314```ts15rules: [16['m-1', { margin: '0.25rem' }],17['font-bold', { 'font-weight': 700 }],18]19```2021Usage: `<div class="m-1">` generates `.m-1 { margin: 0.25rem; }`2223**Note:** Use CSS property syntax with hyphens (e.g., `font-weight` not `fontWeight`). Quote properties with hyphens.2425## Dynamic Rules2627Use RegExp matcher with function body for flexible utilities:2829```ts30rules: [31// Match m-1, m-2, m-100, etc.32[/^m-(\d+)$/, ([, d]) => ({ margin: `${d / 4}rem` })],3334// Access theme and context35[/^p-(\d+)$/, (match, ctx) => ({ padding: `${match[1] / 4}rem` })],36]37```3839The function receives:401. RegExp match result (destructure to get captured groups)412. Context object with `theme`, `symbols`, etc.4243## CSS Fallback Values4445Return 2D array for CSS property fallbacks (browser compatibility):4647```ts48rules: [49[/^h-(\d+)dvh$/, ([_, d]) => [50['height', `${d}vh`],51['height', `${d}dvh`],52]],53]54```5556Generates: `.h-100dvh { height: 100vh; height: 100dvh; }`5758## Special Symbols5960Control CSS output with symbols from `@unocss/core`:6162```ts63import { symbols } from '@unocss/core'6465rules: [66['grid', {67[symbols.parent]: '@supports (display: grid)',68display: 'grid',69}],70]71```7273### Available Symbols7475| Symbol | Description |76|--------|-------------|77| `symbols.parent` | Parent wrapper (e.g., `@supports`, `@media`) |78| `symbols.selector` | Function to modify the selector |79| `symbols.layer` | Set the UnoCSS layer |80| `symbols.variants` | Array of variant handlers |81| `symbols.shortcutsNoMerge` | Disable merging in shortcuts |82| `symbols.noMerge` | Disable rule merging |83| `symbols.sort` | Override sorting order |84| `symbols.body` | Full control of CSS body |8586## Multi-Selector Rules8788Use generator functions to yield multiple CSS rules:8990```ts91rules: [92[/^button-(.*)$/, function* ([, color], { symbols }) {93yield { background: color }94yield {95[symbols.selector]: selector => `${selector}:hover`,96background: `color-mix(in srgb, ${color} 90%, black)`97}98}],99]100```101102Generates both `.button-red { background: red; }` and `.button-red:hover { ... }`103104## Fully Controlled Rules105106Return a string for complete CSS control (advanced):107108```ts109import { defineConfig, toEscapedSelector as e } from 'unocss'110111rules: [112[/^custom-(.+)$/, ([, name], { rawSelector, theme }) => {113const selector = e(rawSelector)114return `115${selector} { font-size: ${theme.fontSize.sm}; }116${selector}::after { content: 'after'; }117@media (min-width: ${theme.breakpoints.sm}) {118${selector} { font-size: ${theme.fontSize.lg}; }119}120`121}],122]123```124125**Warning:** Fully controlled rules don't work with variants like `hover:`.126127## Symbols.body for Variant Support128129Use `symbols.body` to keep variant support with custom CSS:130131```ts132rules: [133['custom-red', {134[symbols.body]: `135font-size: 1rem;136&::after { content: 'after'; }137& > .bar { color: red; }138`,139[symbols.selector]: selector => `:is(${selector})`,140}]141]142```143144## Rule Ordering145146Later rules have higher priority. Dynamic rules output is sorted alphabetically within the group.147148## Rule Merging149150UnoCSS merges rules with identical CSS bodies:151152```html153<div class="m-2 hover:m2">154```155156Generates:157```css158.hover\:m2:hover, .m-2 { margin: 0.5rem; }159```160161Use `symbols.noMerge` to disable.162163<!--164Source references:165- https://unocss.dev/config/rules166-->167