Fullstack CourseLearn by building
Back to week 3

Topic

Style Next.js applications

Definition

Next.js supports global CSS and scoped styling approaches such as CSS Modules while preserving component and route composition.

In simpler words

Use global CSS for application-wide foundations and local styles for a component’s own rules.

Style ownership should be as deliberate as component ownership so visual changes remain predictable and accessible.

After this you can

  • Choose global versus scoped CSS
  • Import global styles at the root boundary
  • Preserve focus and semantic styling

Choose the right style scope

Global CSS is appropriate for resets, design tokens, typography foundations, and application-wide element defaults. Component-specific selectors should stay scoped so a future route cannot accidentally inherit an unrelated rule.

CSS Modules give local class names without requiring global naming conventions. Whatever system a repository adopts, semantic HTML and visible focus states remain part of the component contract.

Global foundation and local component style

// app/layout.tsx
import './globals.css';

// TicketCard.tsx
import styles from './TicketCard.module.css';
return <article className={styles.card}>...</article>;

The root imports global foundations; the card owns its isolated rules.

Mistake: removing focus outlines globally

/* Wrong — keyboard users lose their location */
*:focus { outline: none; }

/* Right — provide a visible replacement */
:focus-visible { outline: 2px solid var(--focus-ring); }

A visual design rule must not erase keyboard navigation feedback.

Keep styles close to their meaning

Definition

A component style should describe that component’s visual responsibility, not a route-wide accident.

In simpler words

Prefer meaningful class names and tokens over brittle selectors that depend on nesting depth.

Use the Course’s established IBM Plex and neutral visual language instead of introducing a competing visual system for one lesson.

Test hover-only affordances with keyboard focus and touch use in mind.

Live playground

CSS playground

Move one selector from global CSS to a module and observe its scope.

Global CSS from root layout; prefer scoped rules for components.

next-css

Keep in mind

  • Import global foundations from the root layout.
  • Scope component rules to avoid selector collisions.
  • Never remove focus visibility without an accessible replacement.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥80% (enforced by the API) to save progress.

Checking your session…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. Global CSS import place?
Concept
2. CSS Modules benefit?
Syntax
3. Tailwind in Next?
Practical
4. Why avoid huge global CSS soups?
Logic
5. Dark mode approaches?
Concept
6. CSS-in-JS caution with RSC?
Practical
7. Asset imports?
Syntax
8. Responsive layouts?
Logic
9. Composition with modules?
Concept
10. Which claim is true?
Practical
11. Where are global styles typically imported?
Conceptintermediate
// app/layout.tsx
import './globals.css';
12. What does the .module.css convention give you?
Syntaxintermediate
import styles from './Card.module.css';
<div className={styles.card} />
13. What must you consider with runtime CSS-in-JS here?
Practicaladvanced
import styled from 'styled-components';
// used in a Server Component
14. Why use clsx here?
Logicintermediate
<div className={clsx('btn', isActive && 'btn-active')} />
15. What is a simpler approach for responsive layout here?
Conceptadvanced
useEffect(() => window.addEventListener('resize', onResize));

Checking your session…