---
description: Independent text, spacing, and radius scales, base element styles, surface
  relief tokens, prose typography, and optional IBM Plex fonts.
output-file: typography.html
title: Typography and density

---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

AnswerUI controls text size, spacing, and corner radius with three independent CSS custom properties: `--text`, `--space`, and `--radius`. Override any of them on `:root` for the whole page or on a `[ui]` container for one region. You can tighten `--space` in a dialog without shrinking `--text`. You can also enlarge `--text` in a reading pane without changing padding or corner radius.

```python
from answerui.preview import preview
```

## Scales and tokens

Steps with negative suffixes are smaller than the base. Steps with positive suffixes are larger. The stylesheet uses spacing tokens for internal control padding and gaps between layout items.

- Text (`--text: 1rem`): `--text-1` (`0.875`), `--text1` (`1.25`), `--text2` (`1.5`), `--text3` (`2`).
- Space (`--space: 1rem`): `--space-3` (`0.25`), `--space-2` (`0.5`), `--space-1` (`0.75`), `--space1` (`1.5`), `--space2` (`2`), `--space3` (`3`).
- Radius (`--radius: 0.5rem`): `--radius-1` (`0.5`), `--radius1` (`2`), plus the fixed pill radius `--radius-full` (`9999px`).

The token layer also defines font weights (`--weight: 400`, `--weight1: 600`), line heights (`--leading: 1.5`, `--leading-1: 1.25`), motion timings (`--motion: 150ms`, `--ease: cubic-bezier(.2, 0, 0, 1)`), and `@keyframes ui-spin`. Under `prefers-reduced-motion: reduce`, `--motion` becomes `0ms`.

Base tokens inherit to descendants. The stylesheet declares every derived step on all elements and pseudo-elements inside `:root` and `[ui]`. Overriding a base on a container recalculates every derived step inside that container. Set a base token to scale a whole region. Override a single derived step only for a one-off exception on one element, because derived steps are still computed from the base on every descendant.

## Base styles and scoping

Inside `:root` and `[ui]`, the reset sets `box-sizing: border-box` and clears default margins on block elements (`body`, headings, `p`, lists, `blockquote`, `pre`, `figure`, `figcaption`, and `hr`). Form controls inherit `font`. Media elements (`img`, `picture`, `video`, `canvas`, and `svg`) use `max-inline-size: 100%` and `block-size: auto`, except height-only images (`img[height]:not([width])`), which keep their HTML `height` attribute with `inline-size: auto` and `object-fit: contain`. With block margins cleared, layout attributes such as `[stack]` and `[grid]` control spacing. The reset does not remove native browser focus outlines.

The base layer sets `--color-background`, `--color-text`, and `--color-accent` on `:root` and `[ui]`. It sets `--weight`, `--text`, `--leading`, and `--font` on `body` and `[ui]`. Headings use `--font-heading` (defaulting to `inherit`), `--weight1`, `--leading-1`, and `text-wrap: balance`. Heading sizes use `--text3` for `h1`, `--text2` for `h2`, `--text1` for `h3`, and `--text` for `h4` through `h6`. Links use `--color-accent` with `text-underline-offset: .15em`. Code elements (`code`, `kbd`, `pre`, `samp`) use `--font-mono`. `small` uses `--text-1`. Icons matching `svg.ui-icon`, `svg[icon]`, or `svg:has(> use)` are sized to `1.25em` square. Keyboard-focused elements (`:focus-visible`) show `--ring` with a `2px` offset. Use `.ui-divider` for a `1px` top border in `--color-border`. Unmarked tables and form controls remain unstyled native elements.

Add `[ui]` to any container to override base scales, colour scheme, or hue locally without loading a second stylesheet. Wrap an opaque widget and its popup host in `[unstyle]` to stop AnswerUI selectors from matching inside it. Ordinary CSS inheritance still passes through `[unstyle]`. Add `[ui]` to an element inside `[unstyle]` to start a new AnswerUI scope.

## Surface relief and shadows

Raised controls use `--bevel` and `--raised`. Sunken controls such as inputs use `--bevel-in` and `--sunken`. Both bevels model top-left lighting. With `--bevel`, `--hi` is on the top and left edges and `--lo` is on the bottom and right. With `--bevel-in`, those edges are reversed.

`--hi` and `--lo` are computed by shifting the `oklch` lightness of `--face` (default `var(--color-surface)`) by `+0.16` and `-0.16`. Deriving rim colours from `--face` keeps highlights lighter and shadows darker in both light and dark schemes. Fixed palette steps cannot do that because their lightness order reverses between schemes. `--tint` is an 8% opacity tint derived from `currentColor`.

`--relief` (default `1`) scales the alpha of `--hi`, `--lo`, and `--shadow-color` without changing labels or borders. Setting `--relief: 0` hides bevels and control shadows while `--face` and borders stay visible. `--floating` uses `--floating-shadow`, which does not scale with `--relief`. Dialogs and menus still have drop shadows when `--relief` is `0`.

## Varying density by region

Both panels below use identical markup. The left panel uses the defaults. On the right panel, `--space` is `.75rem` and `--radius` is `.375rem`, while `--text` stays at `1rem`.

```python
def panel(density, sizes):
    return f'''
    <section ui style="{sizes}" card stack>
      <h3>{density}</h3>
      <p>Manage settings for this workspace.</p>
      <label stack class="gap-1">Display name
        <input input value="Ada Lovelace">
      </label>
      <div cluster><button type="button" btn>Save</button><a href="#help">Help</a></div>
    </section>
    '''

spacious = panel('Spacious', '')
compact = panel('Compact', '--space:.75rem;--radius:.375rem')
preview(f'<div grid>{spacious}{compact}</div>', height='17rem')
```

## Prose typography

Add `[prose]` to a container for long-form document typography covering headings, paragraphs, lists, links, blockquotes, code, `kbd`, `mark`, tables, media, `.lead`, and `hr`.

Vertical spacing uses `--sp`, computed as `calc(var(--prose-scale) * 1em)` (default `--prose-scale: .75`). Block elements use top margins for vertical spacing. Lists immediately following a paragraph (`p + ul`, `p + ol`) use a tighter `0.25 * --sp` top margin. Headings have larger top margins (`1.4 * --sp` for `h1` and `h2`, `1.2 * --sp` for `h3`, `1.25 * --sp` for `h4` through `h6`). The stylesheet also adjusts the top margin of the element right after each heading. Code blocks (`pre`) use `line-height: 1.25` without changing `--leading` or `--prose-leading`. Direct `:first-child` and `:last-child` elements have zero outer margin. Tables inside `[prose]` scroll horizontally and wrap cell text separately from the `[table]` component.

Prose font size is `calc(var(--text) * var(--prose-font-scale))` (default `--prose-font-scale: 1`). Line height uses `--prose-leading` (default `1.45`). Maximum width uses `--prose-measure` (default `65ch`). Four `[prose-size]` presets set `--prose-scale` and `--prose-font-scale` together: `sm` (`0.7`, `0.875`), `lg` (`0.8`, `1.125`), `xl` (`0.9`, `1.25`), and `2xl` (`1.1`, `1.5`). In a notebook or narrow pane, you can set `--prose-measure: none` and lower `--prose-leading` without affecting the surrounding UI.

```python
article = '''<h2>Notes in a narrow pane</h2>
<p>Override prose variables on a container to adjust reading width and line spacing:</p>
<ul><li>Limit line width with <code>--prose-measure</code>.</li><li>Set <code>--prose-measure: none</code> for full-width panes.</li></ul>
<pre><code>parts = walk(workspace)
print(len(parts))</code></pre>
<p>The last block has no bottom margin.</p>'''
preview(f'''<div grid style="--grid-min:17rem">
  <article card prose style="--prose-measure:28ch">{article}</article>
  <article card prose style="--text:.875rem;--prose-leading:1.3;--prose-measure:none">{article}</article>
</div>''', height='23rem')
```

## Optional fonts

By default, `--font` uses `system-ui, sans-serif` and `--font-mono` uses `ui-monospace, Menlo, Consolas, monospace`. To use IBM Plex, load `fonts.css` and set `--font: 'IBM Plex Sans', system-ui, sans-serif` and `--font-mono: 'IBM Plex Mono', ui-monospace, monospace`. `fonts.css` declares `IBM Plex Sans` (400 normal, 400 italic, 600 normal), `IBM Plex Serif` (600 normal), and `IBM Plex Mono` (400 normal). Set `--font-heading` (for example, `'IBM Plex Serif', serif`) on `:root` or a `[ui]` region to use Plex Serif for headings. Layout and spacing scales do not depend on `fonts.css`.
