HTML CSS JS Projects
Project 1HTML · CSS · JS~45 min
Random Color Generator
DOM, loops, and the Clipboard API
Track progress0/3 days
What you'll learn
HTML structureCSS FlexboxcreateElementMath.random()clipboard
- Build a grid of color cards with JavaScript
- Generate random hex color codes
- Style cards dynamically with inline background colors
- Copy a color code to the clipboard on button click
Lesson & examples
Read the code, then the explanation — try changing values before the project.
Create elements in JS
const div = document.createElement("div");
div.classList.add("color-container");
document.querySelector(".container").appendChild(div);The DOM is a tree. createElement builds a node; appendChild attaches it to .container on the page.
- createElement
Creates an empty <div> in memory — not visible until appended.
- classList.add
Applies CSS class names from style.css.
Random hex digit
const chars = "0123456789ABCDEF";
const randomNum = Math.floor(Math.random() * chars.length);
const digit = chars.substring(randomNum, randomNum + 1);Math.random() gives 0–1. Multiply by length and floor to get a valid index into the hex character string.
Set background from JS
colorContainerEl.style.backgroundColor = "#" + newColorCode;Inline styles override CSS. Here the card background becomes whatever hex code you generated.
