Pen Settings

HTML

CSS

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

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

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.

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

              
                <div class="prompt">
        <p>Type text and press enter to create tags. Add up to 10 tags.</p>
        <p class="tag-counter">Remaining Tags: <span id="tag-count">10</span></p>
    </div>

    <div class="input-wrap">
        <div class="scroll-wrap">
            <div id="tag-input">
                <input type="text" placeholder="Type Tags">
            </div>
        </div>
        <button value="disabled">Add Tags</button>
    </div>
              
            
!

CSS

              
                :root {
  --input-width: inputWidth;
  --deleteColor: #aaa;
}

* {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  scrollbar-width: thin !important;
}

p {
  margin: 0;
}

.prompt {
  text-align: center;
  margin: 20px 0;
}

.tag-counter {
  margin: 10px 0;
}

#tag-count {
  font-weight: bold;
}

.input-wrap {
  display: grid;
  grid-template-columns: 1fr auto;
  width: 50%;
  margin: 0 auto;
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.3);
}

.scroll-wrap {
  position: relative;
  overflow: scroll;
  border: 2px solid white;
  border-radius: 5px 0 0 5px;
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.3);
}

.active {
  border: 2px solid black;
}

div#tag-input {
  display: inline-flex;
  position: relative;
  width: 100%;
  border-radius: 5px 0 0 5px;
  font-size: 18px;
  transition: border 0.25s ease-in-out;
}

div#tag-list {
  display: inline-block;
  position: relative;
  height: 100%;
  background-color: green;
}

input {
  display: inline-block;
  position: relative;
  height: 100%;
  min-width: 50%;
  width: 100%;
  box-sizing: border-box;
  padding: 10px 0;
  border: none;
  font-size: 14px;
  letter-spacing: 1px;
  margin: 0 10px;
}

input:focus {
  outline: none;
}

.input-disabled {
  min-width: 10px;
  background: white;
}

.live {
  background-color: #ccc;
}

.tag {
  display: inline-block;
  align-self: center;
  justify-self: center;
  font-size: 18px;
  box-sizing: border-box;
  font-size: 14px;
  letter-spacing: 1px;
  background-color: #efefef;
  border-radius: 10px;
  box-shadow: inset 0px 0px 0px 2px #ddd;
  padding: 4px 4px 4px 10px;
  margin-left: 10px;
  transition: all 0.1s ease-in;
}

.tag:after {
  box-sizing: border-box;
  content: "\0078";
  margin: 0 5px;
  transition: all 0.2s ease-in;
  color: var(--deleteColor);
}

.tag:hover {
  cursor: pointer;
  box-shadow: inset 0px 0px 0px 2px #444;
}

.tag:hover:after {
  color: #444;
}

button {
  background-color: rgba(0, 0, 0, 1);
  color: #efefef;
  border: none;
  font-size: 13px;
  letter-spacing: 1px;
  padding: 0 20px;
  border-radius: 0 5px 5px 0;
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.3);
}

button:hover {
  cursor: pointer;
  background-color: rgba(0, 0, 0, 0.8);
}

.confirm-wrap {
  text-align: center;
  margin-top: 25px;
}

.confirm {
  display: inline-block;
  letter-spacing: 1px;
  padding: 10px 20px;
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.3);
}

              
            
!

JS

              
                const inputWrap = document.querySelector(".input-wrap");
const scrollWrap = document.querySelector(".scroll-wrap");
const div = document.querySelector("#tag-input");
const list = document.querySelector("#tag-list");
const input = document.querySelector("input");
const button = document.querySelector("button");
const tagCount = document.querySelector("#tag-count");

// reset input value on reload
input.value = "";

const divWidth = div.getBoundingClientRect().width;
let inputWidth = input.getBoundingClientRect().width;

let inputActive = false;
let inputArr = [];
let tagArr = [];
let tagWidthArr = [];
let newWidth;
let totalWidth;
let widthIndex;

let pendingDelete;

document.addEventListener("click", (e) => {
  console.log("e", e);
  console.log("e.target", e.target);
  console.log("this", this);
});

// TAG COLORS
const blue = ["41, 128, 185", "41, 128, 185, 0.1"];
const green = ["39, 174, 96", "39, 174, 96, 0.1"];
const turqoise = ["22, 160, 133", "22, 160, 133, 0.1"];
const purple = ["142, 68, 173", "142, 68, 173, 0.1"];
const orange = ["211, 84, 0", "211, 84, 0, 0.1"];
const red = ["192, 57, 43", "192, 57, 43, 0.1"];

const colorArr = [blue, green, turqoise, purple, orange, red];

let randomColor;

window.addEventListener("click", (e) => {
  if (inputActive === true && e.target !== input) {
    scrollWrap.classList.remove("active");
    inputActive = false;
    console.log("removed");
  }
});

function createTag(e) {
  e.preventDefault();
  // removes tag pending deletion
  if (e.key !== "Backspace" && pendingDelete === true) {
    pendingTag.style.boxShadow = "unset";
    pendingTag.style.setProperty("--deleteColor", "#aaa");
    pendingTag = "";
    pendingDelete = false;
  }

  if (e.key === "Enter") {
    // prevents creating empty tag
    if (input.value === "") {
      return alert("Tag cannot be empty");
    }

    // validates input
    const inputValidation = input.value.match(/[^a-zA-Z0-9]/g);
    if (inputValidation) {
      return alert(
        "Invalid characters present. Tags can only contain letters or numbers."
      );
    }
    
    const tagInput = input.value.trim().toLowerCase();

    // check if tag already exists
    let checkMatch = tagArr.find((tag) => tag === tagInput);
    if (checkMatch !== undefined) {
      return alert("tag already created");
    }
    tagArr.push(tagInput);

    input.value = '';

    const tag = document.createElement("span");
    tag.innerText = tagInput;
    tag.id = tagInput;
    tag.classList.add("tag");

    randomColor = Math.floor(Math.random() * colorArr.length);
    tag.style.color = `rgb(${colorArr[randomColor][0]}`;
    tag.style.backgroundColor = `rgba(${colorArr[randomColor][1]}`;
    tagCount.innerText = 10 - tagArr.length;
    
    div.insertBefore(tag, input);
    let tagWidth;
    if (tagWidthArr.length < 1) {
      tagWidth = tag.getBoundingClientRect().width + 10;
    } else if (tagWidthArr.length > 0) {
      tagWidth = tag.getBoundingClientRect().width + 10;
    }
    tagWidthArr.push(tagWidth);
    const tagWidthSum = tagWidthArr.reduce((a, b) => a + b, 0);
    newWidth = inputWidth - tagWidthSum;
    totalWidth = newWidth + tagWidthSum;
    
    if (tagWidthSum >= divWidth) {
      input.style.width = "1000px";
    }
    
    input.style.width = `${newWidth}px`;

    // scrolls to end of div if overflow
    if (scrollWrap.scrollLeft !== divWidth) {
      scrollWrap.scrollTo(scrollWrap.scrollLeft + divWidth, 0);
      console.log(scrollWrap.scrollLeft);
    }

    tag.addEventListener("click", removeTag);

    if (tagArr.length === 10) {
      tagCount.innerText = "Max number of tags added";
      input.classList.add("input-disabled");
      input.disabled = true;
    }
  }
}

// remove tags from list by click
function removeTag(e) {
  if (tagArr.length === 10) {
    input.classList.remove("input-disabled");
    input.disabled = false;
  }

  // find tag and tag width in array
  const selectedTag = this.innerText;
  const tagIndex = tagArr.findIndex((tag) => tag === selectedTag);
  const removedTag = tagArr.splice(tagIndex, 1);
  widthIndex = tagWidthArr.splice(tagIndex, 1);

  // remove tag from DOM and update input width
  const tagWidthSum = tagWidthArr.reduce((a, b) => a + b, 0);
  
  let totalWidth;
  if (tagWidthArr.length === 0) {
    newWidth = inputWidth;
  } else if (tagWidthArr.length > 0) {
    newWidth = parseInt(newWidth) + parseInt(widthIndex);
  }
  input.style.width = `${newWidth}px`;
  const toDelete = document.getElementById(selectedTag);
  toDelete.remove(1);

  tagCount.innerText = `${10 - tagArr.length}`;
}

div.addEventListener("keyup", createTag);

let pendingItem;
let pendingTag;

// remove tag by backspacing
function backspaceTag(e) {
  if (e.key === "Backspace" && input.value.length === 0) {
    if (pendingDelete !== true) {
      pendingItem = tagArr.slice(tagArr.length - 1);
      pendingTag = document.getElementById(`${pendingItem}`);
      pendingDelete = true;
      pendingTag.style.boxShadow = "0px 0px 0px 2px black";
      pendingTag.style.setProperty("--deleteColor", "black");
    } else if (pendingDelete === true) {
      pendingItem = "";
      tagArr.pop();
      widthIndex = tagWidthArr.pop();

      const tagWidthSum = tagWidthArr.reduce((a, b) => a + b, 0);
      let totalWidth;
      if (tagWidthArr.length === 0) {
        newWidth = inputWidth;
      } else if (tagWidthArr.length > 0) {
        newWidth = parseInt(newWidth) + parseInt(widthIndex);
      }
      input.style.width = `${newWidth}px`;

      pendingTag.remove();
      pendingTag = "";
      pendingDelete = false;

      tagCount.innerText = `${10 - tagArr.length}`;
    }
  }
}

input.addEventListener("keyup", backspaceTag);

input.addEventListener("click", (e) => {
  console.log(e.target);
  if (inputActive === false) {
    scrollWrap.classList.add("active");
    inputActive = true;
    console.log(inputActive);
  }
});

function submitTags(e) {
  e.preventDefault();
  // prevents submitting if there are no tags
    if (input.value.length > 0 && tagArr.length < 1) {
      return alert("Create tags before submitting");
    }
  console.log(tagArr);

  const html = `
            <div class="confirm-wrap">
            <div class="confirm">
            <p>Tags have been created ✔️</p>
            </div>
            </div>`;

  inputWrap.insertAdjacentHTML("afterend", html);
  const confirmOverlay = document.querySelector(".confirm-wrap");

  window.setTimeout(function () {
    confirmOverlay.remove();
    tagArr = [];
    document.querySelectorAll(".tag").forEach((tag) => tag.remove());
    tagCount.innerText = `${10 - tagArr.length}`;
    input.style.width = `${inputWidth}px`;
  }, 2000);
}

button.addEventListener("click", submitTags);

              
            
!
999px

Console