To highlight specific lines in Astro code blocks, add Shiki transformers from @shikijs/transformers to markdown.shikiConfig.transformers. transformerMetaHighlight() reads line numbers from the fence (```js {2,4-5}), transformerNotationHighlight() reads // [!code highlight] comments, and both add a highlighted class to the marked lines that you style with CSS.
Key Takeaways
- Astro’s built-in Shiki doesn’t highlight lines on its own; transformers add it.
- Use meta ranges like
{1,3-5}when you don’t want markers inside the code.- Use notation comments (
[!code highlight],[!code ++],[!code focus]) when lines move around.- Transformers only add classes, so you write the CSS.
Install and configure the transformers
npm install @shikijs/transformers
// astro.config.mjs
import { defineConfig } from 'astro/config';
import {
transformerMetaHighlight,
transformerNotationHighlight,
transformerNotationDiff,
transformerNotationFocus,
} from '@shikijs/transformers';
export default defineConfig({
markdown: {
shikiConfig: {
theme: 'github-dark',
transformers: [
transformerMetaHighlight(),
transformerNotationHighlight(),
transformerNotationDiff(),
transformerNotationFocus(),
],
},
},
});
Install the @shikijs/transformers version that matches the Shiki major Astro ships (check with npm ls shiki). shikiConfig works with both the default Sätteri processor and unified(), and Astro passes the code fence’s meta string through to Shiki, so the meta syntax works in .md and .mdx.
Highlight by line number
Put the line numbers in braces after the language:
```js {2,4-5}
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
export default defineConfig({
integrations: [sitemap()],
});
```
Lines 2, 4 and 5 get class="line highlighted", and the <pre> gets has-highlighted. Ranges and single numbers can be mixed. The downside: edit the snippet and the numbers drift.
Highlight with comments
Notation comments mark lines in place and are removed from the output:
```js
export default defineConfig({
site: 'https://www.example.com', // [!code highlight]
integrations: [sitemap()],
});
```
transformerNotationDiff() adds diff add and diff remove classes with // [!code ++] and // [!code --], and transformerNotationFocus() adds focused with // [!code focus] (plus has-focused on the <pre>). Use the comment syntax of the language: # [!code highlight] in YAML or bash, <!-- [!code highlight] --> in HTML.
Style the highlighted lines
/* src/styles/global.css */
.astro-code code { display: grid; }
.astro-code .line { padding-inline: 1rem; }
.astro-code .line.highlighted { background: rgb(255 255 255 / 0.08); box-shadow: inset 3px 0 #facc15; }
.astro-code .line.diff.add { background: rgb(34 197 94 / 0.15); }
.astro-code .line.diff.remove { background: rgb(239 68 68 / 0.15); opacity: 0.7; }
.astro-code.has-focused .line:not(.focused) { filter: blur(1px); opacity: 0.5; transition: 0.2s; }
.astro-code.has-focused:hover .line { filter: none; opacity: 1; }
display: grid on code makes each line stretch to the full width, so the background covers the whole row instead of just the text. If you use light and dark Shiki themes, pick highlight colors that work on both, or scope them under your dark mode selector; the Shiki dual themes tip shows the setup.
Diffs also work without a transformer: a ```diff block colors + and - lines, and Astro keeps the markers out of copied text. That’s handy for short before-and-after snippets, though you lose syntax colors for the underlying language.
For titles, frames and line highlighting with no CSS to write, Expressive Code is the all-in-one alternative. Our Shiki syntax highlighting guide covers themes and languages, and the copy code button guide adds a copy button that works with this markup. The Shiki transformers reference lists every transformer. Many documentation themes already ship styles for these classes.
FAQ
How do I highlight a line in an Astro Markdown code block?
Install @shikijs/transformers, add transformerMetaHighlight() to markdown.shikiConfig.transformers, then write the line numbers after the language, like ```js {3}. Add CSS for .line.highlighted.
Does line highlighting work with the Astro Code component?
Yes. Pass the transformers to the component’s transformers prop and the meta string to its meta prop, for example <Code code={src} lang="js" meta="{2}" transformers={[transformerMetaHighlight()]} />.
Why do my highlighted lines only color the text width?
Each line is an inline span. Set display: grid (or display: block on .line) so lines fill the full width of the code block, and the background spans the row.