cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <!--
    Demo for this article:
    http://blustemy.io/using-the-html-template-element/
-->

<div class="buttons">
    <button type="button" class="colors-add">Add color</button>
    <button type="button" class="colors-clear">Clear all colors</button>
</div>

<ul class="colors-list"></ul>

<template id="color-template">
    <figure class="color-sample">
        <svg width="80" height="80" viewBox="0 0 80 80"
             xmlns="http://www.w3.org/2000/svg">
            <polyline class="color-stroke" points="10,5 5,10 20,5 5,20 30,5 10,30 40,10 8,45 55,10 10,60 70,10 15,70 68,20 30,70 69,33 39,72 72,45 50,73 68,60 62,72 70,69"/>
        </svg>
        <figcaption>
            <code></code>
            <button type="button" class="color-remove">×</button>
        </figcaption>
    </figure>
</template>

            
          
!
            
              /*
    Demo for this article:
    http://blustemy.io/using-the-html-template-element/
*/

.buttons {
    padding: .5rem 0;
    text-align: center;
}

.buttons button {
    font-size: 1em;
    padding: .25em .75em;
    background: #1098ad;
    border: .075em solid rgba(0, 0, 0, 0.1);
    border-radius: .1em;
    color: white;
}

.buttons button:hover, .buttons button:focus {
    border-color: rgba(0, 0, 0, 0.5);
}

.buttons button:active {
    box-shadow: 0 0 0 .05em rgba(0, 0, 0, .5) inset;
}

.colors-list,
.colors-list > li {
    margin: 0;
    padding: 0;
    list-style: none;
}

.colors-list > li {
    display: inline-block;
}

.color-sample {
    position: relative;
    width: 9rem;
    margin: .5rem;
    text-align: center;
}

.color-sample code {
    font-size: .8em;
}

.color-stroke {
    stroke-width: 7;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.color-remove {
    position: absolute;
    right: 1rem;
    top: 0;
    font-size: 1rem;
    width: 1.25em;
    height: 1.25em;
    text-align: center;
    line-height: 1.25;
    background: #eee;
    border: none;
    border-radius: 50%;
    color: #444;
}

.color-remove:hover,
.color-remove:focus {
    background: #444;
    color: #eee;
}

            
          
!
            
              /*
    Demo for this article:
    http://blustemy.io/using-the-html-template-element/
*/

document.addEventListener("DOMContentLoaded", () => {
    function addColor() {
        const colorTemplate = document.getElementById("color-template"),
              colorsList = document.querySelector(".colors-list"),
              colorTemplateInstance = document.importNode(colorTemplate.content, true),
              colorTemplateInstanceContent = colorTemplateInstance.querySelector("figure"),
              colorTemplateInstanceRemoveButton = colorTemplateInstance.querySelector("button"),
              colorsListItem = document.createElement("li");

        let randomColor = `hsl(${ Math.floor(Math.random() * 360) }, 75%, 80%)`;
        colorTemplateInstanceContent.style.stroke = randomColor;
        colorTemplateInstanceContent.querySelector("figcaption code").textContent = randomColor;
        colorsListItem.appendChild(colorTemplateInstance);
        colorsList.appendChild(colorsListItem);

        colorTemplateInstanceRemoveButton.addEventListener("click", () => colorsList.removeChild(colorsListItem));
    }
    
    function removeColors() {
        const colorsList = document.querySelector(".colors-list");

        while (colorsList.firstChild) {
            colorsList.removeChild(colorsList.firstChild);
        }
    }
    
    document.querySelector(".colors-add").addEventListener("click", () => addColor());
    document.querySelector(".colors-clear").addEventListener("click", () => removeColors());
    
    for (let i = 0; i < 3; i++) {
        addColor();
    }
});

            
          
!
999px
Loading ..................

Console