# 样式篇 本篇主要介绍 `UnoCSS` 的使用,以及如何与 `设计稿尺寸` 对应。 ## UnoCSS [UnoCSS](https://unocss.dev/) 是按需使用的原子 CSS 引擎,提供了良好的样式支持。 ![alt text](./assets/4-1.png) 在 VSCode 中还可以预览, ![alt text](./assets/4-2.png) ![alt text](./assets/4-3.png) > 如果原子化 `UnoCSS` 没有预览效果,请安装 `VSCode` 插件 `antfu.unocss`。 如果不记得原子类,可以查 `UnoCSS 的原子类`,[UnoCSS Interactive](https://unocss.dev/interactive/),如下图 ![alt text](./assets/4-4.png) 也可以查看 `tailwindcss` 的原子类,更加清晰明了,[链接 - tailwindcss](https://tailwindcss.com/),如下图: ![alt text](./assets/4-5.png) ## 常用的原子类 - 宽高内外边距: `w-2`, `h-4`, `px-6`, `mt-8`等 - 前景色背景色:`text-green-400`, `bg-green-500` - border: `border-2`, `border-solid`, `border-green-600`, `b-r-2` (注意 `border` = `border-1`,就是说边框 `1px` 时,一般简写为 `border` ) - border-radius: `rounded-full`, `rounded-6`, `rounded-sm` (不是 `br-10`, 也不是 `b-r-10`) - line-height: `leading-10` (不是 `l-10`, 也不是 `lh-10`) - hover: `hover:text-green-200`, `hover:bg-green-300`, `hover:border-dashed` - flex: `flex`, `items-center`, `justify-center`, `flex-1` ## `UnoCSS` 配置 下面内容选读: :::details `unocss.config.ts` 文件内容如下: ```ts // uno.config.ts import { type Preset, defineConfig, presetUno, presetAttributify, presetIcons, transformerDirectives, transformerVariantGroup, } from 'unocss' import { presetApplet, presetRemRpx, transformerAttributify } from 'unocss-applet' // @see https://unocss.dev/presets/legacy-compat import { presetLegacyCompat } from '@unocss/preset-legacy-compat' const isMp = process.env?.UNI_PLATFORM?.startsWith('mp') ?? false const presets: Preset[] = [] if (isMp) { // 使用小程序预设 presets.push(presetApplet(), presetRemRpx()) } else { presets.push( // 非小程序用官方预设 presetUno(), // 支持css class属性化 presetAttributify(), ) } export default defineConfig({ presets: [ ...presets, // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `