Skip to content

标签化预设

这将为其他预设启用 标签化模式

源代码

安装

bash
pnpm add -D @unocss/preset-tagify
bash
yarn add -D @unocss/preset-tagify
bash
npm install -D @unocss/preset-tagify
bash
bun add -D @unocss/preset-tagify
uno.config.ts
ts
import presetTagify from '@unocss/preset-tagify'
import { defineConfig } from 'unocss'

export default defineConfig({
  presets: [
    presetTagify({ /* options */ }),
    // ...other presets
  ],
})

标签化模式

当你只需要为一个元素应用单一的 UnoCSS 规则时,这个预设就会派上用场。

html
<span class="text-red"> red text </span>
<div class="flex">flexbox</div>
I'm feeling <span class="i-line-md-emoji-grin"></span> today!

使用标签化模式,你可以将 CSS 样式嵌入到 HTML 标签中:

html
<text-red> red text </text-red>
<flex> flexbox </flex>
I'm feeling <i-line-md-emoji-grin /> today!

上述 HTML 的工作方式与你预期的完全一致。

带前缀

js
presetTagify({
  prefix: 'un-'
})
html
<!-- this will be matched -->
<un-flex> </un-flex>
<!-- this will not be matched -->
<flex> </flex>

额外属性

你可以将额外的属性注入到匹配的规则中:

js
presetTagify({
  // adds display: inline-block to matched icons
  extraProperties: matched => matched.startsWith('i-')
    ? { display: 'inline-block' }
    : { }
})
js
presetTagify({
  // extraProperties can also be a plain object
  extraProperties: { display: 'block' }
})

选项

prefix

  • 类型: string

用于标签化变体的前缀。

excludedTags

  • 类型: string[] | RegExp[]
  • 默认值: ['b', /^h\d+$/, 'table']

排除在处理之外的标签。

extraProperties

  • 类型: Record<string, string> | ((matched: string) => Partial<Record<string, string>>)

应用于匹配规则的额外 CSS 属性。

defaultExtractor

  • 类型: boolean
  • 默认值: true

启用默认提取器。