HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's 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 it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1>My first web component</h1>
<!-- Three Media Objects -->
<media-object data-avatar="round">
<img slot="img" src="https://assets.codepen.io/15542/mia.jpg" alt="Miriam speaking" height="300" width="300" />
<h2 slot="title">Miriam Suzanne</h2>
<p slot="content">
I was looking for the fastest way to create a web component,
with the least custom JS required.
This is my first attempt,
using
<a href="https://twitter.com/WestbrookJ/status/1369350640019922948">
JS snippet provided by @WestbrookJ
</a>
on Twitter.
</p>
<p slot="content">
It took a bit to figure out the exact interplay
of shadow elements, attributes, slots, and selectors
to get my styling right.
I'm decently happy with the results,
but do still have a few issues.
</p>
</media-object>
<media-object data-theme="dark">Hello World</media-object>
<media-object class="grumpy">
<img slot="img" src="https://assets.codepen.io/15542/sad.jpg" alt="little girl frowning" />
<h2 slot="title">My main complaints:</h2>
<ul slot="content">
<li>
I can't establish attributes/semantics on the host element
from within the template
(at least using declarative syntax).
</li>
<li>
In the template, a <code>slot</code> wraps other elements,
but in usage, a <code>slot</code> <em>is an element</em>.
That makes it hard to style both the slot-default
and slotted content in a consistent way.
</li>
<li>
I'd love a declarative way to provide default semantics/attributes
on both the template & slots,
and have those "merge" with semantics/attrs
defined in the outer document.
</li>
</ul>
</media-object>
<!-- The Source Template -->
<template id="media-object-template">
<style>
:host {
display: contents;
--media-grid--default: 'img text'auto / minmax(5em, 30%) 1fr;
--media-grid--flip: 'text img'auto / 1fr minmax(5em, 30%);
}
:host([data-grid="flip"]) {
--media-grid: var(--media-grid--flip);
}
:host([data-avatar="round"]) {
--media-avatar: 100%;
}
@media (max-width: 60ch) {
:host {
--media-grid--override: 'img'minmax(4em, 1fr) 'text'auto / 100%;
}
}
article {
background-color: var(--media-bg, var(--bg, papayawhip));
color: var(--media-text, var(--text, black));
display: grid;
grid-auto-flow: dense;
grid-gap: var(--media-gutter, var(--gutter, 1em));
grid-template: var(--media-grid--override, var(--media-grid, var(--media-grid--default)));
padding: var(--media-gutter, var(--gutter, 1em));
}
::part(avatar) {
align-self: var(--media-align, start);
display: flex;
justify-self: center;
grid-column: img;
}
[data-default=title],
::slotted([slot=title]) {
color: var(--highlight, maroon);
margin-bottom: 0;
}
/* I wish there was a way to select both in one */
[data-default=img],
::slotted([slot=img]) {
border-radius: var(--media-avatar, 0);
max-width: 100%;
max-height: 100%;
height: auto;
overflow: hidden;
object-fit: var(--media-fit, cover);
}
[data-default=img],
::slotted([slot=img]:not([alt])) {
outline: medium dotted tomato;
}
[data-default=content] {
font-style: italic;
opacity: 0.5;
}
</style>
<!-- I don't see a clear way to add semantics to the template itself -->
<article>
<div part="avatar">
<slot name="img">
<img data-default="img" src="https://placeimg.com/300/300/any" height="300" width="300" alt="random person placeholder" />
</slot>
</div>
<div part="content">
<slot name="title">
<!-- use slot=title to replace the full title -->
<!-- or use the unnamed slot to just change the text -->
<h2 data-default="title">
<slot>Odd McBird</slot>
</h2>
</slot>
<slot name="content">
<p data-default="content">(empty)</p>
</slot>
</div>
</article>
</template>
/* external style, via props */
media-object:nth-of-type(odd) {
--media-grid: var(--media-grid--flip);
}
.grumpy {
--media-align: stretch;
--media-fit: cover;
--media-gutter: 0.5em 1em;
}
/* external style, targeting slots */
.grumpy [slot="img"] {
outline: medium dashed green;
}
/* external style, using global patterns */
[data-theme] {
background: var(--bg, white);
color: var(--text, black);
}
[data-theme="dark"] {
--bg: maroon;
--text: white;
--highlight: papayawhip;
}
/* page layout */
body {
--gutter: 0.5em;
display: grid;
grid-template-columns: minmax(min-content, 70ch);
grid-gap: var(--gutter);
justify-content: center;
padding: var(--gutter);
}
// copypasta from https://twitter.com/WestbrookJ/status/1369350640019922948
customElements.define(
"media-object",
class extends HTMLElement {
constructor() {
super();
let t = document.getElementById("media-object-template");
let content = t.content;
const shadowRoot = this.attachShadow({ mode: "open" }).appendChild(
content.cloneNode(true)
);
}
}
);
Also see: Tab Triggers