css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div class="container-fluid">
    <h1>Custom Element Demo</h1>
  <div class="row">
    <div class="col-sm-6">
      <div id="prefill">
        <div class="input-group">
          <span class="input-group-addon">
            Autofill this form
          <div class="dropdown-menu dropdown-menu-right" aria-labelledby="opts">
          <button class="btn btn-secondary dropdown-toggle form-control" type="button" id="opts" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Select Data</button>
      <hr />
      <div class="form">
        <div class="form-group">
          <label for="width">Width <small class="text-muted">(in px)</small></label>
          <input type="number" class="form-control" id="width" placeholder="Width">
        <div class="form-group">
          <label for="alt">Alt <small class="text-muted">(can be empty)</small></label>
          <input type="text" class="form-control" id="alt" placeholder="Alt Attriibute">
        <div class="form-group">
          <label for="src">Source Image <small class="text-muted">(src)</small></label>
          <input type="url" class="form-control" id="src" placeholder="http://.....">
        <div class="form-group">
          <label for="caption">Caption <small class="text-muted">(figcaption)</small></label>
          <input type="text" class="form-control" id="caption" placeholder="Caption">
        <button type="button" class="btn btn-primary" id="append">Add</button>
        <button type="button" class="btn btn-danger float-right" id="clear">Remove all</button>
        <button type="reset" class="btn btn-warning">Reset</button>
    <div class="col-sm-6">
      <div id="app"></div>

              img-figure {
  display: block;
  max-width: 400px;
  img {
    max-width: 100%;
  figcaption {
    width: 100%;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
pre {
  border: 0;
  padding: 10px;
  // margin: -1.1rem;
  margin: 0;
  border-radius: 0.25rem;
#app > div {
  margin: 1.125rem 0;
  &:first-child {
    margin-top: 0;
.tab-bodies {
  > div:not(.active) {
    display: none
              let elName = 'img-figure';
const template = state => `<figure>
    alt="${state.alt || state.caption}">

class ImgFigure extends HTMLElement {
  connectedCallback() {
    this.src = this.getAttribute("src") || null;
    this.caption = this.getAttribute("caption") || "";
    this.alt = this.getAttribute("alt") || null;

  render() {
    this.innerHTML = template({
      src: this.src,
      alt: this.alt,
      caption: this.caption

customElements.define("img-figure", ImgFigure);

const maxWidth = 400;

const data = [
    src: "//res.cloudinary.com/time2hack/image/upload/q_auto:good/goodbye-xmlhttprequest-ajax-with-fetch-api-demo.png",
    alt: "GoodBye XMLHttpRequest",
    caption: "GoodBye XMLHttpRequest; AJAX with fetch API (with Demo)"
    src: "//res.cloudinary.com/time2hack/image/upload/iterating-data-with-map-reduce-foreach-and-filter.png",
    alt: "Array Iteration Functions",
    caption: 'Iterating data with Map, Reduce, ForEach and Filter'
    src: "//res.cloudinary.com/time2hack/image/upload/q_auto:good/ways-to-host-single-page-application-spa-static-site-for-free.png",
    alt: "Free Static Hosting",
      "Ways to host single page application (SPA) and Static Site for FREE"
    src: "//res.cloudinary.com/time2hack/image/upload/v1508699480/javascript-template-literals.png",
    alt: "JavaScript Template Literals",
    caption: "Benefits of JavaScript Template Literals and Tagged Templates"
const card = data => {
  const id = `${+new Date}`;
  return `<div class="card" id="card_${id}">
  <div class="card-header">
    <ul class="nav nav-tabs card-header-tabs">
      <li class="nav-item">
        <a class="nav-link disabled" href="#">#card_${id}</a>
      <li class="nav-item">
        <a class="nav-link active" data-target="#card_${id} .preview" href="#">Preview</a>
      <li class="nav-item">
        <a class="nav-link" data-target="#card_${id} .source" href="#">Source</a>
      <li class="nav-item">
        <a class="nav-link" data-target="#card_${id} .data" href="#">Data</a>
  <div class="card-body tab-bodies">
    <div class="preview active">${elSrc(data)}</div>
    <div class="source">${source(data)}</div>
    <div class="data"><pre>${JSON.stringify(data, null, 2)}</pre></div>
const elSrc = data => {
  return `<${elName}
  style="max-width: ${data.maxWidth || maxWidth}px"

// Add element in conventional way
const element = data => {
  // create element
  const i = document.createElement(elName);

  //set the required attributes
  i.setAttribute("src", data.src);
  i.setAttribute("caption", data.caption);
  i.setAttribute("alt", data.alt);
  i.style.maxWidth = data.maxWidth || maxWidth;
  return i;
const clear = () => (document.querySelector("#app").innerHTML = "");

const _prettify = block => {
  window.PR &&
    (function(block) {
const source = data => {
  const src = elSrc(data).replace(/</ig, '&lt;').replace(/>/ig, '&gt;');
  return `<pre>${src}</pre>`;

const getFormData = () => {
  return {
    maxWidth: document.querySelector("#width").value,
    alt: document.querySelector("#alt").value,
    src: document.querySelector("#src").value,
    caption: document.querySelector("#caption").value
const add = data => {
  const d = document.createElement('div');
  d.innerHTML = card(data);
  document.querySelector("#app").insertBefore(d, document.querySelector("#app").firstChild);
    .forEach(el => _prettify(el))
//attach to the DOM

data.forEach((item, index) => {
  const container = document.querySelector('#prefill .dropdown-menu');
  const a = document.createElement('a');
  a.setAttribute('href', '#');
  a.setAttribute('data-index', index);
  a.innerHTML = `<img src="${item.src}" height="35" alt="${item.alt}" class="rounded" /> ${item.caption}`;

document.querySelector("#append").addEventListener("click", () => {
document.querySelector("#clear").addEventListener("click", () => {
document.querySelector("#prefill").addEventListener("click", (e) => {
    const index = e.target.dataset.index;
    document.querySelector("#width").value = data[index].maxWidth;
    document.querySelector("#alt").value = data[index].alt
    document.querySelector("#src").value = data[index].src
    document.querySelector("#caption").value = data[index].caption;
    document.querySelector('#prefill .dropdown-menu').classList.remove('show')
document.querySelector("#opts").addEventListener("click", (e) => {
  const toggle = document.querySelector('#prefill .dropdown-menu').classList.contains('show');
  document.querySelector('#prefill .dropdown-menu').classList[toggle ? 'remove' : 'add']('show');

//All the tab changing events
document.querySelector("#app").addEventListener("click", (e) => {
  const {target} = e;
  const a = 'active';

  if(target.classList.contains('nav-link') && target.dataset.target) {
    const t = target.dataset.target;
    const c = t.split(' ')[0];
    document.querySelector(`${c} .nav-link.${a}`).classList.remove(a);
    document.querySelector(`${c} .tab-bodies > .${a}`).classList.remove(a);
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................