Layouts and Templates

Back

Loading concept...

๐Ÿ  Next.js Layouts and Templates: Building Your Websiteโ€™s Blueprint

Imagine youโ€™re building a house. Every room needs walls, a ceiling, and a floor. But you donโ€™t rebuild these for every single room, right? You build them once and then customize what goes inside each room. Thatโ€™s exactly what Layouts do in Next.js!


๐ŸŽฏ What Youโ€™ll Learn

Think of this guide as a treasure map. Weโ€™ll explore:

  • How layouts wrap your pages like a cozy blanket
  • The special โ€œRoot Layoutโ€ that holds everything
  • How to nest layouts like Russian dolls
  • The magic props that layouts receive
  • Templates vs Layouts (sneaky differences!)
  • Making things pretty with CSS

๐Ÿ“ฆ Layouts Overview

The Big Idea

A layout is like a picture frame. The frame stays the same, but you can swap out the picture inside!

graph TD A["Layout"] --> B["Header stays same"] A --> C["Page Content CHANGES"] A --> D["Footer stays same"]

Real-Life Example

When you visit YouTube:

  • The top bar (search, logo) = stays the same
  • The sidebar (menu) = stays the same
  • The middle videos = changes when you click!

That unchanging part? Thatโ€™s the layout!

Code Example

// app/layout.js
export default function Layout({ children }) {
  return (
    <div>
      <header>๐Ÿ  My Website</header>
      {children}
      <footer>ยฉ 2024</footer>
    </div>
  );
}

Whatโ€™s happening?

  • children = the page that changes
  • Everything else = stays put!

๐ŸŒณ Root Layout

The Boss of All Layouts

The Root Layout is special. Itโ€™s like the foundation of your house. Every single page in your app lives inside it!

It MUST have two things:

  1. <html> tag
  2. <body> tag
// app/layout.js (Root Layout)
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
      </body>
    </html>
  );
}

Why Is It Special?

Regular Layout Root Layout
Optional Required
Can skip Cannot skip
No html/body Must have html/body
Many possible Only ONE

๐ŸŽช Think of it this way: Root Layout is the circus tent. Everything happens inside it!


๐Ÿช† Nested Layouts

Layouts Inside Layouts!

Just like Russian dolls (matryoshka), you can put layouts inside layouts!

graph TD A["Root Layout"] --> B["Dashboard Layout"] B --> C["Settings Layout"] C --> D["Page Content"]

How It Works

app/
โ”œโ”€โ”€ layout.js          โ† Root Layout
โ”œโ”€โ”€ dashboard/
โ”‚   โ”œโ”€โ”€ layout.js      โ† Dashboard Layout
โ”‚   โ””โ”€โ”€ settings/
โ”‚       โ”œโ”€โ”€ layout.js  โ† Settings Layout
โ”‚       โ””โ”€โ”€ page.js    โ† Actual Page

Real Example

// app/dashboard/layout.js
export default function DashboardLayout({ children }) {
  return (
    <div>
      <nav>๐Ÿ“Š Dashboard Menu</nav>
      <main>{children}</main>
    </div>
  );
}

Magic Result:

  • Dashboard pages get the dashboard menu
  • Other pages donโ€™t!

๐ŸŽ Layout Props

What Does a Layout Receive?

Layouts get a special gift called children. Itโ€™s the content that goes inside!

export default function Layout({ children }) {
  // children = whatever page is being shown
  return <div>{children}</div>;
}

The Props Available

Prop What It Is
children The page or nested layout

๐Ÿ’ก Simple rule: children is always the โ€œstuff insideโ€!

Example With Styling

export default function Layout({ children }) {
  return (
    <div className="container">
      <aside>Sidebar</aside>
      <main>{children}</main>
    </div>
  );
}

โš”๏ธ Templates vs Layouts

The Sneaky Difference!

They look the sameโ€ฆ but they act differently!

Feature Layout Template
Keeps state โœ… Yes โŒ No
Re-renders โŒ No โœ… Yes
File name layout.js template.js

What Does This Mean?

Layout = Lazy friend ๐Ÿ›‹๏ธ

  • Stays put when you navigate
  • Doesnโ€™t rebuild itself
  • Keeps remembering things

Template = Energetic friend ๐Ÿƒ

  • Fresh start every time!
  • Rebuilds on each navigation
  • Forgets everything

When to Use Each?

graph TD A["Need state to persist?"] A -->|Yes| B["Use Layout"] A -->|No| C["Need fresh start?"] C -->|Yes| D["Use Template"] C -->|No| B

๐Ÿ“„ template.js File

Creating a Template

Same structure as layout, different behavior!

// app/dashboard/template.js
export default function Template({ children }) {
  return (
    <div>
      <p>I re-render every navigation!</p>
      {children}
    </div>
  );
}

Real Use Case

Animation on every page visit:

// app/template.js
export default function Template({ children }) {
  return (
    <div className="fade-in">
      {children}
    </div>
  );
}

Every time you navigate, the fade-in animation plays again! ๐ŸŽฌ


๐ŸŽจ CSS Modules Support

Scoped Styles (No Conflicts!)

CSS Modules = styles that only work in one file. Like having your own private paint bucket!

How To Use

Step 1: Create a file ending in .module.css

/* layout.module.css */
.container {
  display: flex;
  gap: 20px;
}

.sidebar {
  width: 200px;
  background: #f0f0f0;
}

Step 2: Import and use it

// layout.js
import styles from './layout.module.css';

export default function Layout({ children }) {
  return (
    <div className={styles.container}>
      <aside className={styles.sidebar}>
        Menu
      </aside>
      <main>{children}</main>
    </div>
  );
}

Why CSS Modules Rock

Problem CSS Modules Fix
Name clashes โœ… Each file is isolated
Global mess โœ… Styles stay local
Confusion โœ… Clear ownership

๐ŸŒ Global CSS Imports

Styles That Work Everywhere

Sometimes you WANT styles to apply to everything. Thatโ€™s Global CSS!

How To Use

Step 1: Create a global CSS file

/* app/globals.css */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: Arial, sans-serif;
}

a {
  color: blue;
}

Step 2: Import it in Root Layout

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

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

Important Rule! โš ๏ธ

Global CSS can ONLY be imported in the Root Layout or inside CSS Modules!

graph TD A["Global CSS"] --> B["Import in Root Layout"] B --> C["Works everywhere!"]

๐ŸŽฏ Quick Summary

Concept One-Liner
Layout Wrapper that stays put
Root Layout The main wrapper with html/body
Nested Layouts Layouts inside layouts
Layout Props Just children!
Templates Fresh wrapper every time
CSS Modules Private styles per file
Global CSS Styles for everyone

๐Ÿš€ You Did It!

Now you understand how Next.js organizes pages like a pro!

Remember the house analogy:

  • ๐Ÿ  Root Layout = Foundation
  • ๐Ÿšช Nested Layouts = Rooms
  • ๐Ÿ–ผ๏ธ Templates = Repainting walls each visit
  • ๐ŸŽจ CSS = Making it all pretty!

Go build something amazing! ๐ŸŽ‰

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.