Skip to content

Next.js

开始使用 UnoCSS 和 Next.js。查看示例

🌐 Getting Started with UnoCSS and Next.js. Check the example.

设置

🌐 Setup

安装

🌐 Installation

bash
pnpm add -D unocss @unocss/postcss @unocss/reset
bash
yarn add -D unocss @unocss/postcss @unocss/reset
bash
npm install -D unocss @unocss/postcss @unocss/reset
bash
bun add -D unocss @unocss/postcss @unocss/reset

配置

🌐 Configuration

在项目根目录下创建 uno.config.tsuno.config.js

🌐 Create uno.config.ts or uno.config.js at the root of your project.

ts
import {
  defineConfig,
  presetAttributify,
  presetIcons,
  presetWebFonts,
  presetWind3
} from 'unocss'

export default defineConfig({
  presets: [
    presetWind3(),
    // ...
  ],
})
js
import {
  defineConfig,
  presetAttributify,
  presetIcons,
  presetWebFonts,
  presetWind3
} from 'unocss'

export default defineConfig({
  presets: [
    presetWind3(),
    // ...
  ],
})

在项目根目录下创建 postcss.config.mjs

🌐 Create postcss.config.mjs at the root of your project.

postcss.config.mjs
js
export default {
  plugins: {
    '@unocss/postcss': {
      content: ['./app/**/*.{html,js,ts,jsx,tsx}'],
    },
  },
}

导入样式表

🌐 Import stylesheets

globals.css 中添加 @unocss all;

🌐 Add @unocss all; in globals.css.

globals.css
css
@unocss all;

/* ... */

然后在你的布局文件中导入 @unocss/reset/tailwind.css

🌐 Then import @unocss/reset/tailwind.css in your layout file.

tsx
import type { Metadata } from 'next'
import { Geist, Geist_Mono } from 'next/font/google'
import '@unocss/reset/tailwind.css'
import './globals.css'

const geistSans = Geist({
  variable: '--font-geist-sans',
  subsets: ['latin'],
})

const geistMono = Geist_Mono({
  variable: '--font-geist-mono',
  subsets: ['latin'],
})

export const metadata: Metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
}

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode
}>) {
  return (
    <html lang="en">
      <body
        className={`${geistSans.variable} ${geistMono.variable} antialiased`}
      >
        {children}
      </body>
    </html>
  )
}
js
import { Geist, Geist_Mono } from 'next/font/google'
import '@unocss/reset/tailwind.css'
import './globals.css'

const geistSans = Geist({
  variable: '--font-geist-sans',
  subsets: ['latin'],
})

const geistMono = Geist_Mono({
  variable: '--font-geist-mono',
  subsets: ['latin'],
})

export const metadata = {
  title: 'Create Next App',
  description: 'Generated by create next app',
}

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body className={`${geistSans.variable} ${geistMono.variable}`}>
        {children}
      </body>
    </html>
  )
}

用法

🌐 Usage

使用 unocss 设计你的组件!

🌐 Style your components with unocss!

tsx
export default function Home() {
  return (
    <main className="py-20 px-12 text-center flex flex-col items-center gap-20px">
      <span className="text-blue text-5xl text-hover:red cursor-default">Nextjs</span>
      <div className="i-carbon-car inline-block text-4xl" />
      <button className="btn w-10rem">Button</button>
    </main>
  )
}
js
export default function Home() {
  return (
    <main className="py-20 px-12 text-center flex flex-col items-center gap-20px">
      <span className="text-blue text-5xl text-hover:red cursor-default">Nextjs</span>
      <div className="i-carbon-car inline-block text-4xl" />
      <button className="btn w-10rem">Button</button>
    </main>
  )
}

许可

🌐 License