Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://prismjs.com/prism.js" data-manual></script>

<h1>Syntax highlighting code blocks with Prism and the <a href="https://drafts.csswg.org/css-highlight-api/" target="_top">Custom Highlight API</a></h1>

<div class="warning"><p>Your browser does not support the Custom Highlight API. Please try Chrome 105+, Safari 17.2+, or Firefox Nightly</p></div>

<p>What if I told you you can syntax highlight code snippets without needing to overload the DOM with a ton of <code>&lt;span&gt;</code> elements wrapping each token in order to colorize them?</p>

<h2>Howto</h2>

<h3>Step 1: Declare your syntax highlighting color scheme</h3>

<p>The format is <code>::highlight(x)</code> where <code>x</code> is the type of token.</p>

<style visible>/* From prism.css */
::highlight(parameter) {
	color: #1a1a1a;
}

::highlight(comment), ::highlight(prolog), ::highlight(doctype), ::highlight(cdata) {
	color: slategray;
}

::highlight(punctuation) {
	color: #999;
}

::highlight(property), ::highlight(tag), ::highlight(boolean), ::highlight(number), ::highlight(constant), ::highlight(symbol), ::highlight(deleted), ::highlight(class-name) {
	color: #905;
}

::highlight(selector), ::highlight(attr-name), ::highlight(string), ::highlight(char), ::highlight(builtin), ::highlight(inserted) {
	color: #690;
}

::highlight(operator), ::highlight(entity), ::highlight(url) {
	color: #a67f59;
	background: hsla(0, 0%, 100%, 0.5);
}

::highlight(atrule), ::highlight(attr-value), ::highlight(keyword) {
	color: #07a;
}

::highlight(function) {
	color: #dd4a68;
}

::highlight(regex), ::highlight(important), ::highlight(variable) {
	color: #e90;
}

::highlight(important), ::highlight(bold) {
	font-weight: bold;
}

::highlight(italic) {
	font-style: italic;
}

::highlight(entity) {
	cursor: help;
}</style>

<h3>Step 2: Register all token types as highlight styles</h3>
<script type="module" visible>if (CSS.highlights) {
	const tokenTypes = [ 'comment', 'prolog', 'doctype', 'cdata', 'punctuation', 'namespace',
	                     'property', 'tag', 'boolean', 'number', 'constant', 'symbol', 'deleted',
	                     'selector', 'attr', 'string', 'char', 'builtin', 'inserted', 'operator',
	                     'entity', 'url', 'string', 'atrule', 'attr', 'keyword', 'function', 'class',
	                     'regex', 'important', 'variable', 'important', 'bold', 'italic', 'entity',
	                     'parameter', 'class-name'];

	tokenTypes.forEach(tokenType => {
		CSS.highlights.set(tokenType, new Highlight());
	});
}</script>

<h3>Step 3: Tokenize the source code and register them with the highlights</h3>
<script type="module" visible>if (CSS.highlights) {
	const codeBlocks = document.querySelectorAll('script[visible], style[visible]');
		
	for (const codeBlock of codeBlocks) {
		// Tokenize it
		let tokens = Prism.tokenize(
			codeBlock.innerText,
			codeBlock.tagName == 'STYLE' ? Prism.languages.css : Prism.languages.javascript
		);

		// Loop all tokens and highlight them based on their type
		let pos = 0;
		for (const token of tokens) {
			if (token.type) {
				const range = new Range();
				range.setStart(codeBlock.firstChild, pos);
				range.setEnd(codeBlock.firstChild, pos + token.length);
				CSS.highlights.get(token.alias ?? token.type)?.add(range);
			}
			pos += token.length;
		}
	}
}</script>

<h2>Demo</h2>

<p>This page acts as its own demo: the CSS and JavaScript you see here is the actual code that gets executed and thereby syntax highlighted.</p>

<h2>Need more info?</h2>

<p>Go read <a href="https://brm.us/syntax-highlighting" target="_top">https://brm.us/syntax-highlighting</a> which hold a full write-up on this (and more)</p>
              
            
!

CSS

              
                @layer reset, layout;

@layer layout {
	@layer general {
		html {
			font-family: sans-serif;
		}
		body {
			padding: 3rem 0 10rem;
			width: 90%;
			max-width: 50em;
			margin: 0 auto;
		}
		h2 {
			margin-top: 2em;
		}
		a {
			color: #0000aa;
			text-decoration: none;
			&:hover {
				color: blue;
			}
		}
		footer {
			text-align: center;
			font-style: italic;
			margin: 4em 0;
		}
	}

	@layer code {
		script[visible],
		style[visible],
		pre {
			display: block;
			white-space: pre;
			border: 1px solid #dedede;
			padding: 1em;
			background: #fafafa;
			font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas,
				Liberation Mono, monospace;
			overflow-x: auto;
			border-left: 0.4em solid cornflowerblue;
			tab-size: 2;
			color: #1a1a1a;
			line-height: 1.6;
		}

		code:not(pre code), /* output:not(code:has(output) output) */ {
			background: #f7f7f7;
			border: 1px solid rgb(0 0 0 / 0.2);
			padding: 0.1rem 0.3rem;
			margin: 0.1rem 0;
			border-radius: 0.2rem;
			display: inline-block;
		}
	}

	@layer warning {
		.warning {
			display: none;
			box-sizing: border-box;
			padding: 1em;
			margin: 1em 0;
			border: 1px solid #ccc;
			background: rgba(255 255 205 / 0.8);
		}

		.warning > :first-child {
			margin-top: 0;
		}

		.warning > :last-child {
			margin-bottom: 0;
		}

		.warning a {
			color: blue;
		}
		.warning--info {
			border: 1px solid #123456;
			background: rgb(205 230 255 / 0.8);
		}
		.warning--alarm {
			border: 1px solid red;
			background: #ff000010;
		}
	}
}

@layer reset {
	* {
		box-sizing: border-box;
	}
	html,
	body {
		margin: 0;
		padding: 0;
	}
}

              
            
!

JS

              
                if (!CSS.highlights) {
	document.querySelector('.warning').style.display = 'block';
}
              
            
!
999px

Console