PostCSS 插件
適用於 UnoCSS 的 PostCSS 插件。支援 @apply
、@screen
和 theme()
指令。
警告
此套件目前處於實驗階段。它不遵循語義化版本,並且可能在修補版本中引入重大變更。
安裝
bash
pnpm add -D unocss @unocss/postcss
bash
yarn add -D unocss @unocss/postcss
bash
npm install -D unocss @unocss/postcss
ts
import UnoCSS from '@unocss/postcss'
export default {
plugins: [
UnoCSS(),
],
}
ts
import { defineConfig, presetUno } from 'unocss'
export default defineConfig({
content: {
filesystem: [
'**/*.{html,js,ts,jsx,tsx,vue,svelte,astro}',
],
},
presets: [
presetUno(),
],
})
css
@unocss;
用法
@unocss
@unocss
at-rule 是一個佔位符。它將被生成的 CSS 取代。
您也可以單獨注入每一層
css
@unocss preflights;
@unocss default;
/*
Fallback layer. It's always recommended to include.
Only unused layers will be injected here.
*/
@unocss;
如果您想包含所有層級,無論它們先前是否已包含,您可以使用 @unocss all
。如果您想在多個檔案中包含生成的 CSS,這會很有用。
css
@unocss all;
@apply
css
.custom-div {
@apply text-center my-0 font-medium;
}
將會轉換成
css
.custom-div {
margin-top: 0rem;
margin-bottom: 0rem;
text-align: center;
font-weight: 500;
}
@screen
@screen
指令允許您建立媒體查詢,透過名稱引用您的斷點,名稱來自 theme.breakpoints
。
css
.grid {
@apply grid grid-cols-2;
}
@screen xs {
.grid {
@apply grid-cols-1;
}
}
@screen sm {
.grid {
@apply grid-cols-3;
}
}
/* ... */
...
將會轉換成
css
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) {
.grid {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
}
@media (min-width: 640px) {
.grid {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
/* ... */
斷點變體支援
@screen
也支援 lt
、at
變體
@screen lt
css
.grid {
@apply grid grid-cols-2;
}
@screen lt-xs {
.grid {
@apply grid-cols-1;
}
}
@screen lt-sm {
.grid {
@apply grid-cols-3;
}
}
/* ... */
將會轉換成
css
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (max-width: 319.9px) {
.grid {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
}
@media (max-width: 639.9px) {
.grid {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
/* ... */
@screen at
css
.grid {
@apply grid grid-cols-2;
}
@screen at-xs {
.grid {
@apply grid-cols-1;
}
}
@screen at-xl {
.grid {
@apply grid-cols-3;
}
}
@screen at-xxl {
.grid {
@apply grid-cols-4;
}
}
/* ... */
將會轉換成
css
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
}
@media (min-width: 320px) and (max-width: 639.9px) {
.grid {
grid-template-columns: repeat(1, minmax(0, 1fr));
}
}
@media (min-width: 1280px) and (max-width: 1535.9px) {
.grid {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
}
@media (min-width: 1536px) {
.grid {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
}
/* ... */
theme()
使用 theme()
函式,您可以使用點記法訪問您的主題設定值。
css
.btn-blue {
background-color: theme('colors.blue.500');
}
將會編譯成
css
.btn-blue {
background-color: #3b82f6;
}