Blazor Navbar
The Blazor Bootstrap Navbar component is a responsive navigation header with a brand, links, dropdowns, forms, and text, which collapses behind a toggler or into an offcanvas panel. The examples below cover brand, nav, forms, text and color schemes.
INFO
Navbar follows the Bootstrap 5.3 navbar.
It collapses its menu behind a toggler below
ExpandBreakpoint (Large by default), so resize the window to see the collapsed menu.
The collapse, offcanvas, and dropdowns use the Bootstrap JavaScript bundle the app already loads.
Basic usage #
A navbar holds a brand, navigation links, dropdowns, and forms, and collapses them behind a toggler on small screens.
How to use:
The item matching the current URL is active and marked
How to use:
- Add
Navbarwith anAriaLabeland aBackgroundColor. - Put a
NavbarBrandin theBrandslot. - Put the menu in
ChildContent: aNavbarNavwithNavbarItemandNavbarDropdowncomponents, and any form. - Give a search form
role="search".
aria-current="page".
Brand #
The brand can be a link, plain text, an image, or an image with text.
How to use:
How to use:
- Set
Hreffor a link; leave it unset for plain text. - Set
ImageSrc,ImageWidth, andImageHeightfor an image. - Set
ImageAltto the brand name when the image is shown alone, or to an empty string when text follows it.
Nav #
Navigation links, disabled links, and dropdowns.
How to use:
How to use:
- Add
NavbarItemwithHref. SetMatch="NavLinkMatch.All"on a home link so it is not active on every page. - Set
Disabled="true"to disable a link. - Add
NavbarDropdownwithText, holdingDropdownItem,DropdownHeader, andDropdownDividercomponents.
Forms #
Place a search form, an input group, or buttons in the navbar.
How to use:
These navbars set
How to use:
- Put the form in
ChildContent. Useclass="d-flex"to keep its controls on one line. - Use
InputGroupfor add-ons, andButtonfor actions.
ExpandBreakpoint="NavbarExpand.Always", so they never collapse.
Text #
Add text that is vertically centered and colored for the navbar.
How to use:
Change the display name and press Tab to update the text. It is rendered as text, never as HTML.
How to use:
- Add
NavbarTextinChildContent.
Color schemes #
Pair a background with a light or dark theme.
How to use:
How to use:
- Set
BackgroundColor(abg-*class) orColor(atext-bg-*class), or a custom background withStyle. - Set
Theme="NavbarTheme.Dark"on dark backgrounds, so the text is light, andNavbarTheme.Lighton light ones.
Containers #
The navbar's contents are fluid by default. Wrap the navbar in a container, or limit its contents to a container width.
How to use:
How to use:
- Set
ContainertoNavbarContainer.Defaultfor a centered container, or to a breakpoint for a responsive container.
Placement #
Place the navbar in the page flow, fix it to the top or bottom of the viewport, or make it sticky.
How to use:
Pick a placement and scroll the box. The demo box stands in for the viewport.
How to use:
- Set
Placement. A fixed navbar leaves the page flow, so add padding to thebodyto keep content from going under it.
Responsive behaviors #
Choose when the menu collapses and where the toggler goes.
How to use:
Narrow the window below 992px to see the first three collapse.
How to use:
- Set
ExpandBreakpoint.Neverkeeps the menu collapsed at every width;Alwaysnever collapses it. - Set
TogglerPosition="NavbarTogglerPosition.Start"to put the toggler before the brand. - Set
BrandInCollapse="true"to hide the brand until the menu expands.
Scrolling #
Let a long collapsed menu scroll instead of growing.
How to use:
How to use:
- Set
Scrollable="true"onNavbarNav, andScrollHeightto its maximum height (75vh by default).
External content #
Show and hide content that lives outside the navbar.
How to use:
How to use:
- Add a
Collapsewith anId. - Set
ExternalCollapseIdon the navbar to that id. The toggler then controls the collapse, and focus moves into it when it opens. - Set
ExpandBreakpoint="NavbarExpand.Never"so the toggler shows at every width.
Offcanvas #
Show the collapsed menu in an offcanvas panel.
How to use:
The first navbar is a responsive offcanvas; the second always uses a dark offcanvas.
How to use:
- Set
CollapseMode="NavbarCollapseMode.Offcanvas"and anOffcanvasTitle. - Set
OffcanvasPlacementto choose the side it opens from; the default is the end. - Above
ExpandBreakpointthe menu shows inline, as a regular navbar. UseNavbarExpand.Neverto keep the offcanvas at every width.
Methods #
Show, hide, or toggle the collapsed menu from code.
How to use:
How to use:
- Get a reference with
@ref, and callShowAsync,HideAsync, orToggleAsync. - The methods do nothing while the navbar is expanded.
- Parameters such as
CollapseModecan change at runtime; the methods keep working on the new menu.
Close after navigation #
The collapsed menu closes by itself after a navigation, so the new page is not hidden behind it.
Open the menu and pick a view. The menu closes, and the picked view becomes the active item.
Open the menu and pick a view. The menu closes, and the picked view becomes the active item.
Sidebar layout #
Use the navbar as the top bar of a layout with a
How to use:
Sidebar.
How to use:
- Put the navbar at the top of
main, beside theSidebar, withPlacement="NavbarPlacement.StickyTop". - The navbar uses Bootstrap's own sticky (1020) and fixed (1030) z-index, which matches the layout's sticky header.