UnoCSS 是一个即时的原子化 CSS 引擎,旨在灵活和可扩展。核心是不拘一格的,所有的 CSS 工具类都是通过预设提供的。再也不用为了取一个 classname 类名而烦恼了。
这里以 Vue3 + Vite 为例
pnpm add -D unocss
vite.config.js
中配置// vite.config.ts
import UnoCSS from 'unocss/vite'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [UnoCSS()]
})
uno.config.jts
配置文件:// uno.config.js
import { defineConfig } from 'unocss'
export default defineConfig({
// ...UnoCSS选项
})
main.js
中引入// main.js
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import App from '@/App.vue'
import router from '@/router'
import 'unocss/themes/solid.css'
// ..
createApp(App)
.use(createPinia())
.use(router)
.mount('#app')
常用值:
w-0
:width: 0
w-1
:width: 0.25rem
h-100px
: height: 100px
h--10px
: height: -10px
h-full
: height: 100%
min-w-screen
: min-width: 100vw
<div class="min-h-100px w-50vw">div>
<div style="min-height: 100px; width: 50vw;">div>
<div class="bg-#f00">div>
<div style="background-color: #f00">div>
<div class="text-center font-700 text-#f00 text-20px">div>
<div style="text-align: center; font-weight: 700; color: #f00; font-size: 20px;">div>
常用值:
rounded-none
:border-radius: 0;
rounded-l
:border-top-left-radius: 0.25rem; border-bottom-left-radius: 0.25rem;
rounded-full
: border-radius: 9999px;
border-0
: border-width: 0;
b-0
: border-width: 0;
b-1
: border-width: 1px;
b-b-1
: border-bottom-width: 1px;
b-solid
: border-style: solid;
b-#f00
: border-color: #f00;
b-rd
: border-radius: 0.25rem;
b-rounded
: border-radius: 0.25rem;
b-rd-6px
: border-radius: 6px;
b-rd-full
: border-radius: 9999px;
b-rd-50%
: border-radius: 50%;
<div class="b-1 b-solid b-#f00 b-rd-6px">div>
<div style="border: 1px solid #f00; border-radius: 6px;">div>
常用值:
m-0
:margin: 0;
mx-0
: margin-left: 0; margin-right: 0;
py-10px
: padding-top: 10px; padding-bottom: 10px;
p-10px
: padding: 10px;
p--20px
: padding: -20px;
<div class="m-10px p-10px">div>
<div style="margin: 10px; padding: 10px;">div>
常用值:
flex
: display: flex;
flex-wrap
: flex-wrap: wrap;
flex-row
: flex-direction: row;
flex-col
: flex-direction: column;
flex-justify-between
: justify-content: space-between;
flex-justify-center
: justify-content: center;
flex-items-center
: align-items: center;
<div class="flex flex-row justify-between items-center">div>
<div style="display: flex; flex-direction: row; justify-content: space-between; align-items: center;">div>
常用值:
overflow-hidden
: overflow: hidden;
overflow-auto
: overflow: auto;
overflow-scroll
: overflow: scroll;
overflow-x-auto
: overflow-x: auto;
<div class="min-h-100px max-h-60vh w-100% overflow-y-scroll">div>
<div style="min-height: 100px; max-height: 60vh; width: 100%; overflow-y: scroll;">div>
常用值:
absolute
: position: absolute;
position-absolute
: position: absolute;
position-fixed
: position: fixed;
position-relative
: position: relative;
left-0
: left: 0;
top-5px
: top: 5px;
z-0
: z-index: 0;
z-100
: z-index: 100;
<div class="absolute left-0 top-5px z-100">div>
<div style="position: absolute; left: 0; top: 5px; z-index: 100;">div>
常用值:
hover-block
: display: block;
hover-text-pink
: color: pink;
<div class="last-hover-text-pink">div>
<style>
.last-hover-text-pink:hover:last-child {
--un-text-opacity: 1;
color: rgba(244, 114, 182, var(--un-text-opacity));
}
style>
常用值:
block
: display: block;
inline-block
: display: inline-block;
flex
: display: flex;
hidden
: display: none;
<div class="flex">div>
<div style="display: flex;">div>
<div class="!-text-20px">div>
<div style="font-size: 20px !important;">div>
UnoCSS 与 TailwindCSS 类似,但是它更加轻量级,使用体验更好,而且它的中文文档也很完善,可以说是 TailwindCSS 的一个很好的替代品。
尤其是 UnoCSS 的自定义 classname 类名功能,不用 []
包裹,更加直观,例如:
<div class="w-[100px] h-[20vh]">div>
<div class="w-100px h-20vh">div>
参考文档
欢迎访问:天问博客