navigation

Top-of-page navbar (NavBar) with a brand link, navigation links, actions, and an optional disclosure button for narrow screens

NavBar renders a <header navbar> with a brand link, a <nav> region for links and collapsible controls such as search, and an optional end group for actions such as an account menu.

from fasthtml.common import H1, P
from answerui.registry import reg
from answerui.layout import Stack, Cluster
from answerui.surfaces import Badge
from answerui.demo import demo

By default, NavBar uses the page background (--color-background) with a bottom hairline border (--color-border) and places the brand, navigation, and end actions on one row. Links appear as plain text. Mark the current link with aria_current='page' to highlight it with the accent wash (--v-3 background and --v2 text, or --tint on a vivid bar).

Pass align='center' to centre <nav> between the brand and end actions, or align='end' to align <nav> beside end (the default align='start' places links right after the brand). Pass sticky=True to keep the bar pinned to the top while scrolling. Pass vivid=True for an accent surface (--color-accent with --color-on-accent text), and theme='light' or theme='dark' to override the inherited colour scheme (ui-theme).

When collapse=True and navigation items are passed, NavBar adds a disclosure button. The shared mountNavbar controller measures the actual row width instead of using a fixed viewport breakpoint, and hides <nav> behind that button whenever the row overflows. Opening the disclosure shows the same <nav> in normal flow below the bar without duplicating its content. Clicking a link, clicking outside the bar, or pressing Escape closes the menu. Pressing Escape returns focus to the disclosure button. Content passed as end stays visible in the bar.


def NavBar(
    brand, *c, end:tuple=(), href:str='/', label:str='Main navigation', align:str='start', sticky:bool=False,
    vivid:bool=False, theme=None, collapse:bool=False, **attrs
):

A brand, navigation links and actions sharing a responsive row

In this dashboard example, the links and search field go into the collapsible <nav> while the account menu stays visible in end. The menu trigger wraps an Avatar with aria_label='Account'. Shared CSS styles an avatar button as a circle:

NavBar.demo(height='14rem')

Configurations

These options can be combined. The examples below show align='center', align='end' beside sign-in buttons in end, vivid=True with an accent background, and theme='dark' on a light page:

def _auth(): return [Btn('Log in', variant='secondary'), Btn('Sign up')]
demo(Stack(
    NavBar('Acme', *_links(), align='center'),
    NavBar('Acme', *_links(), end=_auth(), align='end'),
    NavBar('Acme', *_links(), end=_auth(), vivid=True),
    NavBar('Acme', *_links(), end=_auth(), theme='dark')))

Collapse

At 24rem wide, the bar collapses because the links and search field do not fit beside the brand. Open the disclosure menu to tab through the links. The account menu stays visible in end.

demo(_navbar_example(), width='24rem', height='18rem')

Customisation

You can register custom variants with NavBar.variant without editing the shared stylesheet. Below, reg.rule defines --column (42rem). Passing glass=True applies a translucent blurred background capped to --column. mountNavbar still measures the row and collapses <nav> on overflow. Scroll the preview to see content pass behind the bar:

reg.rule(':root { --column: 42rem; }', name='column', doc='The width of the content column.')

NavBar.variant('glass', r"""
background: oklch(from var(--n-2) l c h / .5); backdrop-filter: blur(16px) saturate(1.5);
max-inline-size: var(--column); margin: var(--space) auto 0; border: 1px solid var(--n-1); border-radius: var(--radius);
box-shadow: var(--floating);

""", doc='A translucent bar at the width of the content column.')

demo(Div(
    NavBar('Acme', A('About', href='#', aria_current='page'), A('Blog', href='#'), A('Contact', href='#'),
        end=Btn('Subscribe', variant='secondary'), sticky=True, glass=True, collapse=True),
    Stack(H1('Field notes'), Cluster(Badge('Design'), Badge('CSS', ui_tone='success'), Badge('FastHTML', ui_tone='warning'), cls='gap-2'),
        P('A short account of building a component library on the web platform, written as it happened.'),
        P('The first decision was to style native tags rather than invent new ones. Every form control the browser already has is already accessible, already keyboard-driven, and already known to assistive technology.'),
        P('The second was that colour should come from one hue. A single number sets the whole palette, and severity is a context rather than a variant.'),
        cls='mx-auto py-6 px-4', style='max-inline-size: var(--column)')),
    height='14rem')