
- Next.js 教程
- Next.js - 主頁
- Next.js - 概述
- Next.js - 環境設定
- Next.js 功能
- Next.js - 頁面
- Next.js - 靜態檔案服務
- Next.js - 元資料
- Next.js - CSS 支援
- Next.js - 全域性 CSS 支援
- Next.js - 預渲染
- Next.js 路由
- Next.js - 路由
- Next.js - 動態 API 路由
- Next.js - 命令式路由
- Next.js - 淺層路由
- Next.js API 路由
- Next.js - API 路由
- Next.js - API 中介軟體
- Next.js - 響應助手
- Next.js 雜項
- Next.js - Typescript
- Next.js - 環境變數
- Next.js - 部署
- Next.js - CLI
- Next.js 有用資源
- Next.js - 快速指南
- Next.js - 有用資源
- Next.js - 討論
Next.js - CSS 支援
在 Next.js 中,我們可以使用內建名為 styled-jsx 的 css-in-js 庫。它允許在 React 元件內編寫 css,並且這些樣式將限制到元件。
在此示例中,我們將建立一個 Container 物件,透過包含其他元件對其進行樣式化。
讓我們更新 元資料 章節中使用的 nextjs 專案。
首先在根級別建立一個 Components 目錄,並如下面新增一個檔案 container.module.css −
.container { max-width: 36rem; padding: 0 1rem; margin: 3rem auto 6rem; border: 1px solid red; }
在 Components 目錄中建立 container.js 檔案
import styles from './container.module.css' function Container({ children }) { return <div className={styles.container}>{children}</div> } export default Container
現在在 first.js 中使用 Container 元件。
import Link from 'next/link' import Head from 'next/head' import Container from '../../components/container' export default function FirstPost() { return ( <> <Container> <Head> <title>My First Post</title> </Head> <h1>My First Post</h1> <h2> <Link href="/"> <a>Home</a> </Link> </h2> </Container> </> ) }
啟動 Next.js 伺服器
執行以下命令啟動伺服器 −.
npm run dev > nextjs@1.0.0 dev \Node\nextjs > next ready - started server on https://:3000 event - compiled successfully event - build page: / wait - compiling... event - compiled successfully event - build page: /next/dist/pages/_error wait - compiling... event - compiled successfully
驗證輸出
在瀏覽器中開啟 localhost:3000 並轉到第一個帖子,您將看到以下輸出。

廣告