๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปColorful Boxes In HTML

๐Ÿงถ Tags:: #HTML #Random_Projects
2022-10-13 - 04:26

HTML Code

<!DOCTYPE html>

<html lang="en">

ย  <head>

ย  ย  <meta charset="UTF-8" />

ย  ย  <meta http-equiv="X-UA-Compatible" content="IE=edge" />

ย  ย  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

ย  ย  <title>Document</title>

ย  ย  <link rel="stylesheet" href="style.css" />

ย  ย  <script src="app.js" defer></script>

ย  </head>

ย  <body>

ย  ย  <div class="container" id="container"></div>

ย  </body>

</html>

STYLE.CSS

* {

ย  ย  box-sitting:border-box

}

body{

ย  ย  background-color: #111;

ย  ย  display: flex;

ย  ย  align-items: center;

ย  ย  justify-content: center;

ย  ย  height: 100vh;

ย  ย  overflow: hidden;

ย  ย  margin: 0;

}

  

.container {

ย  ย  display: flex;

ย  ย  align-items: center;

ย  ย  justify-content: center;

ย  ย  flex-wrap: wrap;

ย  ย  max-width: 1600px;

  

}

  

.square {

ย  ย  background-color: #1d1d1d;

ย  ย  box-sizing: 0 0 2px black;

ย  ย  height: 50px;

ย  ย  width: 50px;

ย  ย  margin: 2px;

ย  ย  transition: 2s ease;

}

  

.square:hover {

ย  ย  transition-duration: 0s;

  

}

APP.JS

const container = document.getElementById('container')

const colors = ['#e74c4c', '#8e44ad', '#3498db', '#e67e22', '#2ecc71']

const SQUARES = 522

  

for(let i = 0; i < SQUARES; i++) {

ย  ย  const square = document.createElement('div')

ย  ย  square.classList.add('square')

  

ย  ย  square.addEventListener('mouseover', () => setColor(square))

ย  ย  square.addEventListener('mouseout', () => removeColor(square))

  

ย  ย  container.appendChild(square)

}

  

function setColor(element) {

ย  ย  const color = getRandom()

ย  ย  element.style.background = color

ย  ย  element.style.boxShadow = '0 0 2px ${color}, 0 0 10px $(color)'

}

  
  

function removeColor(element) {

ย  ย  element.style.background = '#1d1d1d'

ย  ย  element.style.boxShadow = '0 0 2px #000'

}

  

function getRandom() {

ย  ย  return colors[Math.floor(Math.random() * colors.length)]

}