skip to content

astro 3 min read

Write your code like a decent human being – how Astro Expressive Code makes your snippets beautiful

Have you ever looked at your fresh block of code and wanted to gouge your freaking eyes out? Yeah, me neither. I've been using Astro Expressive Code, cause I'm not an idiot.

Have you ever looked at a block of code and wanted to gouge your freaking eyes out?

No? Yeah, me neither.

I’ve been using astro-expressive-code, cause I’m not an idiot.

If you’re still presenting your code like an ape with just the basic styling, then I’m sorry – you’re an idiot.

BUT, worry no more, cause today I’ll show you how with just a couple of lines of code you can stop being one (an idiot, I mean).

Don't be an idiot

Introducing Astro Expressive Code

The beauty of Expressive Code is that it integrates seamlessly with Astro and gives you professional-looking code blocks with minimal setup. Line numbers, syntax highlighting, line markers, titles – all of that jazz works out of the box.

Let’s make your code look pretty in no time!

1. Install the package

Terminal window
pnpm add astro-expressive-code

(or npm install if you’re into that sort of thing)

If you’re interested in how it works under the hood, check out the docs.

2. Adjust the astro.config file

astro.config.ts
import { defineConfig } from "astro/config";
import expressiveCode from "astro-expressive-code";
export default defineConfig({
integrations: [
// Add it BEFORE your markdown/mdx integration
expressiveCode({
themes: ["dracula", "github-light"],
}),
],
});

At a minimum, you need to add the integration and choose your themes. The plugin handles the rest.

You can customize Expressive Code to match your site’s vibe. Here’s what I’m using:

site.config.ts
export const expressiveCodeOptions: AstroExpressiveCodeOptions = {
styleOverrides: {
borderRadius: "4px",
codeFontFamily:
'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
codeFontSize: "0.875rem",
codeLineHeight: "1.7142857rem",
codePaddingInline: "1rem",
frames: {
frameBoxShadowCssValue: "none",
},
uiLineHeight: "inherit",
},
themes: ["dracula", "github-light"],
useThemedScrollbars: false,
};

Then import it in your astro.config.ts:

astro.config.ts
import { expressiveCodeOptions } from "./src/site.config";
export default defineConfig({
integrations: [expressiveCode(expressiveCodeOptions)],
});

What you get out of the box

Syntax highlighting

Just write your code blocks with the language specified:

function demo() {
console.log("Beautiful syntax highlighting");
return true;
}

Code titles

Add a title to give context:

my-awesome-function.js
function myAwesomeFunction() {
return "This has a title!";
}

Line highlighting

Mark specific lines as inserted, deleted, or highlighted:

line-markers.js
function demo() {
console.log("this line is marked as deleted");
// This line and the next one are marked as inserted
console.log("Hello");
return "this line uses the neutral default marker type";
}

Terminal-style blocks

Perfect for showing bash commands:

Terminal window
pnpm install astro-expressive-code
pnpm dev

The result

Your code blocks will look professional, readable, and actually pleasant to look at. No more ugly, unstyled code that makes people want to close the tab.

Professional doggo

Expressive Code handles:

  • Syntax highlighting for dozens of languages
  • Line numbers (optional)
  • Line markers (insertions, deletions, highlights)
  • Code titles for context
  • Theme switching (dark/light mode support)
  • Copy button for easy code copying
  • And much more – check the docs

Summary

If you’re building with Astro and you care even a little bit about how your code looks, use Expressive Code. It takes 5 minutes to set up and makes your content look 100x more professional.

Stop being an idiot. Install it now.


Useful links:

Filip Chrapek

Let's work together

What do you need?
Budget
When?

Prefer email? hi@philgood.dev