answerui
Styles and browser behavior for semantic HTML, with FastHTML components
Use the @answerdotai/answerui npm package for the browser foundation: a stylesheet keyed on semantic HTML attributes and CSS custom properties, optional fonts, and vanilla JavaScript modules. Use the answerui-py Python package to add FastHTML constructors and a registry for app-specific CSS and browser behavior on top of those same assets.
Python constructors emit marked HTML, such as <section card> from Card() and <button btn> from Btn(). AnswerUI does not restyle unmarked form elements. Frontend frameworks such as Svelte can emit the same markup and import the JavaScript modules directly without Python. The notebooks cover the generated OKLCH palette, typography and spacing scales, parent-driven layouts, UI components, and the Python registry.
Read answerui.skill with doc(answerui.skill) for an LLM guide to building FastHTML pages, inspecting and customising registered components, and previewing results in a persistent Python kernel.
Install
For a JavaScript app:
npm install @answerdotai/answerui
Import the stylesheet once:
import '@answerdotai/answerui/styles.css';
import {mountPopover} from '@answerdotai/answerui/floating.js';
@answerdotai/answerui/styles.css works without JavaScript, including native dialogs, disclosures, and CSS tooltips. Import individual browser modules only for components that require JavaScript: @answerdotai/answerui/floating.js for popovers, @answerdotai/answerui/fit.js for responsive navbars, @answerdotai/answerui/interactions.js for tabs, toolbars, and hover cards, and @answerdotai/answerui/copy.js for clipboard actions. Load @answerdotai/answerui/fonts.css to use the bundled IBM Plex font faces.
For server-rendered or static HTML (with HTMX 4 for swap-driven behaviors such as dialog auto-open), import '@answerdotai/answerui/browser.js' mounts those behaviors automatically on marked elements and observes later DOM insertions. In Svelte or another component framework, import the individual modules inside your component lifecycle instead of loading @answerdotai/answerui/browser.js.
For FastHTML (requires python-fasthtml>=0.15 / HTMX 4):
pip install answerui-py
If you serve the browser assets directly from Python, from answerui import assets returns the packaged Path to the assets directory.
Use from Python
reg.hdrs() returns the shared foundation stylesheet, browser scripts, and registered app rules. reg.ftrs() returns the fastlucide icon sprite sheet. Because FastHTML evaluates both on every response, newly registered rules take effect on the next request without a restart:
from fasthtml.common import *
from answerui.all import *
app = FastHTML(hdrs=reg.hdrs(), ftrs=reg.ftrs())
rt = app.route
To serve cached assets instead of inlining them on every page, create the app without reg.hdrs() or reg.ftrs(), then call reg.serve(app). That call registers the headers, icon sprite sheet, and content-hashed asset routes while preserving relative imports between the browser JavaScript modules.
served_app = FastHTML()
reg.serve(served_app);
Each Python constructor renders standard HTML with a semantic attribute. Below, Card renders <section card> and Stack wraps the children in <div stack> to space them vertically:
page = Card(Stack(H2('Profile'), Field('Name', Input(name='name')), Cluster(Btn('Save'), Btn('Cancel', variant='secondary'))))
demo(page)
demo(page) renders the Python markup with the same stylesheet and browser scripts used by JavaScript applications.
Customise
Register standard CSS at the scope you want to change: :root custom properties via reg.rule for the whole app, a scoped selector for one region, or a component rule via Btn.rule. Because registered rules are placed in the app cascade layer after every framework layer except utilities, they override AnswerUI defaults without modifying the package:
reg.rule(':root { --hue: 150; --radius: 0; }', 'theme')
Btn.rule('caps', 'text-transform: uppercase;')
Btn.caps [app] [btn]
Call .variant() on a component to declare a named style modifier. The component constructor then accepts that variant name as a keyword argument. Declared variants also appear in the component's documentation:
Btn.variant('cta', '--ui-pad-block: var(--space); --ui-pad-inline: var(--space2); font-size: var(--text1);', doc='The landing page call to action.')
Btn.cta [app] :is([btn])[cta]: The landing page call to action.
Declaring a variant registers an app-layer rule for that attribute on the component element without modifying the default styles in styles.css.
demo(Btn('Request access', cta=True))
Btn
Btn(*c, variant='primary', size=None, href=None, type='button', icon=False, **attrs)
A raised action, latched by aria-pressed and sunken while busy or expanded
Root selector: [btn].
Declared variants:
cta: The landing page call to action.
Rules:
base(app)caps(app)
Previews in notebooks
demo(page) returns a Preview with two MIME representations: an HTML iframe containing the full stylesheet and browser scripts, and a Markdown block of the HTML markup. Because each iframe embeds the entire stylesheet, committing them would bloat notebooks and churn diffs on every stylesheet edit. Preview._repr_mimebundle_ sets nbdev's strip metadata flag on the HTML output. When you run nbdev-clean, it strips the iframe and keeps the Markdown markup in git. In a fresh clone, run the notebook cells to display the iframes again.
For the docs, set exec_all in pyproject.toml:
[tool.nbdev]
exec_all = true
nbdev-docs then executes every notebook and renders each iframe from the current code.
With an LLM
answerui.skill is a pyskill whose module docstring documents how to use AnswerUI in a Python kernel. Run list_pyskills() to discover it, doc(answerui.skill) to read the guide, and doc(Btn) to inspect a component's signature, declared variants, and CSS rules.