# The Root Layout

> The mandatory top-level wrapper that defines your HTML document.

Subject: Next.js | Lesson 7 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/root-layout
Previous: [The layout.js Convention](https://codewithanimation.in/nextjs/layout-file.md) | Next: [The template.js Convention](https://codewithanimation.in/nextjs/template-file.md)

## The foundation of the app

Every Next.js App Router application must have a Root Layout. This is a `layout.js` (or `.tsx`) file placed at the very top of your `app` directory.

While normal layouts share UI between specific routes, the Root Layout shares UI across the *entire application*, wrapping every single page.

## No more magic document

In older versions of Next.js, the framework magically provided the HTML skeleton, and you had to use special files like `_app.js` and `_document.js` to customize it.

The App Router removes that magic. You are in complete control of the document. The Root Layout is entirely responsible for returning the fundamental `<html>` and `<body>` tags.

## The mandatory tags

A valid Root Layout must export a React component that accepts `{children}`, and it MUST return `<html>` and `<body>` tags containing those children.

This is also the perfect place to inject global CSS, global providers (like Redux or ThemeContext), and structural UI like a global navigation bar or footer.

## Common mistake: The fatal missing tags error

If you delete the Root Layout, Next.js will actually auto-generate a basic one for you. However, if you write your own Root Layout but forget the `<html>` or `<body>` tags, the framework will crash.

Next.js cannot render a webpage without these tags, and it will throw an error telling you that the Root Layout is invalid.

## Tip: Multiple root layouts

Usually, an app has one Root Layout. But what if your marketing pages need a completely different HTML structure than your dashboard app?

By using Route Groups (folders with parentheses like `(marketing)` and `(dashboard)`), you can completely delete the top-level `app/layout.tsx` and place distinct Root Layouts inside each group folder.

## Try it: The Document Tree

See how the Root Layout encapsulates everything, from global providers down to the innermost nested page.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/root-layout

## Check yourself

### Question 1

What two HTML tags are strictly mandatory inside a Next.js Root Layout?

- A. <head> and <title>
- B. <header> and <footer>
- C. <html> and <body>
- D. <main> and <section>

Correct answer: C

Because Next.js no longer provides a hidden `_document.js` file, your Root Layout must explicitly define the `<html>` and `<body>` tags to construct a valid web page.

- Why not A: Metadata like titles is now handled by the separate Metadata API, not directly hardcoded.
- Why not B: These are semantic UI elements, but they are not required by the framework.
- Why not D: These are just semantic blocks.

### Question 2

The Root Layout in the App Router replaces which two files from the older Pages Router?

- A. next.config.js and package.json
- B. _app.js and _document.js
- C. index.js and 404.js
- D. api/route.js and middleware.js

Correct answer: B

The Root Layout unifies the global application wrapper (formerly `_app.js`) and the custom HTML document (formerly `_document.js`) into a single, intuitive file.

- Why not A: These configuration files still exist.
- Why not C: These are replaced by page.js and not-found.js.
- Why not D: These handle endpoints and routing, not layouts.

## Remember this

- A Root Layout is required in the top level of the `app` directory.
- It must contain the `<html>` and `<body>` tags.
- It replaces the old `_app.js` and `_document.js` files.
- You can have multiple root layouts by utilizing Route Groups.
