surfaces
Cards for grouping content, badges for short labels, and avatars for people and teams
Card wraps content in a bordered section styled with the shared colour, spacing, and radius tokens. Badge renders an inline text label for a category, count, or status. Avatar renders a circular image or initials for a person or team.
from fasthtml.common import Article, Header, Footer, Small, to_xml
from answerui.layout import Grid
from answerui.controls import Btn
from answerui.demo import demo
from urllib.parse import quote
Card
Add the card attribute to any element for a --color-surface background, a 1px --color-border, --space padding, --radius corners, and a --raised shadow. [card] uses no bevels so cards do not look clickable. Card(...) returns a <section card> element and uses no JavaScript.
[card] sets no child layout. Pass stack=True for a vertical stack, or nest another layout inside the card.
Card
def Card(
*c, **attrs
):
A bordered section with no prescribed layout
Inner padding comes from [card]. Add stack=True for spacing between children, and use a surrounding layout such as Grid or Stack to space cards apart.
Card.demo()
Composition
You can use any HTML structure inside a card without dedicated header, body, or footer constructors. Below, Grid arranges two cards side by side when the container is wide enough. The second card is an Article with card=True and stack=True directly, using stack=True on its Header and cluster=True on its Footer.
demo(Grid(
_card_example(),
Article(
Header(H3('Field notes'), Small('Updated today'), stack=True, cls='gap-1'),
P('Observations and ideas from the team, ready for the next meeting.'),
Footer(Btn('Open project'), Btn('Archive', variant='secondary'), cluster=True),
card=True, stack=True)))
Customisation
Overriding --radius or --space changes card corners and padding across a region. To restyle all cards without affecting other components, add a rule such as [card] { padding: var(--space1); } in @layer app. For a single card, pass utility classes such as cls='gap-2 p-2' (or inline style for custom values):
to_xml(Card(P('A compact note'), P('Another line'), stack=True, cls='gap-2 p-2'))
'<section card stack class="gap-2 p-2">\n <p>A compact note</p>\n <p>Another line</p>\n</section>\n'
You can combine card=True with any layout constructor, such as Grid(..., card=True), without adding a wrapper div. Plain article, section, and div elements without card have no card border or background.
Badge
Badge renders an inline <span> with a soft --v-3 background and --v2 text rather than the solid, raised fill used on buttons. Pass ui_tone (such as 'success', 'warning', 'danger', or 'info') to change its hue, or variant='secondary' for neutral --color-surface and --color-text styling with a 1px inset --color-border.
Badge
def Badge(
*c, variant=None, **attrs
):
A small text label, optionally coloured with ui_tone
Badges use em padding (.125em .5em) at --text-1 size so padding scales with the text. Put the meaning in the badge text and use colour only to reinforce it. [badge] is display-only, with no interaction or ARIA live-region role.
Badge.demo()
Badges have no outer margin. Below, Cluster places a badge beside the heading while Card(..., stack=True) spaces the rows apart. Pass counts as ordinary text children rather than CSS-generated content:
demo(Card(
Cluster(H3('Field notes'), Badge('Shared'), cls='gap-2'),
P('Observations and ideas from the team.'),
Cluster(Span('Unread notes'), Badge(3, variant='secondary'), cls='gap-2'), stack=True))
Avatar
Avatar takes a name and an optional image URL in src. With src, it renders an <img> with alt=name. Without src, it renders a <span role="img"> showing the first and last initials (or ? if name is empty) with aria-label set to name (or 'Unknown'). Both forms share the same circular frame controlled by --avatar-size (default 2.5rem), with initials sized to 40% of --avatar-size.
Avatar
def Avatar(
name, src=None, **attrs
):
A circular image or initials identifying a person or team
Avatar.demo()
def _picture(name): return f'https://api.dicebear.com/10.x/miniavs/svg?seed={quote(name)}&backgroundColor=ffe3ea'
demo(Cluster(Avatar('Ada Lovelace', src=_picture('Ada Lovelace'), aria_hidden='true'),
Stack(Span('Ada Lovelace'), Small('Workspace owner'), cls='gap-1')))
To make an avatar clickable, place it as the only child of a Btn. The [btn]:has(> [avatar]:only-child) rule sets 1px padding, border-radius: var(--radius-full), --face: var(--color-surface), and --color-border on the button without overriding the avatar's colours. When the button is disabled, the avatar has opacity: .5.
demo(Cluster(Btn(Avatar('Ada Lovelace')), Btn(Avatar('Grace Hopper', src=_picture('Grace Hopper'))),
Btn(Avatar('Alan Turing', src=_picture('Alan Turing')), disabled=True)))
Inset
Inset renders a left-bordered panel ([inset]) for conditional form fields (such as Checkbox(reveals=...) targets or Svelte {#if} panels) and callout notes. Pass ui_tone to tint the rail.
Inset
def Inset(
*c, **attrs
):
A left-bordered panel for conditional form fields or inset callout text
Inset.demo()
Disclosure and Accordion
Disclosure wraps native <details disclosure> with <summary> and a padded body <div>. Group multiple Disclosure elements inside Accordion (using the same name attribute for exclusive open).
Accordion
def Accordion(
*c, **attrs
):
A stacked group of Disclosure elements
Disclosure
def Disclosure(
summary, *c, **attrs
):
A native collapsible <details> disclosure with <summary>
Accordion.demo()