Layouts
Layouts hold the parts that repeat on every page — header, menu, footer — so pages only contain their own content.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Related tutorials
Interview questions
Learning objectives#
- Create a layout with @Body
- Apply layouts per page or folder
- Nest layouts
- Build a responsive navigation menu
Explanation#
A layout is a component that inherits LayoutComponentBase and renders @Body where the page content goes.
The default layout is set in Routes.razor (<RouteView DefaultLayout="typeof(Layout.MainLayout)" />). A page can choose another with @layout AdminLayout.
An _Imports.razor file in a folder can apply @layout to every page in that folder.
Layouts can be nested: AdminLayout can itself use @layout MainLayout.
Code example: An admin layout#
@inherits LayoutComponentBase
@layout MainLayout
<div class="admin">
<aside>
<nav aria-label="Admin">
<NavLink href="admin" Match="NavLinkMatch.All">Dashboard</NavLink>
<NavLink href="admin/products">Products</NavLink>
<NavLink href="admin/orders">Orders</NavLink>
</nav>
</aside>
<section class="admin-content">
@Body
</section>
</div>
@* Components/Pages/Admin/_Imports.razor *@
@* @layout AdminLayout *@
Every page under Pages/Admin shows the site header and footer (from MainLayout) plus the admin side menu, with the page content in the right column.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The layout inherits LayoutComponentBase.
- @layout MainLayout nests it inside the main site layout.
- NavLink items highlight the current section.
- _Imports.razor applies the layout to every admin page.
Real-world use case#
A public site layout and a separate admin layout with a side menu, shared header and footer.
Common mistakes#
Put shared UI in a layout so changes happen once.
Make only the menu component interactive; keep the layout static.
Best practices#
- Keep layouts thin
- Use _Imports.razor for folder-wide layouts
- Use semantic landmarks (header, nav, main, footer)
- Interactive islands instead of interactive layouts
Remember this#
Layout = LayoutComponentBase + @Body.
@layout on a page or in _Imports.razor.
Layouts can nest.
Practice exercise#
Your task: Make a single page use a layout called BlankLayout.
Show solution
@page "/print/{Id:int}"
@layout BlankLayout
Mini quiz#
Where does a layout render the page content?
Want more? Practise all Blazor MCQs · Blazor interview questions
My notes#
Saved in this browser.