Expressive Code is an Astro integration that replaces plain Shiki code blocks with styled ones: editor and terminal frames, file name tabs, marked and inserted or deleted lines, word wrap and a copy button. Install it with npx astro add astro-expressive-code, keep it before mdx() in integrations, and control each block with meta options such as title="src/pages/index.astro" or {2-3}.
Key Takeaways
astro-expressive-codehandles highlighting, frames, markers and copy buttons in one integration.- It must come before
@astrojs/mdxin theintegrationsarray.- Per-block options go in the code fence meta:
title,frame,{1,3},ins,del,wrap.- Starlight uses Expressive Code by default, so the syntax is the same there.
Install it
npx astro add astro-expressive-code
The command installs the package and adds it to your config. Check the order:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import expressiveCode from 'astro-expressive-code';
import mdx from '@astrojs/mdx';
export default defineConfig({
integrations: [
expressiveCode({
themes: ['github-dark', 'github-light'],
}),
mdx(),
],
});
Expressive Code must run before MDX so it processes code blocks in .mdx files. It does its own highlighting (with Shiki under the hood) and styling, so markdown.shikiConfig no longer applies to Markdown code blocks. Expressive Code hooks into Astro’s Markdown pipeline, so check its README for the Astro versions and Markdown processor each release supports before upgrading Astro.
Frames and titles
Add a title to get an editor tab:
```astro title="src/components/Card.astro"
---
const { title } = Astro.props;
---
<h2>{title}</h2>
```
Shell languages (bash, sh, powershell) get a terminal frame automatically. Override it with frame="terminal", frame="code" or frame="none". A file-path comment on the first line, like // src/pages/index.astro, is also picked up as the title and removed from the code, so existing snippets in that style get tabs for free.
Mark lines and text
```js {2} ins={5} del={4} "defineConfig"
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({});
export default defineConfig({ integrations: [sitemap()] });
```
{2}ormark={2}marks a line as highlighted. Ranges work:{1,3-5}.ins={5}anddel={4}mark inserted and deleted lines with green and red backgrounds and+/-indicators.- A quoted string like
"defineConfig"marks every occurrence of that text;/regex/works too. - Add labels to ranges:
{"1":2-3}shows a “1” badge you can refer to in the prose.
For long lines, wrap turns on word wrap for one block, and showLineNumbers works once you add the line numbers plugin (@expressive-code/plugin-line-numbers).
Configure it globally
For more than a few options, move the config to ec.config.mjs in the project root, which also lets the <Code> component from astro-expressive-code/components read it:
// ec.config.mjs
import { defineEcConfig } from 'astro-expressive-code';
export default defineEcConfig({
themes: ['github-dark', 'github-light'],
styleOverrides: {
borderRadius: '0.5rem',
codeFontFamily: 'var(--font-mono)',
},
defaultProps: {
wrap: true,
},
});
With two themes, Expressive Code follows prefers-color-scheme by default and also respects a data-theme attribute on <html>; the themeCssSelector option lets you match a different dark mode toggle. Theme colors become CSS variables, so the page doesn’t need JavaScript to switch.
Expressive Code or plain Shiki?
| Need | Plain Shiki | Expressive Code |
|---|---|---|
| Syntax colors | Yes | Yes |
| Line highlighting | With transformers + your CSS | Built in |
| File titles and frames | Build it yourself | Built in |
| Copy button | Build it yourself | Built in (small script) |
| Extra CSS and JS | None | Small stylesheet and copy script |
Choose Expressive Code for docs and tutorials with many annotated snippets. Stay with Shiki and a couple of transformers if you only need occasional highlights and want zero extra assets. The Expressive Code docs list every option, and our Shiki guide covers the built-in route. Expressive Code is common in documentation themes and Starlight sites.
FAQ
Why is Expressive Code not styling my MDX code blocks?
The integration order is wrong. expressiveCode() must come before mdx() in the integrations array, so it registers its processing before MDX compiles your files.
Does Expressive Code work with Starlight?
Yes. Starlight includes Expressive Code by default. Configure it with the expressiveCode option in the Starlight integration or an ec.config.mjs file, rather than adding the integration a second time.
Does Expressive Code add JavaScript to the page?
Only a small script for the copy button. Highlighting, frames and markers are rendered at build time as HTML and CSS.