安装
- pnpm安装
pnpm add -D unocss
- 配置
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import UnoCSS from 'unocss/vite'
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
AutoImport({
imports: ['vue'],
dts: "src/auto-import.d.ts"
}),
UnoCSS(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
})
- 在根目录新建unocss的配置文件
uno.config.ts
import { defineConfig } from 'unocss'
export default defineConfig({
shortcuts: [['center', 'flex justify-center items-center']],
})
main.js
里引入
import 'virtual:uno.css'
- 完成!!
动态生成类
1. 动态生成任意CSS属性
<element class="[property]-[value]">
工具类 | CSS解释 |
---|
w-[100px] | width: 100px |
h-[100px] | height: 100px |
bg-[#3498db] | background-color: #3498db |
text-[20px] | font-size: 20px |
border-[2px] | border-width: 2px |
border-[#ff0000] | border-color: #ff0000 |
2. 动态生成伪类和伪元素
<element class="[pseudo-class]:[property]-[value]">
工具类 | CSS解释 |
---|
hover:bg-[#ff0000] | 鼠标悬停时动态生成 background-color: #ff0000 |
focus:border-[2px] | 聚焦时动态生成 border-width: 2px |
3. 动态生成媒体查询
<element class="[media-query]:[property]-[value]">
工具类 | CSS解释 |
---|
md:text-[20px] | 在中等屏幕尺寸下动态生成 font-size: 20px |
lg:text-[24px] | 在大屏幕尺寸下动态生成 font-size: 24px |
4. 动态生成自定义属性
<element class="[property]-[var:custom-property]">
工具类 | CSS解释 |
---|
bg-[var:primary-color] | background-color: var(--primary-color) |
text-[var:text-size] | font-size: var(--text-size) |
5. 动态生成复杂属性
<div class="shadow-[0_4px_6px_rgba(0,0,0,0.1)] transform-[rotate(45deg)]">
Complex Properties
</div>
工具类 | CSS解释 |
---|
shadow-[0_4px_6px_rgba(0,0,0,0.1)] | box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1) |
transform-[rotate(45deg)] | transform: rotate(45deg) |
6. 动态生成动画
<element class="animate-[animation-name]">
<div class="animate-[spin_2s_linear_infinite]">
Spinning Element
</div>
工具类 | CSS解释 |
---|
animate-[spin_2s_linear_infinite] | animation: spin 2s linear infinite |
7. 动态生成渐变
<div class="bg-[linear-gradient(to_right,#3498db,#9b59b6)]">
Gradient Background
</div>
工具类 | CSS解释 |
---|
bg-[linear-gradient(to_right,#3498db,#9b59b6)] | background: linear-gradient(to right, #3498db, #9b59b6) |
8. 动态生成网格和 Flexbox 布局
<div class="grid grid-cols-[repeat(3,1fr)] gap-[10px]">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
工具类 | CSS解释 |
---|
grid-cols-[repeat(3,1fr)] | grid-template-columns: repeat(3, 1fr) |
gap-[10px] | gap: 10px |
9. 动态生成过渡效果
<button class="transition-[all_0.3s_ease-in-out] hover:bg-[#ff0000]">
Hover Me
</button>
工具类 | CSS解释 |
---|
transition-[all_0.3s_ease-in-out] | transition: all 0.3s ease-in-out |
10. 动态生成滤镜
<div class="filter-[blur(5px)]">
Blurred Element
</div>
工具类 | CSS解释 |
---|
filter-[blur(5px)] | filter: blur(5px) |
工具类分类
1. 布局(Layout)
工具类 | CSS 解释 |
---|
block | display: block |
inline-block | display: inline-block |
inline | display: inline |
flex | display: flex |
inline-flex | display: inline-flex |
grid | display: grid |
inline-grid | display: inline-grid |
hidden | display: none |
visible | visibility: visible |
overflow-auto | overflow: auto |
overflow-hidden | overflow: hidden |
overflow-visible | overflow: visible |
overflow-scroll | overflow: scroll |
2. 间距(Spacing)
工具类 | CSS 解释 |
---|
m-0 | margin: 0 |
m-1 | margin: 0.25rem |
mx-auto | margin-left: auto; margin-right: auto |
my-2 | margin-top: 0.5rem; margin-bottom: 0.5rem |
p-0 | padding: 0 |
p-1 | padding: 0.25rem |
px-2 | padding-left: 0.5rem; padding-right: 0.5rem |
3. 颜色(Colors)
工具类 | CSS 解释 |
---|
bg-white | background-color: white |
bg-black | background-color: black |
bg-primary | background-color: #3498db |
text-white | color: white |
text-black | color: black |
text-primary | color: #3498db |
border-white | border-color: white |
border-black | border-color: black |
border-primary | border-color: #3498db |
4. 字体(Typography)
工具类 | CSS 解释 |
---|
text-sm | font-size: 0.875rem |
text-base | font-size: 1rem |
text-lg | font-size: 1.125rem |
text-xl | font-size: 1.25rem |
font-bold | font-weight: 700 |
font-normal | font-weight: 400 |
italic | font-style: italic |
text-center | text-align: center |
text-left | text-align: left |
text-right | text-align: right |
uppercase | text-transform: uppercase |
lowercase | text-transform: lowercase |
capitalize | text-transform: capitalize |
text-xs | 0.75rem 12px 超小字体 |
text-sm | 0.875rem 14px 小字体 |
text-base | 1rem 16px 默认字体大小 |
text-lg | 1.125rem 18px 大字体 |
text-xl | 1.25rem 20px 超大字体 |
text-2xl | 1.5rem 24px 两倍超大字体 |
字间距
5. 边框(Borders)
工具类 | CSS 解释 |
---|
border | border-width: 1px; border-style: solid |
border-2 | border-width: 2px |
border-0 | border-width: 0 |
rounded | border-radius: 0.25rem |
rounded-lg | border-radius: 0.5rem |
rounded-full | border-radius: 9999px |
border-gray-300 | border-color: #d1d5db |
border-primary | border-color: #3498db |
6. 定位(Positioning)
工具类 | CSS 解释 |
---|
static | position: static |
fixed | position: fixed |
absolute | position: absolute |
relative | position: relative |
sticky | position: sticky |
top-0 | top: 0 |
right-0 | right: 0 |
bottom-0 | bottom: 0 |
left-0 | left: 0 |
inset-0 | top: 0; right: 0; bottom: 0; left: 0 |
z-0 | z-index: 0 |
z-10 | z-index: 10 |
z-50 | z-index: 50 |
top-1/2 | 距离顶部 50% |
left-1/2 | 距离左侧 50% |
7. 背景(Backgrounds)
工具类 | CSS 解释 |
---|
bg-cover | background-size: cover |
bg-contain | background-size: contain |
bg-center | background-position: center |
bg-no-repeat | background-repeat: no-repeat |
bg-gradient-to-r | background-image: linear-gradient(to right, ...) |
8. 过渡与动画(Transitions & Animations)
工具类 | CSS 解释 |
---|
transition | transition: all 0.2s ease-in-out |
transition-colors | transition: background-color 0.2s ease-in-out |
animate-spin | animation: spin 2s linear infinite |
transform-[translate(-50%,-50%)] | transform: translate(-50%, -50%) |
translate-x--1/2 | transform: translateX(-50%) |
translate-y--1/2 | transform: translateY(-50%) |
9. 滤镜(Filters)
工具类 | CSS 解释 |
---|
filter | filter: ... |
blur-sm | filter: blur(4px) |
blur-lg | filter: blur(8px) |
grayscale | filter: grayscale(100%) |
10. 网格(Grid)
工具类 | CSS 解释 |
---|
grid | display: grid |
grid-cols-1 | grid-template-columns: repeat(1, minmax(0, 1fr)) |
grid-cols-2 | grid-template-columns: repeat(2, minmax(0, 1fr)) |
gap-2 | gap: 0.5rem |
gap-4 | gap: 1rem |
11. Flexbox
工具类 | CSS 解释 |
---|
flex | display: flex |
flex-row | flex-direction: row |
flex-col | flex-direction: column |
justify-start | justify-content: flex-start |
justify-center | justify-content: center |
justify-end | justify-content: flex-end |
items-start | align-items: flex-start |
items-center | align-items: center |
items-end | align-items: flex-end |
12. 其他(Others)
工具类 | CSS 解释 |
---|
cursor-pointer | cursor: pointer |
opacity-50 | opacity: 0.5 |
shadow | box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1) |
shadow-lg | box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1) |