JSFiddle - React, Tailwind, and code Playground

by Sergei Sokolov

HTML

<button>Нажми меня</button>
<div id="container"></div>

CSS

div.box {width:42px; height:42px; float:left; margin: 0 2px 2px 0}

JavaScript

/**
 * для вопроса https://toster.ru/q/577802
 * Как сделать случайный цвет у div?
 */
function onClick() {
  const div = document.createElement("div");
  div.classList.add("box");
  div.style = "background-color: " + 
    '#' + (Math.random().toString(16) + '000000').substring(2,8).toUpperCase()
  ;
  document.getElementById("container").appendChild(div);
}

document.querySelector("button").addEventListener("click", onClick);

for(let i = 5; i > 0; i--) onClick();