🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Content & MDX••By Komal Nakrani

Astro Shiki Dark Mode: Light and Dark Code Themes

Set up Astro Shiki dark mode: configure light and dark themes in shikiConfig, switch them with prefers-color-scheme or a class toggle, and style the Code component.

#Astro#Shiki#Dark Mode#Syntax Highlighting

For Shiki dark mode in Astro, set markdown.shikiConfig.themes to a light and a dark theme. Astro renders code blocks with the light colors inline and the dark colors as --shiki-dark CSS variables, and a few lines of CSS switch to them under prefers-color-scheme: dark or a .dark class on <html>. No JavaScript runs to recolor the code.

Key Takeaways

  • Use themes: { light, dark } instead of theme in markdown.shikiConfig.
  • Shiki writes both palettes into the HTML; CSS decides which one shows.
  • Switch with a media query, a class, or both, depending on how your site’s dark mode works.
  • The <Code /> component takes the same themes prop for code outside Markdown.

Configure two themes

// astro.config.mjs
import { defineConfig } from 'astro/config';

export default defineConfig({
  markdown: {
    shikiConfig: {
      themes: {
        light: 'github-light',
        dark: 'github-dark',
      },
    },
  },
});

shikiConfig applies whichever Markdown processor you use, Sätteri or unified(), and MDX picks it up too. Any of Shiki’s bundled themes work; pairs that come in light and dark variants, such as github-light/github-dark, catppuccin-latte/catppuccin-mocha or min-light/min-dark, look the most consistent.

Each token now renders like this:

<span style="color:#D73A49;--shiki-dark:#F97583">const</span>

The light color is the default, and the dark one waits in a CSS variable. The <pre> gets --shiki-dark-bg for the background as well.

Switch with the system setting

If your site follows the operating system’s color scheme, a media query is all you need:

/* src/styles/global.css */
@media (prefers-color-scheme: dark) {
  .astro-code,
  .astro-code span {
    color: var(--shiki-dark) !important;
    background-color: var(--shiki-dark-bg) !important;
    font-style: var(--shiki-dark-font-style) !important;
    font-weight: var(--shiki-dark-font-weight) !important;
    text-decoration: var(--shiki-dark-text-decoration) !important;
  }
}

The !important is needed because the light colors are inline styles. Astro renames Shiki’s shiki class to astro-code, so target that.

Switch with a dark mode toggle

Sites with a manual toggle usually set a class or attribute on <html>. Key the same rules off it:

/* src/styles/global.css */
html.dark .astro-code,
html.dark .astro-code span {
  color: var(--shiki-dark) !important;
  background-color: var(--shiki-dark-bg) !important;
  font-style: var(--shiki-dark-font-style) !important;
  font-weight: var(--shiki-dark-font-weight) !important;
  text-decoration: var(--shiki-dark-text-decoration) !important;
}

Use [data-theme='dark'] instead if that’s what your toggle sets. Our dark mode toggle guide shows how to set the class before first paint so code blocks don’t flash light on load.

Make dark the default instead

If most of your readers use dark mode, set defaultColor: 'dark'. Then the dark colors are inline and the light ones become --shiki-light variables, and your CSS flips the logic. With defaultColor: false, Shiki writes only variables (--shiki-light and --shiki-dark) and no inline colors, which is the cleanest option if you want CSS to control everything:

/* src/styles/global.css */
.astro-code, .astro-code span {
  color: var(--shiki-light);
  background-color: var(--shiki-light-bg);
}
html.dark .astro-code, html.dark .astro-code span {
  color: var(--shiki-dark);
  background-color: var(--shiki-dark-bg);
}

No !important needed here, because there are no inline colors to override.

The Code component

For code in .astro files, the built-in <Code /> component accepts the same options:

---
// src/components/Snippet.astro
import { Code } from 'astro:components';
const example = `npm create astro@latest`;
---
<Code code={example} lang="bash" themes={{ light: 'github-light', dark: 'github-dark' }} />

It outputs the same astro-code markup, so the CSS above covers it. For more Shiki options, including transformers and custom themes, see our Shiki syntax highlighting guide and the Shiki dual themes docs. If your Astro theme ships its own dark mode, check which class it toggles before writing the selector.

FAQ

Why are my Astro code blocks still light in dark mode?

Shiki only writes the dark colors as CSS variables. You need CSS that applies var(--shiki-dark) under your dark mode selector or media query, with !important unless you set defaultColor: false.

Can I use more than two Shiki themes in Astro?

Yes. themes accepts any number of keys, such as light, dark and dim, and each becomes a --shiki-<key> variable. Add a CSS rule per theme to switch between them.

Does Shiki dark mode add JavaScript to my Astro site?

No. Highlighting happens at build time and both palettes are in the HTML. Switching is pure CSS, so code blocks add no client-side JavaScript.