2021-10-11 12:57:54 +00:00
---
icon: IconDirectory
title: 'layouts'
head.title: Layouts directory
---
2021-06-08 21:44:30 +00:00
2021-10-11 12:57:54 +00:00
# Layouts directory
2021-06-30 16:32:22 +00:00
Nuxt provides a customizable layouts framework you can use throughout your application, ideal for extracting common UI or code patterns into reusable layout components.
2022-06-10 13:35:58 +00:00
Layouts are placed in the `layouts/` directory and will be automatically loaded via asynchronous import when used. Layouts are used by setting a `layout` property as part of your page metadata (if you are using the `~/pages` integration), or by using the `<NuxtLayout>` component. (**Note**: The layout name is normalized to kebab-case, so `someLayout` becomes `some-layout` .)
2021-06-30 16:32:22 +00:00
2022-04-16 13:53:36 +00:00
If you only have a single layout in your application, we recommend using [app.vue ](/guide/directory-structure/app ) instead.
2021-06-30 16:32:22 +00:00
2022-03-14 10:47:24 +00:00
::alert{type=warning}
2022-04-07 10:59:40 +00:00
Unlike other components, your layouts must have a single root element to allow Nuxt to apply transitions between layout changes - and this root element cannot be a `<slot />` .
2022-03-14 10:47:24 +00:00
::
## Example: Enabling layouts with `app.vue`
2021-06-30 16:32:22 +00:00
```bash
-| layouts/
---| custom.vue
2022-03-14 10:47:24 +00:00
-| app.vue
2021-06-30 16:32:22 +00:00
```
2022-03-14 10:47:24 +00:00
In your layout files, you'll need to use `<slot />` to define where the content of your layout will be loaded. For example:
2021-06-30 16:32:22 +00:00
2022-03-14 10:47:24 +00:00
```vue{}[layouts/custom.vue]
2021-06-30 16:32:22 +00:00
< template >
< div >
Some shared layout content:
< slot / >
< / div >
< / template >
```
2022-03-14 10:47:24 +00:00
Here's how you might use that layout in `app.vue` :
2021-06-30 16:32:22 +00:00
2022-03-14 10:47:24 +00:00
```vue{}[app.vue]
< template >
< NuxtLayout name = "custom" >
Hello world!
< / NuxtLayout >
< / template >
```
## Example: setting the layout with `~/pages`
```bash
-| layouts/
---| custom.vue
-| pages/
---| index.vue
```
You can set your layout within your page components like this:
```vue{}[pages/index.vue]
2021-06-30 16:32:22 +00:00
< script >
2022-01-17 18:27:23 +00:00
// This will also work in `<script setup>`
definePageMeta({
2021-06-30 16:32:22 +00:00
layout: "custom",
2022-01-17 18:27:23 +00:00
});
2021-06-30 16:32:22 +00:00
< / script >
```
2022-05-31 22:34:41 +00:00
And in your `app.vue` you need to wrap the `NuxtPage` component with the `NuxtLayout` component.
```vue{}[app.vue]
< template >
< NuxtLayout >
< NuxtPage / >
< / NuxtLayout >
< / template >
```
2022-01-17 18:27:23 +00:00
::alert{type=info}
2022-04-06 05:56:08 +00:00
Learn more about [defining page meta ](/guide/directory-structure/pages#page-metadata ).
2022-01-17 18:27:23 +00:00
::
2022-03-14 10:47:24 +00:00
## Example: manual control with `~/pages`
2021-06-30 16:32:22 +00:00
2022-03-14 10:47:24 +00:00
Even if you are using the `~/pages` integration, you can take full control by using the `<NuxtLayout>` component (which is globally available throughout your application), by setting `layout: false` .
2021-06-30 16:32:22 +00:00
2022-04-25 16:54:20 +00:00
::code-group
```vue [layouts/custom.vue]
< template >
< div >
< header >
< slot name = "header" >
Default header content
< / slot >
< / header >
< main >
< slot / >
< / main >
< / div >
< / template >
```
```vue [pages/index.vue]
2021-06-30 16:32:22 +00:00
< template >
2022-06-08 19:53:39 +00:00
< div >
< NuxtLayout name = "custom" >
< template #header > Some header template content. </ template >
2021-06-30 16:32:22 +00:00
2022-06-08 19:53:39 +00:00
The rest of the page
< / NuxtLayout >
< / div >
2021-06-30 16:32:22 +00:00
< / template >
2022-01-17 18:27:23 +00:00
< script setup >
definePageMeta({
2021-06-30 16:32:22 +00:00
layout: false,
2022-01-17 18:27:23 +00:00
});
2021-06-30 16:32:22 +00:00
< / script >
```
2021-10-21 10:14:40 +00:00
2022-04-25 16:54:20 +00:00
::
2022-06-08 19:53:39 +00:00
::alert{type=warning}
If you use `<NuxtLayout>` within your pages, make sure it is not the root element (or disable layout/page transitions).
::
2022-01-17 18:27:23 +00:00
## Example: changing the layout
2021-10-21 10:14:40 +00:00
2022-01-17 18:27:23 +00:00
You can also use a ref or computed property for your layout.
2021-10-21 10:14:40 +00:00
```vue
< template >
< div >
2022-01-17 18:27:23 +00:00
< button @click =" enableCustomLayout " > Update layout</ button >
2021-10-21 10:14:40 +00:00
< / div >
< / template >
< script setup >
2022-01-17 18:27:23 +00:00
const route = useRoute()
function enableCustomLayout () {
2022-01-26 11:56:24 +00:00
route.meta.layout = "custom"
2022-01-17 18:27:23 +00:00
}
definePageMeta({
2022-01-26 11:56:24 +00:00
layout: false,
2022-01-17 18:27:23 +00:00
});
2021-10-21 10:14:40 +00:00
< / script >
```
2022-04-09 09:25:13 +00:00
:LinkExample{link="/examples/routing/layouts"}