Styling

Every custom property and part a site can style.

The default look is native to the page: it takes the page's font and text colour, and mixes its lines and tints from that colour, so it fits a white, a tinted and a dark page without configuration. Everything about it can be changed from the page, in four ways, from least to most work:

  1. Tokens: set a --pg-* custom property.
  2. Parts: style an element with ::part().
  3. A stylesheet: add your own rules to every playground, inside its shadow roots.
  4. Nothing: drop the default look and write all of it.

No way needs a <style> element or a style attribute, so a strict Content Security Policy is not involved.

Tokens

Set any --pg-<name> on an element or on an ancestor (a stylesheet, :root, a wrapper).

:root {
  color-scheme: light dark; /* follow the reader's system */
}
code-playground {
  --pg-accent: rebeccapurple;
  --pg-radius: 2px;
  --pg-font-mono: "Berkeley Mono", monospace;
}

Every colour, font, size, space, radius and border of the default look is a token, the editor's and its tooltips' included; the table below lists them all. A token that is another by default (--pg-accent is --pg-blue) follows it: set --pg-blue and the accent, links, functions and information all change; set --pg-accent and only the accent does.

  • Light and dark. Colours are light-dark() values, so they follow the element's color-scheme. A page chooses it: color-scheme: light dark follows the reader's system, color-scheme: dark forces dark, and with none the playground is light. Set it to match the page: on a dark page that sets no color-scheme, the neutral colours still follow the text colour and Run stays legible, but the syntax colours are the light scheme's.
  • Neutral colours. --pg-muted, --pg-border, --pg-code-bg, --pg-hover and the like are color-mix(in oklab, currentColor N%, transparent): they follow the page's text colour. Set them to any colour to take control. The text ones (muted text and placeholders at 70%, line numbers and comments at 66%) are at least 4.5:1 (WCAG AA) on the editor's ground on a white page with near-black text, a cream one and a dark one with near-white text; a test holds that line. Your own colours are yours to check.
  • Space. --pg-space-1 to --pg-space-6 are a scale on a 4px base. --pg-pad is the one left edge: the toolbar's label, the line numbers (or the code, with no numbers) and the output text all start there.
  • Shape. One radius, --pg-radius; --pg-radius-inner, --pg-radius-control and --pg-radius-small are derived from it, so changing it changes all of them.
  • The editor's geometry (--pg-line-height, --pg-editor-padding-block, --pg-editor-padding-inline, --pg-editor-gutters, …) lets an editor match a static code block line for line.
Token Light, or derived from Dark For
--pg-red #97423e #eea099 Red: errors, deletions.
--pg-orange #914b0e #e7a77c Orange: constants, changes.
--pg-yellow #7a5b00 #d3b36e Yellow: warnings, parameters.
--pg-green #23723b #8bc997 Green: success, strings, insertions.
--pg-cyan #036e77 #65c9d4 Cyan: types.
--pg-blue #2e61a0 #8ebaf4 Blue: accent, links, functions.
--pg-purple #704d94 #c5a8e9 Purple: keywords.
--pg-bg transparent Background of the frame.
--pg-fg currentColor Text: the page's.
--pg-muted color-mix(in oklab, currentColor 70%, transparent) Secondary text: status, placeholders, the language.
--pg-border color-mix(in oklab, currentColor 16%, transparent) Hairlines.
--pg-code-bg color-mix(in oklab, currentColor 4%, transparent) Code background.
--pg-surface #ffffff #1e2127 Opaque background of what floats: tooltips.
--pg-hover color-mix(in oklab, currentColor 8%, transparent) A hovered control.
--pg-active-line color-mix(in oklab, currentColor 5%, transparent) The editor's current line.
--pg-gutter color-mix(in oklab, currentColor 66%, transparent) Line numbers.
--pg-accent --pg-blue The Run button, the selection.
--pg-accent-fg #ffffff #10151c Text on the accent.
--pg-focus color-mix(in oklab, var(--pg-accent, var(--pgd-accent, light-dark(#2e61a0, #8ebaf4))) 60%, currentColor) The keyboard focus ring.
--pg-selection color-mix(in oklab, var(--pg-accent, var(--pgd-accent, light-dark(#2e61a0, #8ebaf4))) 28%, transparent) Selected text.
--pg-error --pg-red Errors.
--pg-warning --pg-yellow Warnings.
--pg-ok --pg-green Success.
--pg-info --pg-blue Information.
--pg-linked color-mix(in oklab, var(--pg-yellow, var(--pgd-yellow, light-dark(#7a5b00, #d3b36e))) 32%, transparent) Code linked to a hovered item.
--pg-marked color-mix(in oklab, var(--pg-accent, var(--pgd-accent, light-dark(#2e61a0, #8ebaf4))) 22%, transparent) Code marked by mark().
--pg-space-1 4px Spacing scale: 4px.
--pg-space-2 8px Spacing scale: 8px.
--pg-space-3 12px Spacing scale: 12px.
--pg-space-4 16px Spacing scale: 16px.
--pg-space-5 24px Spacing scale: 24px.
--pg-space-6 32px Spacing scale: 32px.
--pg-pad --pg-space-4 The shared left edge: the toolbar, the line numbers and the output text start here, and the right edge mirrors it.
--pg-gap --pg-space-2 Space between neighbours.
--pg-border-width 1px Width of a hairline.
--pg-radius 8px Corner radius of the frame.
--pg-radius-inner calc(var(--pg-radius, var(--pgd-radius, 8px)) - var(--pg-border-width, var(--pgd-border-width, 1px))) Corner radius of what sits against the frame's inside.
--pg-radius-small calc(var(--pg-radius, var(--pgd-radius, 8px)) / 4) Corner radius of a highlighted span.
--pg-radius-control calc(var(--pg-radius, var(--pgd-radius, 8px)) * 0.75) Corner radius of buttons, menus and tooltips.
--pg-control-height 28px Height of a button (44px with a coarse pointer).
--pg-font inherit Interface font: the page's.
--pg-font-mono ui-monospace, "SF Mono", SFMono-Regular, "Cascadia Code", Menlo, Consolas, monospace Code font.
--pg-font-size-ui 0.875em Size of the toolbar, tabs and status.
--pg-font-size-code 0.875em Size of code and output text.
--pg-line-height 1.5 Line height of code and output text.
--pg-editor-bg --pg-code-bg The editor's background.
--pg-editor-border none The editor's border (a border shorthand).
--pg-editor-radius 0 The editor's corner radius.
--pg-editor-padding-block --pg-space-3 Space above the first line and below the last.
--pg-editor-padding-inline 16px 16px Space before and after each line of code.
--pg-editor-gutters flex flex shows the line numbers, none hides them.
--pg-tooltip-fg --pg-fg Tooltip text.
--pg-tooltip-bg --pg-surface Tooltip background.
--pg-tooltip-border 1px solid var(--pg-border, var(--pgd-border, color-mix(in oklab, currentColor 16%, transparent))) Tooltip border (a border shorthand).
--pg-tooltip-radius --pg-radius-control Tooltip corner radius.
--pg-tooltip-font --pg-font Tooltip font family.
--pg-token-keyword --pg-purple Keywords.
--pg-token-string --pg-green Strings.
--pg-token-string-expression --pg-green Expressions inside strings.
--pg-token-comment color-mix(in oklab, currentColor 66%, transparent) Comments.
--pg-token-constant --pg-orange Constants and numbers.
--pg-token-parameter --pg-yellow Parameters.
--pg-token-function --pg-blue Functions.
--pg-token-type --pg-cyan Types.
--pg-token-variable inherit Variables: the text colour.
--pg-token-punctuation inherit Punctuation: the text colour.
--pg-token-link --pg-blue Links.
--pg-token-inserted --pg-green Insertions.
--pg-token-deleted --pg-red Deletions.
--pg-token-changed --pg-orange Changes.
--pg-ansi-0 #2e2e2e #989898 ANSI colour 0.
--pg-ansi-1 --pg-red ANSI colour 1.
--pg-ansi-2 --pg-green ANSI colour 2.
--pg-ansi-3 --pg-yellow ANSI colour 3.
--pg-ansi-4 --pg-blue ANSI colour 4.
--pg-ansi-5 --pg-purple ANSI colour 5.
--pg-ansi-6 --pg-cyan ANSI colour 6.
--pg-ansi-7 #636363 #cacaca ANSI colour 7.
--pg-ansi-8 #555555 #ababab ANSI colour 8.
--pg-ansi-9 #a03f3c #ffb5ae ANSI colour 9.
--pg-ansi-10 #137738 #95dfa4 ANSI colour 10.
--pg-ansi-11 #7e5e01 #ebc573 ANSI colour 11.
--pg-ansi-12 #2863ab #a8ceff ANSI colour 12.
--pg-ansi-13 #754d9e #d9bbfe ANSI colour 13.
--pg-ansi-14 #00717a #65dfed ANSI colour 14.
--pg-ansi-15 #424242 #eeeeee ANSI colour 15.

The table is generated from packages/core/tokens.ts. Syntax colours also apply to code a page highlighted with highlight(): give the wrapper the class pg-code.

Parts

Every structural element has a part name. From the page, ::part() reaches it through the element's shadow root:

code-playground::part(toolbar) {
  border-bottom: 0;
}
code-playground::part(run) {
  border-radius: 999px;
}
/* The root container: lay the playground out as you like. */
code-playground::part(root) {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

A page's ::part() rule beats the defaults and anything added inside the shadow roots. The parts of each element are listed in elements.md and in the Custom Elements Manifest (cssParts, with cssProperties for the tokens). In short:

Part Is
root the outer container (all three elements); re-lay it out here
toolbar, language, run, stop, reset, examples, keymap, note, status the toolbar and what is in it, and the status line
editor the box that holds the editor
editor-view, editor-scroller, editor-content, editor-gutters, editor-line-numbers, editor-tooltip, prelude-toggle CodeMirror's insides, exported from the editor's own shadow root (exportparts)
tabs, tab, output, placeholder the outputs
item, item-text, item-ansi, item-code, item-tree, item-table, item-diagnostics, item-verdicts, item-graph, item-svg, item-custom, actions, action, graph-save items

A part can be styled from a state: code-playground:state(running)::part(run) { … }.

Adding a stylesheet

::part() reaches what has a part. To write rules against anything inside the shadow roots, add a constructed stylesheet:

import { addStyleSheet } from "code-playgrounds/element";

const sheet = new CSSStyleSheet();
sheet.replaceSync(`
  .pg-tab[aria-selected="true"] { color: tomato; }
  .cm-editor .cm-gutters { display: none; }
`);
const remove = addStyleSheet(sheet); // or addStyleSheet("css text")

A page without a bundler imports it from define.js, the file it loads for the elements, which exports addStyleSheet(), setDefaultStyles() and configure():

<script type="module">
  import { addStyleSheet } from "/playgrounds/element/define.js";
  addStyleSheet("code-playground { --pg-radius: 0 }");
</script>

A module is chosen over a static method on the element class: the functions are not about one element, they reach every playground, the editor's roots included, and the same names work for Solid. The registry is global (Symbol.for("code-playgrounds.styles")), so a bundle of the npm package and the built define.js on one page share it, whichever copy's addStyleSheet() is called (tests/styles.spec.ts).

addStyleSheet(sheet: CSSStyleSheet | string): () => void adds the sheet to every playground's shadow roots, present and future, the editor's nested root included, and returns a function that removes it. A string is made into a constructed stylesheet. Sheets apply in the order they were added, after the default look, so at equal specificity yours wins. Nothing needs a <style> element, so a page policy without 'unsafe-inline' is unaffected.

The default look is in a cascade layer, code-playgrounds. A rule you add outside a layer beats every default whatever its specificity: .pg-run { background: black } is enough. Two things sit outside the layer: CodeMirror's own base theme, and the editor rules that must beat it. To override those, write CodeMirror's own selector (.cm-editor .cm-gutters, not .cm-gutters); a later sheet wins a tie. Tokens and ::part() need none of this.

One thing is outside the layer on purpose: containment. Static code and items that sit in your page's own DOM (.pg, .pg-static, .pg-item) keep min-width: 0, max-width: 100% and overflow-x: auto (overflow: clip for .pg) whatever your reset says, so a reset such as pre { white-space: pre } cannot push the page sideways; the code scrolls inside its box. Each rule weighs one class, so an element selector loses to it, and your own class rule wins when it comes after playground.css. Everything else about the code, its wrapping included, is layered and yours to change.

The classes in the sheets are the .pg-* names the markup carries (.pg, .pg-toolbar, .pg-run, .pg-tab, .pg-output, .pg-item-table, …; playground.css lists them).

Starting from nothing

unstyled on an element drops the default look of that element and its editor; the function setDefaultStyles(false) does it for every playground (setDefaultStyles(true) brings it back):

<code-playground language="python" unstyled></code-playground>

What function needs stays: screen-reader text stays hidden (.pg-sr), the element is a block, and CodeMirror's own layout is untouched, so the editor still lays out lines, gutters and tooltips. Everything else, colours, fonts, spacing, borders and the syntax colours, is yours to add with addStyleSheet():

import { addStyleSheet, setDefaultStyles } from "code-playgrounds/element";

setDefaultStyles(false);
addStyleSheet(`
  .pg { font: 15px/1.5 system-ui; }
  .pg-toolbar { display: flex; gap: 8px; }
  .pg-t-keyword { color: #7a3e9d; }
  .pg-t-string { color: #1a7f37; }
`);

With Solid

The Solid front end draws in the page, not in a shadow root, so link or import playground.css as before (it is the same layered sheet) and style the classes as in any page: rules outside a layer beat it. The editor alone keeps a shadow root. addStyleSheet() and setDefaultStyles() are exported from code-playgrounds/solid (and code-playgrounds/core) and reach that root. The editor's host element carries part="editor" and exports the editor's parts, so .pg-editor::part(editor-content) { … } works in the page. For unstyled, leave out playground.css and call setDefaultStyles(false).

Responsive

A playground works at every width (docs/plan.md D34): the toolbar and the tabs wrap instead of squeezing their labels, long code and wide outputs scroll inside their own box, never the page, controls are 44px with a coarse pointer, and code is at least 16px there. The default look has no rule that depends on the viewport's width; only (pointer: coarse) is queried. A playground sits in a column of any width, so if you add a rule that depends on width, query the playground's container, not the viewport. Make the element a container and re-lay it out with parts:

code-playground {
  container-type: inline-size;
}
@container (min-width: 720px) {
  code-playground::part(root) {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  code-playground::part(toolbar) {
    grid-column: 1 / -1;
  }
}

(tests/styles.spec.ts holds this working in all three browsers.) The default does not set container-type itself: containment would make a playground in an auto-sized flex or grid item collapse.

Examples

Match a site: square and flat

code-playground {
  --pg-radius: 0;
  --pg-font: "IBM Plex Sans", system-ui, sans-serif;
  --pg-font-mono: "IBM Plex Mono", monospace;
  --pg-accent: #0b6e4f;
  --pg-accent-fg: #fff;
  --pg-code-bg: #f4f1ea;
  --pg-border: #1b1b1b;
}

Editor beside the output, no frame

code-playground {
  container-type: inline-size;
}
code-playground::part(root) {
  border: 0;
  border-radius: 0;
}
@container (min-width: 760px) {
  code-playground::part(root) {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto auto 1fr;
    column-gap: 24px;
  }
  code-playground::part(toolbar),
  code-playground::part(status) {
    grid-column: 1 / -1;
  }
  code-playground::part(editor) {
    grid-row: 3;
  }
  code-playground::part(tabs),
  code-playground::part(output) {
    grid-column: 2;
  }
  code-playground::part(tabs) {
    grid-row: 3;
    align-self: start;
  }
}

Everything of your own

import { addStyleSheet, setDefaultStyles } from "code-playgrounds/element";

setDefaultStyles(false);
addStyleSheet(`
  .pg { display: block; font: 15px/1.5 ui-sans-serif, system-ui; color: #e8e6e3; background: #1c1b22;
        border-radius: 14px; overflow: clip; }
  .pg-toolbar { display: flex; align-items: center; gap: 8px; padding: 10px 14px; }
  .pg-language { margin-inline-end: auto; letter-spacing: .04em; }
  .pg-run { font: inherit; border: 0; border-radius: 999px; padding: 4px 14px;
            background: #f2c94c; color: #1c1b22; }
  .pg-tabs { display: flex; gap: 14px; padding: 0 14px; }
  .pg-tab { font: inherit; background: none; border: 0; color: inherit; opacity: .6; }
  .pg-tab[aria-selected="true"] { opacity: 1; text-decoration: underline; }
  .pg-output { padding: 12px 14px; font-family: ui-monospace, monospace; }
  .pg-t-keyword { color: #c792ea; }
  .pg-t-string { color: #c3e88d; }
  .cm-editor { background: #15141a; }
  .cm-editor .cm-scroller { font-family: ui-monospace, monospace; line-height: 1.6; }
`);

This page as MarkdownSource on GitHub