Styles and a missing pageNext.js · 5 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Home</Link>
<Link href="/about">About</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import styles from "./page.module.css";
export default function HomePage() {
return (
<main>
<h1 className={styles.title}>Study Hub</h1>
<p className={styles.intro}>Plan what you study, one day at a time.</p>
<p className="muted">Try a page that doesn't exist, such as /settings.</p>
</main>
);
}
.title {
color: #06617f;
font-size: 2.25rem;
}
.intro {
font-size: 1.15rem;
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<p>A place to plan what you study each day.</p>
</main>
);
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Global CSS
app/layout.jsx imports ./globals.css:
JavaScript
import "./globals.css";
Because the root layout wraps every page, these styles apply everywhere. Use
global CSS for the basics: fonts, colours, spacing, and classes you use all
over, like .muted and .card.
CSS Modules
For styles that belong to one page or component, name the file
something.module.css, and import it as an object:
JSX
import styles from "./page.module.css";<h1 className={styles.title}>Study Hub</h1>
Next.js renames each class to something unique. Inspect the heading in your
browser's developer tools and you'll see a class like …title… with extra
characters, not title. So two modules can both have a .title, and they never
clash.
Not found
Type /settings into the address bar. There's no such page, so Next.js shows
its built-in 404 page, inside your layout.
To show your own, add app/not-found.jsx. It's a component like any page, and
Next.js shows it whenever no route matches, inside the root layout.
Exercise
Your own 404 page
Add a not-found page with the heading Page not found and the text
That page isn't here. Try the home page., where home page links to /.
Your working spaceNext.js · 5 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Home</Link>
<Link href="/about">About</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import styles from "./page.module.css";
export default function HomePage() {
return (
<main>
<h1 className={styles.title}>Study Hub</h1>
<p className={styles.intro}>Plan what you study, one day at a time.</p>
<p className="muted">Try a page that doesn't exist, such as /settings.</p>
</main>
);
}
.title {
color: #06617f;
font-size: 2.25rem;
}
.intro {
font-size: 1.15rem;
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<p>A place to plan what you study each day.</p>
</main>
);
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
app/not-found.jsx, with a default export, like a page.
Hint 2: a first step
Import Link from next/link, as the layout does.
Show one solution
app/not-found.jsx:
JSX
import Link from "next/link";export default function NotFound() { return ( <main> <h1>Page not found</h1> <p> That page isn't here. Try the <Link href="/">home page</Link>. </p> </main> );}
Visit /settings again: your page appears, with the navigation still above it.
Exercise
Style the About page
Give the About page's heading its own colour with a CSS Module, without
changing the home page's heading.
Your working spaceNext.js · 5 filesimport Link from "next/link";
import "./globals.css";
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>
<header>
<nav>
<Link href="/">Home</Link>
<Link href="/about">About</Link>
</nav>
</header>
{children}
</body>
</html>
);
}
import styles from "./page.module.css";
export default function HomePage() {
return (
<main>
<h1 className={styles.title}>Study Hub</h1>
<p className={styles.intro}>Plan what you study, one day at a time.</p>
<p className="muted">Try a page that doesn't exist, such as /settings.</p>
</main>
);
}
.title {
color: #06617f;
font-size: 2.25rem;
}
.intro {
font-size: 1.15rem;
}
body {
font: 16px/1.5 system-ui, sans-serif;
margin: 0;
color: #1c2430;
}
header,
main,
footer {
max-width: 36rem;
margin: 0 auto;
padding: 0.75rem 1rem;
}
header {
border-bottom: 1px solid #d6dde6;
}
nav {
display: flex;
gap: 1rem;
}
a {
color: #06617f;
}
h1 {
margin: 0.5rem 0;
}
.muted {
color: #5b6876;
}
.card {
border: 1px solid #d6dde6;
border-radius: 8px;
padding: 0.75rem 1rem;
margin-bottom: 0.75rem;
}
export default function AboutPage() {
return (
<main>
<h1>About</h1>
<p>A place to plan what you study each day.</p>
</main>
);
}
Runs with real Next.js on your computer: unzip, then npm install and npm run dev.
Hint 1: the idea
A CSS Module keeps a component’s class names separate from the same names used elsewhere.
Hint 2: a first step
Create app/about/page.module.css with a .title class, import it in
app/about/page.jsx, and use className={styles.title}. The home page's
.title won't clash with it.
Show one solution
app/about/page.module.css:
CSS
.title { color: #8a3ffc;}
app/about/page.jsx:
JSX
import styles from "./page.module.css";export default function AboutPage() { return ( <main> <h1 className={styles.title}>About</h1> <p>A place to plan what you study each day.</p> </main> );}
Completion is saved in this browser only. It does not measure mastery.