🔥 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 CodePen Embed: Pens and StackBlitz Demos in MDX

Embed CodePen and StackBlitz in Astro: a lazy-loading component for pens, StackBlitz project and GitHub embeds, click-to-load facades and MDX usage.

#Astro#CodePen#StackBlitz#MDX#Embeds

To embed a CodePen in Astro, render an <iframe> pointing at https://codepen.io/<user>/embed/<pen-id> with loading="lazy", wrapped in a small component you can reuse from MDX. StackBlitz works the same way with https://stackblitz.com/edit/<project>?embed=1. Both are heavy once loaded, so lazy-load them or show a click-to-run button.

Key Takeaways

  • Skip CodePen’s ei.js embed script; a plain iframe gives you the same pen with less JavaScript.
  • Use default-tab and theme-id parameters to control what readers see first.
  • StackBlitz can embed a project or any public GitHub repo with stackblitz.com/github/....
  • Use a click-to-load facade for embeds above the fold or pages with several demos.

A CodePen component

---
// src/components/CodePen.astro
interface Props {
  user: string;
  id: string;
  title: string;
  tab?: string;
  height?: number;
}
const { user, id, title, tab = 'result', height = 420 } = Astro.props;
const src = `https://codepen.io/${user}/embed/${id}?default-tab=${encodeURIComponent(tab)}&theme-id=dark`;
---
<iframe
  src={src}
  title={title}
  height={height}
  loading="lazy"
  allowfullscreen
  style={`width:100%; border:0; height:${height}px`}
>
  <a href={`https://codepen.io/${user}/pen/${id}`}>See the pen "{title}" on CodePen</a>
</iframe>

Use it in MDX:

import CodePen from '../../components/CodePen.astro';

<CodePen user="your-username" id="abcXYZ" title="CSS-only accordion" tab="css,result" />

default-tab takes one or two of html, css, js and result. theme-id accepts light, dark or the ID of a custom theme on a paid account. CodePen’s own embed snippet uses a script (ei.js) to turn a placeholder into this iframe; writing the iframe directly removes that script. Set an explicit height so the layout doesn’t jump.

A StackBlitz component

StackBlitz runs full projects, including Astro itself, in the browser. Embed an existing project or a GitHub repository:

---
// src/components/StackBlitz.astro
interface Props {
  project?: string;
  github?: string;
  file?: string;
  title: string;
  view?: 'editor' | 'preview' | 'both';
}
const { project, github, file, title, view = 'both' } = Astro.props;
const base = github
  ? `https://stackblitz.com/github/${github}`
  : `https://stackblitz.com/edit/${project}`;
const params = new URLSearchParams({ embed: '1', view });
if (file) params.set('file', file);
---
<iframe
  src={`${base}?${params}`}
  title={title}
  loading="lazy"
  style="width:100%; height:520px; border:0; border-radius:8px"
></iframe>
<StackBlitz
  github="withastro/astro/tree/main/examples/basics"
  file="src/pages/index.astro"
  title="Astro basics starter"
/>

file opens a specific file in the editor; view chooses editor, preview or both. StackBlitz boots a WebContainer in the reader’s browser, which downloads a lot and uses real CPU, so a page with three of these gets slow fast.

Load on click instead

For heavy embeds, a facade keeps the page light until the reader opts in:

---
// src/components/ClickToLoad.astro
interface Props { src: string; title: string; height?: number }
const { src, title, height = 480 } = Astro.props;
---
<click-to-load data-src={src} data-title={title} style={`display:block; height:${height}px`}>
  <button type="button">Run demo: {title}</button>
</click-to-load>

<script>
  class ClickToLoad extends HTMLElement {
    connectedCallback() {
      this.querySelector('button')?.addEventListener('click', () => {
        const iframe = document.createElement('iframe');
        iframe.src = this.dataset.src!;
        iframe.title = this.dataset.title!;
        iframe.style.cssText = 'width:100%; height:100%; border:0';
        this.replaceChildren(iframe);
      }, { once: true });
    }
  }
  customElements.define('click-to-load', ClickToLoad);
</script>

Pass it the same embed URL either component builds. Astro bundles the script once per page however many demos you render, as our script tag guide explains.

Or skip the embed

Often a demo doesn’t need a third-party sandbox at all. An Astro component can render the real HTML and CSS right in the article, which is faster and indexable, with the source shown below in a code block. Use CodePen or StackBlitz when readers should edit and fork the code. If your CSP restricts frames, allow https://codepen.io and https://stackblitz.com in frame-src; see the Content Security Policy guide. For video embeds, the lightweight YouTube tip applies the same ideas. Documentation themes are a natural home for these components.

FAQ

How do I embed a CodePen in an Astro blog post?

Use an iframe with src="https://codepen.io/USER/embed/PEN_ID?default-tab=result", loading="lazy" and a fixed height. Wrap it in an Astro component and import it in your MDX posts.

Can I embed an Astro project in StackBlitz?

Yes. StackBlitz runs Astro in the browser. Embed a project with https://stackblitz.com/edit/PROJECT?embed=1 or any public repo folder with https://stackblitz.com/github/OWNER/REPO/tree/BRANCH/PATH?embed=1.

Do CodePen embeds slow down my page?

Each embed loads CodePen’s editor and your pen’s code in an iframe. loading="lazy" defers embeds below the fold, and a click-to-load button keeps them off the page until a reader asks for one.