Code
Styled inline code, keyboard shortcuts, code blocks, and semantic text elements. Semetrika styles the native HTML elements <code>, <pre>, <kbd>, <samp>, and <var> directly so any CMS or hand-authored content renders consistently without classes. Syntax highlighting token hooks are exposed so a consumer can plug in their own highlighter theme.
Usage
When to use
- For inline references to code, file names, commands, or identifiers inside prose (
<code>) - For multi-line code blocks with preserved whitespace (
<pre>+<code>) - For keyboard shortcuts and key names (
<kbd>) - For sample program output (
<samp>) - For variable names or placeholders (
<var>)
When not to use
- For highlighted prose that isn't code - use
<mark>or<strong>instead - As a generic monospace wrapper - use the monospace token directly on a styled element
Do
- Use real semantic elements - assistive tech depends on them
- Wrap multi-line code in
<pre><code>...</code></pre>for correct semantics and whitespace preservation - Let the consumer supply a syntax highlighter and wire its palette into the
--code-*-colortokens
Don't
- Don't use
<code>purely for visual styling of non-code text - Don't wrap
<kbd>around key combinations without nesting -<kbd><kbd>Ctrl</kbd>+<kbd>C</kbd></kbd>is correct
Sizing
The elements render at one size on their own. A size word applies through an opt-in code wrapper, which sets --code-font-size for everything inside it:
<div class="code lg">
<p>Run <code>yarn build</code> before deploying.</p>
<pre><code>yarn build --verbose</code></pre>
</div>
Every <code>, <pre>, <kbd>, <samp> and <var> in the wrapper follows the tier. The wrapper is also where the classes and attributes passthrough props land.
API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
inline | string | - | Inline code shown inside a paragraph (<code>). |
block | string | - | Multi-line code inside a pre element (<pre><code>). |
key | string | - | Keyboard shortcut text (<kbd>). |
output | string | - | Sample program output text (<samp>). |
variable | string | - | Variable identifier (<var>). |
classes | array | - | Additional CSS class words merged into the root element. |
attributes | object | - | Additional HTML attributes spread on the root element. |
Classes
| Class | Capability | Default | Description |
|---|---|---|---|
.sm | Size | - | Small. |
.md | Size | Yes | Medium. |
.lg | Size | - | Large. |
Examples
Inline code
Inline <code> inside a paragraph.
Block code
<pre> block with horizontal scrolling for long lines.
Keyboard shortcut
<kbd> styled as a pill.
Sample output
<samp> styled as program output.
Variable
<var> styled for variable names.
Highlight JS
A block highlighted by highlight.js, whose hljs-* classes read the --code-{token}-color palette.
Prism JS
The same block highlighted by Prism, whose .token classes read the same palette, so either highlighter renders in the design system's colors.
Accessibility
- WCAG 2.2 AA
- All elements are semantic - no additional ARIA required.
- Block code is scrollable via keyboard when it overflows horizontally.
- Color contrast meets AA for text on background in both light and dark themes.
Related
- deflist - classless typography counterpart for key-value lists
- Typography - monospace font family token (
--font-family-mono)