Code Block
Display formatted code snippets with a language label and a copy action.
Default
A standard code block with language label and copy button.
function greet(name) {
return `Hello, ${name}!`;
}
const message = greet("World");
console.log(message);
<div class="vt-code-block">
<div class="vt-code-header">
<span class="vt-code-header-title">javascript</span>
<button type="button" class="vt-btn vt-btn-ghost vt-btn-icon vt-btn-sm" data-code-copy="" aria-label="Copy code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" class="vt-w-4 vt-h-4" aria-hidden="true">
<path d="M7 11V9C7 5.70017 7 4.05025 8.02513 3.02513C9.05025 2 10.7002 2 14 2C17.2998 2 18.9497 2 19.9749 3.02513C21 4.05025 21 5.70017 21 9V11C21 14.2998 21 15.9497 19.9749 16.9749C18.9497 18 17.2998 18 14 18C10.7002 18 9.05025 18 8.02513 16.9749C7 15.9497 7 14.2998 7 11Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path><path d="M3 6V15C3 18.2998 3 19.9497 4.02513 20.9749C5.05025 22 6.70017 22 10 22H17" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path>
</svg></button>
</div>
<pre class="vt-code-block-body"><code>function greet(name) {
return `Hello, ${name}!`;
}
const message = greet("World");
console.log(message);</code></pre>
</div>
With Line Numbers
1interface User {2 id: string;3 name: string;4 email: string;5 role: "admin" | "user";6}78function getUser(id: string): User {9 return fetchUser(id);10}
<div class="vt-code-block">
<div class="vt-code-header">
<span class="vt-code-header-title">typescript</span>
<button type="button" class="vt-btn vt-btn-ghost vt-btn-icon vt-btn-sm" data-code-copy="" aria-label="Copy code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" class="vt-w-4 vt-h-4" aria-hidden="true">
<path d="M7 11V9C7 5.70017 7 4.05025 8.02513 3.02513C9.05025 2 10.7002 2 14 2C17.2998 2 18.9497 2 19.9749 3.02513C21 4.05025 21 5.70017 21 9V11C21 14.2998 21 15.9497 19.9749 16.9749C18.9497 18 17.2998 18 14 18C10.7002 18 9.05025 18 8.02513 16.9749C7 15.9497 7 14.2998 7 11Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path><path d="M3 6V15C3 18.2998 3 19.9497 4.02513 20.9749C5.05025 22 6.70017 22 10 22H17" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path>
</svg></button>
</div>
<pre class="vt-code-block-body"><code><span class="vt-code-block-line"><span class="vt-code-block-line-number">1</span>interface User {</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">2</span> id: string;</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">3</span> name: string;</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">4</span> email: string;</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">5</span> role: "admin" | "user";</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">6</span>}</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">7</span></span><span class="vt-code-block-line"><span class="vt-code-block-line-number">8</span>function getUser(id: string): User {</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">9</span> return fetchUser(id);</span><span class="vt-code-block-line"><span class="vt-code-block-line-number">10</span>}</span></code></pre>
</div>
With Highlighted Lines
Highlight specific lines to draw attention to important code.
const app = express(); app.get("/api/users", (req, res) => { const users = db.getUsers(); res.json(users);}); app.listen(3000);
<div class="vt-code-block">
<div class="vt-code-header">
<span class="vt-code-header-title">javascript</span>
<button type="button" class="vt-btn vt-btn-ghost vt-btn-icon vt-btn-sm" data-code-copy="" aria-label="Copy code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" class="vt-w-4 vt-h-4" aria-hidden="true">
<path d="M7 11V9C7 5.70017 7 4.05025 8.02513 3.02513C9.05025 2 10.7002 2 14 2C17.2998 2 18.9497 2 19.9749 3.02513C21 4.05025 21 5.70017 21 9V11C21 14.2998 21 15.9497 19.9749 16.9749C18.9497 18 17.2998 18 14 18C10.7002 18 9.05025 18 8.02513 16.9749C7 15.9497 7 14.2998 7 11Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path><path d="M3 6V15C3 18.2998 3 19.9497 4.02513 20.9749C5.05025 22 6.70017 22 10 22H17" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path>
</svg></button>
</div>
<pre class="vt-code-block-body"><code><span class="vt-code-block-line">const app = express();</span><span class="vt-code-block-line"> </span><span class="vt-code-block-line vt-code-block-line-highlight">app.get("/api/users", (req, res) => {</span><span class="vt-code-block-line vt-code-block-line-highlight"> const users = db.getUsers();</span><span class="vt-code-block-line vt-code-block-line-highlight"> res.json(users);</span><span class="vt-code-block-line">});</span><span class="vt-code-block-line"> </span><span class="vt-code-block-line">app.listen(3000);</span></code></pre>
</div>
Inline Code
Use inline code for short code references within text.
Use const for variables that won't be reassigned, and
let for variables that will change.
Import components with import Button from './Button' to use them in your templates.
The function returns Promise<User[]> which resolves to an array of user objects.
You can handle errors using try...catch blocks or the
.catch() method.
Run npm install to install dependencies, then
npm run dev to start the development server on
localhost:3000.
<div class="vt-space-y-4 vt-body-sm vt-text-foreground-primary">
<p>
Use <code class="vt-code-inline">const</code> for variables that won't be reassigned, and
<code class="vt-code-inline">let</code> for variables that will change.
</p>
<p>
Import components with <code class="vt-code-inline">import Button from './Button'</code> to use them in your templates.
</p>
<p>
The function returns <code class="vt-code-inline">Promise<User[]></code> which resolves to an array of user objects.
You can handle errors using <code class="vt-code-inline">try...catch</code> blocks or the
<code class="vt-code-inline">.catch()</code> method.
</p>
<p>
Run <code class="vt-code-inline">npm install</code> to install dependencies, then
<code class="vt-code-inline">npm run dev</code> to start the development server on
<code class="vt-code-inline">localhost:3000</code>.
</p>
</div>
Inline Code Sizes
vt-code-inline matches 14px body text; vt-code-inline-sm (Code/SM) matches 12px text — captions, table cells, API tables — so the fragment never outgrows its line.
Set data-theme="my-theme" on <html> to switch themes.
Set data-theme="my-theme" on <html> to switch themes.
<div class="vt-space-y-4">
<p class="vt-body-sm vt-text-foreground-primary">
Set <code class="vt-code-inline">data-theme="my-theme"</code> on <code class="vt-code-inline"><html></code> to switch themes.
</p>
<p class="vt-caption">
Set <code class="vt-code-inline vt-code-inline-sm">data-theme="my-theme"</code> on <code class="vt-code-inline vt-code-inline-sm"><html></code> to switch themes.
</p>
</div>
With Tabs
Switch between multiple languages or file variants.
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10));function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10));def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10))
<div class="vt-code-block" data-vt-tab-group="code-files">
<div class="vt-code-header">
<div class="vt-tabs-list vt-tabs-list-sm" role="tablist" aria-label="Language">
<button type="button" class="vt-tabs-trigger vt-active" role="tab" data-vt-tab="js" aria-selected="true" tabindex="0">JavaScript</button><button type="button" class="vt-tabs-trigger" role="tab" data-vt-tab="ts" aria-selected="false" tabindex="-1">TypeScript</button><button type="button" class="vt-tabs-trigger" role="tab" data-vt-tab="py" aria-selected="false" tabindex="-1">Python</button>
</div>
<button type="button" class="vt-btn vt-btn-ghost vt-btn-icon vt-btn-sm" data-code-copy="" aria-label="Copy code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" class="vt-w-4 vt-h-4" aria-hidden="true">
<path d="M7 11V9C7 5.70017 7 4.05025 8.02513 3.02513C9.05025 2 10.7002 2 14 2C17.2998 2 18.9497 2 19.9749 3.02513C21 4.05025 21 5.70017 21 9V11C21 14.2998 21 15.9497 19.9749 16.9749C18.9497 18 17.2998 18 14 18C10.7002 18 9.05025 18 8.02513 16.9749C7 15.9497 7 14.2998 7 11Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path><path d="M3 6V15C3 18.2998 3 19.9497 4.02513 20.9749C5.05025 22 6.70017 22 10 22H17" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path>
</svg></button>
</div>
<pre class="vt-code-block-body" role="tabpanel" data-vt-panel="js"><code>function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10));</code></pre><pre class="vt-code-block-body vt-hidden" role="tabpanel" data-vt-panel="ts"><code>function fibonacci(n: number): number {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10));</code></pre><pre class="vt-code-block-body vt-hidden" role="tabpanel" data-vt-panel="py"><code>def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
print(fibonacci(10))</code></pre>
</div>
Terminal
Terminal-style code block for CLI commands and output.
$ npm install
added 156 packages in 3.2s
$ npm run build
> my-app@1.0.0 build
> astro build
generating static routes
▶ src/pages/index.astro
▶ src/pages/about.astro
▶ src/pages/components/button.astro
✓ Completed in 1.24s
build complete!
<div class="vt-code-block">
<div class="vt-code-header">
<div class="vt-code-block-dots" aria-hidden="true"><span></span><span></span><span></span></div>
<span class="vt-code-header-title">terminal</span>
<button type="button" class="vt-btn vt-btn-ghost vt-btn-icon vt-btn-sm" data-code-copy="" aria-label="Copy code"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="none" class="vt-w-4 vt-h-4" aria-hidden="true">
<path d="M7 11V9C7 5.70017 7 4.05025 8.02513 3.02513C9.05025 2 10.7002 2 14 2C17.2998 2 18.9497 2 19.9749 3.02513C21 4.05025 21 5.70017 21 9V11C21 14.2998 21 15.9497 19.9749 16.9749C18.9497 18 17.2998 18 14 18C10.7002 18 9.05025 18 8.02513 16.9749C7 15.9497 7 14.2998 7 11Z" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path><path d="M3 6V15C3 18.2998 3 19.9497 4.02513 20.9749C5.05025 22 6.70017 22 10 22H17" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"></path>
</svg></button>
</div>
<pre class="vt-code-block-body"><code><span class="vt-code-block-prompt">$ </span>npm install
<span class="vt-code-block-comment">added 156 packages in 3.2s</span>
<span class="vt-code-block-prompt">$ </span>npm run build
<span class="vt-code-block-comment">> my-app@1.0.0 build
> astro build
generating static routes
▶ src/pages/index.astro
▶ src/pages/about.astro
▶ src/pages/components/button.astro
✓ Completed in 1.24s
build complete!</span></code></pre>
</div>
Guidelines
function greet(name) {
return `Hello, ${name}!`;
}
const message = greet("World");
console.log(message);
Run const sum = a + b; const product = a * b; return sum + product; to compute the result.
Use const for values that never change, and let for ones that do.
const
API Reference
| Class | Type | Description |
|---|---|---|
vt-code-block |
Base | The block — a subtle tint, radius md; header and body stacked |
vt-code-header |
Component | Bar above the code, 40px plus the divider under it — a title and a copy button, or a tab list |
vt-code-header-title |
Part | Language or file name — Caption, muted |
vt-btnvt-btn-ghostvt-btn-iconvt-btn-sm |
Part | Copy action (aria-label="Copy code", data-code-copy) |
vt-tabs-listvt-tabs-list-sm |
Part | Tab list in the header for several files (data-vt-tab-group on the block, role="tabpanel" bodies) |
vt-code-block-body |
Part | The <pre> — Code/MD, 16px padding, scrolls sideways instead of wrapping |
vt-code-block-comment |
Part | A comment or command output, muted |
vt-code-block-prompt |
Part | A shell prompt ($), brand color, not selectable |
vt-code-block-line |
Part | One line as a full-width row — needed for highlighting and numbers |
vt-code-block-line-highlight |
Modifier | Highlighted line — brand tint with a brand edge |
vt-code-block-line-number |
Part | Line number before a line — muted, right-aligned, divided from the code, not selectable |
vt-code-block-dots |
Part | Three window dots of a terminal header (three empty spans) |
vt-code-inline |
Component | Inline code fragment inside prose — Code/MD on the subtle surface |
vt-code-inline-sm |
Size | Small inline code for 12px text (captions, table cells, API tables) — Code/SM, 2px × 4px padding |
Accessibility
| Feature | Details |
|---|---|
| Semantics | Code lives in pre + code elements so screen readers announce it as code |
| Copy action | The copy button carries an aria-label and announces success via its label swap |
| Tabs | File tabs are role="tab" buttons with aria-selected; arrow keys move between them |
| Scroll | Long code scrolls inside the block — the page never scrolls horizontally |