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

              
                <template id="pic">
  <figure>
    <img width="64" />
    <figcaption part="cap"/>
  </figure>
</template>
<div style="display: flex; flex-wrap: wrap;">
  <picsum-pic id="one"
              number="123"
              caption="No stylesheet on its own"
              note="img tag should be square/unstyled"
              >
  </picsum-pic>
  <picsum-pic id="two"
              number="665"
              caption="CSS targeting the elements inside"
              note="img tag should be square/unstyled"
              >
  </picsum-pic>
  <picsum-pic id="three"
              styles="true"
              number="64"
              caption="stylesheet referencing :root properties"
              note="img should be circular since CSS has border-radius at 100%"
              >
  </picsum-pic>
  <picsum-pic id="four"
              styles="true"
              number="67"
              caption="stylesheet referencing properties on this element"
              note="img should have softer border radius since CSS for this element is 0.5rem"
              >
  </picsum-pic>
  <div id="five-parent">
    <picsum-pic id="five"
                number="67"
                caption="no stylesheet but custom element parent should be applied"
                note="caption should be green since color is inherited from the div containing this"
                >
    </picsum-pic>
  </div>
  <picsum-pic id="six"
              number="17"
              caption="no stylesheet but styles applied to this element should be inherited"
              note="caption should be purple since color is inherited from the custom element">
  </picsum-pic>
  <picsum-pic id="seven"
              number="57"
              caption="The main stylesheet has applied styles to an exposed part= of this one"
              note="caption should be in bold">
  </picsum-pic>
  <picsum-pic id="eight"
              number="17"
              styles="host"
              caption="styles exist that are inherited, :host is used inside, but it's specificity is lower so has no effect"
              note="caption should be red">
  </picsum-pic>
    <picsum-pic id="nine"
              number="18"
              styles="host-important"
              caption="styles exist that would be inherited but :host annd !important allow overriding it"
              note="caption should be unstyled/black/normal weight">
  </picsum-pic>
  
</div>

              
            
!

CSS

              
                /* Custom properties */
:root {
  --br: 100%;
}

picsum-pic#four {
  --br: 0.5rem;
}


/** Target an element directly
 * inside a custom element.  This
 * will not have any effect.
 */
picsum-pic#two img {
  border-radius: 100%;
}

/* Target the parent of five directly
 * which should inherit to the inner 
 * elements of the ShadowDOM
 */
#five-parent {
  color: green;
}

/* Target the custom element directly
 * which should inherit to the inner 
 * elements of the ShadowDOM
 */
picsum-pic#six {
  color: purple;
}

/* This should be inherited, but won't because the element uses :host and !important to override
*/
picsum-pic#eight {
  color: red;
}

/* Target elements generally.
 * These will not apply to these elements
 * inside the ShadowDOM
 */
figure {
  padding: 1rem;
  border: solid thin grey;
  border-radius: 1rem;
}
figure img {
  border-radius: 0.5rem;
}
figure figcaption {
  font-weight: bold;
}

/* This targets the part=cap
inside #seven
 */
picsum-pic#seven::part(cap) {
  font-weight: bold;
}


              
            
!

JS

              
                class PicsumPic extends HTMLElement {
  constructor() {
    super();
    this.number  = this.getAttribute("number");
    this.caption = this.getAttribute("caption");
    this.note    = this.getAttribute("note");
    this.styles  = this.getAttribute("styles");
  }

  connectedCallback() {
    const template = document.getElementById("pic");
    const content  = template.content;

    const node    = content.cloneNode(true);
    const img     = node.querySelector("figure img");
    const caption = node.querySelector("figure figcaption");
    caption.style.maxWidth = "30ch"

    if (!this.number) {
      return;
    }

    img.setAttribute("src", `https://picsum.photos/id/${this.number}/200`);

    if (this.caption || this.note) {
      if (this.caption) {
        img.setAttribute("alt", this.caption);
        const pCaption = document.createElement("p")
        pCaption.innerText = this.caption
        caption.appendChild(pCaption)
      }
      if (this.note) {
        const pNote = document.createElement("p")
        pNote.style.fontStyle="italic"
        pNote.style.fontSize="90%"
        pNote.innerText = this.note
        caption.appendChild(pNote)
      }
    }

    const shadowRoot = this.attachShadow({ mode: "open" });
    shadowRoot.appendChild(node);
    if (this.styles === "true") {
      const styles = document.createElement("style");
      styles.innerText = `
          figure img { border-radius: var(--br); }
        `;
      shadowRoot.appendChild(styles);
    } else if (this.styles === "host") {
      const styles = document.createElement("style");
      styles.innerText = `
:host { all:initial }
        `;
      shadowRoot.appendChild(styles);
    } else if (this.styles === "host-important") {
      const styles = document.createElement("style");
      styles.innerText = `
:host { all:initial !important; }
        `;
      shadowRoot.appendChild(styles);
    }

  }
}
customElements.define("picsum-pic", PicsumPic);


              
            
!
999px

Console