Links, images and fonts
Next.js includes components that make navigation instant and media fast, with almost no configuration.
<Link>: client-side navigation
Use <Link> from next/link instead of <a> for links within your app:
import Link from 'next/link';
export default function Nav() {
return (
<nav>
<Link href="/">Home</Link>
<Link href="/blog">Blog</Link>
<Link href="/blog/hello-world">A post</Link>
<Link href={{ pathname: '/search', query: { q: 'shoes' } }}>Search shoes</Link>
</nav>
);
}What <Link> gives you over a plain <a>:
- Client-side navigation: no full page reload, and layouts keep their state.
- Prefetching: when a link scrolls into view, Next.js preloads the route in the background, so clicks feel instant.
- It still renders a real
<a href>, so it's accessible, works for SEO, and Ctrl+click opens a new tab.
For external sites, a normal <a href="https://…"> is fine.
Highlighting the active link
usePathname() reads the current URL. Because it's a hook, the component must be a Client Component:
'use client';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
const links = [
{ href: '/', label: 'Home' },
{ href: '/blog', label: 'Blog' },
{ href: '/about', label: 'About' },
];
export function NavLinks() {
const pathname = usePathname();
return (
<nav>
{links.map((l) => (
<Link key={l.href} href={l.href} className={pathname === l.href ? 'font-bold underline' : ''}>
{l.label}
</Link>
))}
</nav>
);
}The same logic, simulated so you can try it:
Navigating in code: useRouter
'use client';
import { useRouter } from 'next/navigation'; // note: next/navigation, not next/router
export function LogoutButton() {
const router = useRouter();
async function logout() {
await fetch('/api/logout', { method: 'POST' });
router.push('/login'); // also: router.replace(), router.back(), router.refresh()
}
return <button onClick={logout}>Log out</button>;
}On the server (in Server Components and Server Actions), use redirect('/login') from next/navigation instead.
<Image>: automatic image optimisation
import Image from 'next/image';
import hero from './hero.jpg'; // a local import gives width/height automatically
export default function Page() {
return (
<>
<Image src={hero} alt="Mountains at sunrise" placeholder="blur" loading="eager" fetchPriority="high" />
<Image src="/team/asha.jpg" alt="Asha Rao" width={320} height={320} />
</>
);
}next/image automatically:
- Serves modern formats (WebP/AVIF) at the right size for each device
- Lazy-loads off-screen images
- Prevents layout shift by reserving space (
width/height)
| Prop | Use |
|---|---|
alt |
Required: describe the image |
width / height |
Required for remote or public/ images (not for static imports) |
fill |
Fill the parent element (the parent needs position: relative) |
sizes |
Tell the browser how wide the image will display, for responsive layouts |
loading="eager" / fetchPriority="high" |
For the main hero image: load it immediately. (The old priority prop is deprecated in Next.js 16.) |
Remote images must be allowed in the config:
// next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
images: {
remotePatterns: [{ protocol: 'https', hostname: 'images.example-cdn.com' }],
},
};
export default nextConfig;next/font: fast, self-hosted fonts
// app/layout.tsx
import { Inter, JetBrains_Mono } from 'next/font/google';
const inter = Inter({ subsets: ['latin'], display: 'swap' });
const mono = JetBrains_Mono({ subsets: ['latin'], variable: '--font-mono' });
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" className={`${inter.className} ${mono.variable}`}>
<body>{children}</body>
</html>
);
}Google Fonts are downloaded at build time and self-hosted. There are no requests to Google from your users' browsers, which is better for privacy and speed, and it avoids layout shift.
Styling options
| Approach | Notes |
|---|---|
| Tailwind CSS | The default in create-next-app; utility classes in your JSX |
| CSS Modules | Button.module.css → import styles from './Button.module.css', with class names scoped automatically |
| Global CSS | app/globals.css, imported in the root layout |
| Sass, CSS-in-JS | Supported (CSS-in-JS needs Client Components or compatible libraries) |
Practice (in your project)
- Build a header with
<Link>s to three pages, and highlight the active one withusePathname(). - Add a hero image with
next/imageandfetchPriority="high". - Load a Google font with
next/fontand apply it in the root layout.
1.Why use <Link> instead of <a> for internal navigation?
2.Which import gives you useRouter in the App Router?