Control CodePen embeds with data attributes: default tab, height, and theme
Use CodePen embed data attributes to pick the starting tab, set iframe height, and choose a theme without editing the Pen itself.
06 Dec 2025, 15:21 UTC

The problem: your embedded Pen opens on the wrong tab
You paste a CodePen embed into your documentation or blog, and the iframe loads with the HTML editor active. Your readers have to click the Result tab to see the output. Worse, the iframe is fixed at a default height, so a tall preview gets cut off or forces an inner scrollbar. If your site uses a dark theme, CodePen's default light theme can clash visually.
CodePen's embed script reads a small set of data-* attributes from the block element you provide. These attributes let you set the default tab, iframe height, and theme without changing the Pen itself. Attributes that are misspelled or undocumented are silently ignored, so the embed falls back to CodePen defaults with no warning.
Supported embed attributes
The standard embed markup is a block element—usually a p or div—with data-slug-hash and data-user. CodePen's script converts it into an iframe that loads the Pen's HTML, CSS, and JavaScript editors plus a preview pane. The iframe runs in a sandbox, isolating the Pen's code from your host page's DOM, cookies, and same-origin policies.
| Attribute | Accepted values | Effect |
|---|---|---|
data-slug-hash | Pen slug hash | Identifies the Pen to embed. Required. |
data-user | CodePen username | Identifies the Pen author. Required. |
data-default-tab | html, css, js, or result | Selects which editor or preview is active when the iframe loads. |
data-height | Pixel value, e.g. 400 | Sets the iframe height in pixels. Without it, content may clip or scroll. |
data-theme-id | CodePen theme ID | Applies a CodePen theme to the embedded editors. |
Attribute names are case-sensitive. Writing data-defaulttab or data-Height produces no console error; the embed simply uses CodePen's defaults.
Worked example: force the Result tab and a fixed height
Create a public Pen, then copy the username and slug from its URL. For example, if the Pen URL is https://codepen.io/acme-widgets/pen/xYzAbC, the user is acme-widgets and the slug hash is xYzAbC.
Paste this markup into your site's HTML. Run it in a local file or your CMS's HTML editor; no build step or API key is required.
<p
class="codepen"
data-slug-hash="xYzAbC"
data-user="acme-widgets"
data-default-tab="result"
data-height="500"
data-theme-id="dark"
>
See the Pen <a href="https://codepen.io/acme-widgets/pen/xYzAbC">Example Pen</a> by Acme Widgets (<a href="https://codepen.io/acme-widgets">@acme-widgets</a>)</a> on <a href="https://codepen.io">CodePen</a>.
</p>
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
Where to run: any HTML file served over HTTP or opened directly in a browser. Required permissions: none beyond writing the file.
Expected checks:
- Open the page in a browser.
- Right-click the embed and choose Inspect. Confirm an <iframe> appears with a height near 500 pixels.
- Confirm the Result/preview pane is active on load rather than the HTML editor.
- Open your browser's developer tools console. If the embed fails, look for blocked script or iframe requests, which usually indicates a Content Security Policy issue.
Risk: embedded Pens execute JavaScript from the Pen author. The sandbox limits direct access to your page, but treat the embed as untrusted third-party content. If your site has a restrictive Content Security Policy, add the CodePen script origin and iframe source to the allowed directives, or the Pen will not load.
Limitation: height is fixed and initialization is lazy
data-height sets a static pixel value. CodePen does not auto-expand the iframe for dynamic content, so a tall result may still overflow or scroll. The embed script also lazy-initializes iframes near the viewport, which can delay rendering for Pens placed below the fold. Theme IDs are not versioned; CodePen can change supported themes or embed behavior without notice.
Practical check: after adding the embed, resize your browser window and inspect the iframe. If the height does not match data-height, verify the attribute spelling and confirm the embed script loaded before the DOM parsed the element.
Actionable closing
Start with data-default-tab and data-height because they are the most reliable presentation controls. Use data-theme-id only if you have verified the theme ID against a live embed, and avoid undocumented attributes entirely. Test the embed in an incognito window with your site's actual Content Security Policy before deploying.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.