PostCSS Plugin
Plugin PostCSS cho UnoCSS. Hỗ trợ các chỉ thị @apply, @screen và theme().
WARNING
Gói này hiện đang ở trạng thái thử nghiệm. Nó không tuân theo semver, và có thể giới thiệu các thay đổi phá vỡ trong các phiên bản patch.
Cài đặt
pnpm add -D unocss @unocss/postcssyarn add -D unocss @unocss/postcssnpm install -D unocss @unocss/postcssbun add -D unocss @unocss/postcssimport UnoCSS from '@unocss/postcss'
export default {
plugins: [
UnoCSS(),
],
}import { defineConfig, presetWind3 } from 'unocss'
export default defineConfig({
content: {
filesystem: [
'**/*.{html,js,ts,jsx,tsx,vue,svelte,astro}',
],
},
presets: [
presetWind3(),
],
})@unocss;Sử dụng
@unocss
At-rule @unocss là một placeholder. Nó sẽ được thay thế bằng CSS được tạo.
Bạn cũng có thể chèn từng layer riêng lẻ:
@unocss preflights;
@unocss default;
/*
Fallback layer. It's always recommended to include.
Only unused layers will be injected here.
*/
@unocss;Nếu bạn muốn bao gồm tất cả các layer bất kể chúng đã được bao gồm trước đó hay không, bạn có thể dùng @unocss all. Điều này hữu ích nếu bạn muốn bao gồm CSS được tạo trong nhiều tệp.
@unocss all;@apply
.custom-div {
@apply text-center my-0 font-medium;
}Sẽ được biến đổi thành:
.custom-div {
margin-top: 0rem;
margin-bottom: 0rem;
text-align: center;
font-weight: 500;
}@screen
Chỉ thị @screen cho phép bạn tạo các truy vấn media tham chiếu đến các điểm ngắt của bạn theo tên đến từ theme.breakpoints.
.grid {
@apply grid grid-cols-2;
}
@screen xs {
.grid {
@apply grid-cols-1;
}
}
@screen sm {
.grid {
@apply grid-cols-3;
}
}
/* ... */Sẽ được biến đổi thành:
.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));
}
}
/* ... */Hỗ trợ Biến thể Điểm ngắt
@screen cũng hỗ trợ các biến thể lt、at
@screen lt
.grid {
@apply grid grid-cols-2;
}
@screen lt-xs {
.grid {
@apply grid-cols-1;
}
}
@screen lt-sm {
.grid {
@apply grid-cols-3;
}
}
/* ... */Sẽ được biến đổi thành:
.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
.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;
}
}
/* ... */Sẽ được biến đổi thành:
.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()
Dùng hàm theme() để truy cập các giá trị cấu hình chủ đề của bạn bằng ký hiệu dấu chấm.
.btn-blue {
background-color: theme('colors.blue.500');
}Sẽ được biên dịch thành:
.btn-blue {
background-color: #3b82f6;
}