Skip to content

什么是 UnoCSS?

¥What is UnoCSS?

UnoCSS 是即时原子 CSS 引擎,其设计灵活且可扩展。核心是不固定的,所有 CSS 工具都是通过预设提供的。

¥UnoCSS is the instant atomic CSS engine, that is designed to be flexible and extensible. The core is un-opinionated and all the CSS utilities are provided via presets.

例如,你可以通过在本地 配置文件 中提供规则来定义自定义 CSS 工具。

¥For example, you could define your custom CSS utilities, by providing rules in your local config file.

ts
// uno.config.ts
import { defineConfig } from 'unocss'

export default defineConfig({
  rules: [
    ['m-1', { margin: '1px' }],
  ],
})

这将向你的项目添加一个新的 CSS 工具 m-1。由于 UnoCSS 是按需的,因此除非你在代码库中使用它,否则它不会执行任何操作。假设我们有一个这样的组件:

¥This will add a new CSS utility m-1 to your project. Since UnoCSS is on-demand, it won't do anything until you use it in your codebase. So say we have a component like this:

html
<div class="m-1">Hello</div>

将检测到 m-1 并生成以下 CSS:

¥m-1 will be detected and the following CSS will be generated:

css
.m-1 { margin: 1px; }

为了使其更加灵活,你可以通过将规则的第一个参数(我们称之为匹配器)更改为 RegExp,将主体更改为函数,使规则动态化,例如:

¥To make it more flexible, you can make your rule dynamic by changing the first argument on the rule (we call it matcher) to a RegExp, and the body to a function, for example:

diff
// uno.config.ts
export default defineConfig({
  rules: [
-    ['m-1', { margin: '1px' }],
+    [/^m-([\.\d]+)$/, ([_, num]) => ({ margin: `${num}px` })],
  ],
})

通过这样做,现在你可以拥有任意外边距工具,例如 m-1m-100m-52.43。再说一次,UnoCSS 仅在你使用它们时生成它们。

¥By doing this, now you can have arbitrary margin utilities, like m-1, m-100 or m-52.43. And again, UnoCSS only generates them whenever you use them.

html
<div class="m-1">Hello</div>
<div class="m-7.5">World</div>
css
.m-1 { margin: 1px; }
.m-7.5 { margin: 7.5px; }

预设

¥Presets

制定一些规则后,你可以将它们提取到预设中,并与其他人共享。例如,你可以为公司的设计系统创建预设,并与你的团队共享。

¥Once you made a few rules, you can extract them into a preset, and share it with others. For example, you can create a preset for your company's design system, and share it with your team.

ts
// my-preset.ts
import { Preset } from 'unocss'

export const myPreset: Preset = {
  name: 'my-preset',
  rules: [
    [/^m-([\.\d]+)$/, ([_, num]) => ({ margin: `${num}px` })],
    [/^p-([\.\d]+)$/, ([_, num]) => ({ padding: `${num}px` })],
  ],
  variants: [/* ... */],
  shortcuts: [/* ... */],
  // ...
}
ts
// uno.config.ts
import { defineConfig } from 'unocss'
import { myPreset } from './my-preset'

export default defineConfig({
  presets: [
    myPreset, // your own preset
  ],
})

所以同样,我们提供了一些 官方预设 供你立即开始使用,你还可以找到许多有趣的 社区预设

¥So similarly, we provided a few official presets for you to start using right away, and you can also find many interesting community presets.

练习

¥Play

你可以在浏览器中的 在线运行 中尝试 UnoCSS。或者从 交互式文档 中的默认预设查找工具。

¥You can try UnoCSS in your browser, in the Playground. Or look up utilities from the default presets in the Interactive Docs.

集成

¥Integrations

UnoCSS 集成了各种框架/工具:

¥UnoCSS comes with integrations for various frameworks / tools:

示例

¥Examples

所有示例的源代码都可以在 /examples 目录中找到。

¥Source code for all the examples can be found in the /examples directory.