版面優化
目前為止,我們只添加了最基礎的 React 和 CSS 程式碼來說明概念,例如 CSS 模組 (CSS Modules)。在進入下一堂關於 資料獲取 (data fetching) 的課程前,讓我們先優化頁面樣式和程式碼。
更新 components/layout.module.css
首先,開啟 components/layout.module.css
並將其內容替換為以下更完善的版面配置和個人頭像樣式:
.container {
max-width: 36rem;
padding: 0 1rem;
margin: 3rem auto 6rem;
}
.header {
display: flex;
flex-direction: column;
align-items: center;
}
.backToHome {
margin: 3rem 0 0;
}
建立 styles/utils.module.css
接著,我們建立一組可重複使用的 CSS 工具類別(用於文字樣式)。
新增一個名為 styles/utils.module.css
的 CSS 檔案,內容如下:
.heading2Xl {
font-size: 2.5rem;
line-height: 1.2;
font-weight: 800;
letter-spacing: -0.05rem;
margin: 1rem 0;
}
.headingXl {
font-size: 2rem;
line-height: 1.3;
font-weight: 800;
letter-spacing: -0.05rem;
margin: 1rem 0;
}
.headingLg {
font-size: 1.5rem;
line-height: 1.4;
margin: 1rem 0;
}
.headingMd {
font-size: 1.2rem;
line-height: 1.5;
}
.borderCircle {
border-radius: 9999px;
}
.colorInherit {
color: inherit;
}
.padding1px {
padding-top: 1px;
}
.list {
list-style: none;
padding: 0;
margin: 0;
}
.listItem {
margin: 0 0 1.25rem;
}
.lightText {
color: #666;
}
你可以在整個應用程式中重複使用這些工具類別,甚至可以在
global.css
檔案中使用。工具類別指的是撰寫 CSS 選擇器的一種方法,而非特定的技術(例如全域樣式、CSS 模組、Sass 等)。了解更多關於 工具優先的 CSS (utility-first CSS)。
更新 components/layout.js
第三步,開啟 components/layout.js
並將其內容替換為以下程式碼,將 Your Name
改為實際名稱:
import Head from 'next/head';
import Image from 'next/image';
import styles from './layout.module.css';
import utilStyles from '../styles/utils.module.css';
import Link from 'next/link';
const name = 'Your Name';
export const siteTitle = 'Next.js Sample Website';
export default function Layout({ children, home }) {
return (
<div className={styles.container}>
<Head>
<link rel="icon" href="/favicon.ico" />
<meta
name="description"
content="Learn how to build a personal website using Next.js"
/>
<meta
property="og:image"
content={`https://og-image.vercel.app/${encodeURI(
siteTitle,
)}.png?theme=light&md=0&fontSize=75px&images=https%3A%2F%2Fassets.vercel.com%2Fimage%2Fupload%2Ffront%2Fassets%2Fdesign%2Fnextjs-black-logo.svg`}
/>
<meta name="og:title" content={siteTitle} />
<meta name="twitter:card" content="summary_large_image" />
</Head>
<header className={styles.header}>
{home ? (
<>
<Image
priority
src="/images/profile.jpg"
className={utilStyles.borderCircle}
height={144}
width={144}
alt=""
/>
<h1 className={utilStyles.heading2Xl}>{name}</h1>
</>
) : (
<>
<Link href="/">
<Image
priority
src="/images/profile.jpg"
className={utilStyles.borderCircle}
height={108}
width={108}
alt=""
/>
</Link>
<h2 className={utilStyles.headingLg}>
<Link href="/" className={utilStyles.colorInherit}>
{name}
</Link>
</h2>
</>
)}
</header>
<main>{children}</main>
{!home && (
<div className={styles.backToHome}>
<Link href="/">← Back to home</Link>
</div>
)}
</div>
);
}
以下是新增的內容:
meta
標籤(如og:image
),用於描述頁面內容- 布林值
home
屬性,用於調整標題和圖片大小 - 如果
home
為false
,底部會顯示「返回首頁」連結 - 使用
next/image
添加圖片,並透過 priority 屬性進行預載
更新 pages/index.js
最後,讓我們更新首頁。
開啟 pages/index.js
並將其內容替換為:
import Head from 'next/head';
import Layout, { siteTitle } from '../components/layout';
import utilStyles from '../styles/utils.module.css';
export default function Home() {
return (
<Layout home>
<Head>
<title>{siteTitle}</title>
</Head>
<section className={utilStyles.headingMd}>
<p>[Your Self Introduction]</p>
<p>
(This is a sample website - you’ll be building a site like this on{' '}
<a href="https://nextjs.org/learn">our Next.js tutorial</a>.)
</p>
</section>
</Layout>
);
}
然後將 [Your Self Introduction]
替換為你的自我介紹。以下是作者的範例:
完成!我們現在有了優化後的版面程式碼,可以繼續進行資料獲取的課程了。
在結束本課程之前,讓我們在下一頁討論一些與 Next.js CSS 支援相關的實用技巧。