Project-1 CSS Gradient generator
Published: 2024-04-02 07:30:47


 @Miguel Z. Nunez beginners Javascript Projects.

Learning how to use onclick to attach functions to buttons is indeed a fundamental skill in web development. It's amazing how such a simple concept can have such a wide range of applications. Being able to generate random hex colors and dynamically change the background color adds a dynamic and interactive element to your projects. Buttons are everywhere, and knowing how to make them do something meaningful when clicked is invaluable. The ability to handle user interactions like this opens up a world of possibilities for creating engaging and user-friendly experiences.

**index.html**
```
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
 <link rel="stylesheet" href="style.css"> 
 <title>Hex Colors</title>
</head>
<body>
   
  <h2>background: linear-gradient(45deg, #<span id="hex1">000000</span>, #<span id="hex2">000000</span>)</h2>
  <div>
    <button id="generate">Generate</button>
    <button id="copy">Copy</button>
  </div>
  
  
  <script src="script.js"></script>
</body>
</html>
```
**style.css**

```
body {
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  text-align: center;
  color: white;
  background-color: black;
}

h2 {
  text-shadow: 1px 1px 2px black;
}

button {
  padding: 10px;
  border: 2px solid white;
  border-radius: 5px;
  background-color: transparent;
  color: white;
  cursor: pointer;
  text-shadow: 1px 1px 2px black;
  box-shadow: 1px 1px 2px black;
  transition: 0.3s ease;
}

button:hover {
  transform: scale(1.1);
}

```

**script.js**
```
const hexNumbers = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
let hex1 = ""
let hex2 = ""

document.querySelector("#generate").addEventListener("click", () => {
  generateRandomHex()
})

document.querySelector("#copy").addEventListener("click", () => {
  copyHex()
})

function generateRandomHex(){
  hex1 = "", hex2 = ""
  for(let i = 0; i < 6; i++){
    hex1 += hexNumbers[Math.floor(Math.random() * hexNumbers.length)]
    hex2 += hexNumbers[Math.floor(Math.random() * hexNumbers.length)]
  }
  document.querySelector("#hex1").innerHTML = hex1
  document.querySelector("#hex2").innerHTML = hex2
  document.body.style.background = `linear-gradient(45deg, #${hex1}, #${hex2})`
}

function copyHex(){
  const gradient = `background: linear-gradient(45deg, #${hex1}, #${hex2})`
  navigator.clipboard.writeText(gradient)
}
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxdnp7zf5w79bc8ufwi0.png)

Codesandbox : [link](https://codesandbox.io/p/devbox/2j6g3c?migrateFrom=5h8rhk&layout=%257B%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522rootPanelGroup%2522%253A%257B%2522direction%2522%253A%2522horizontal%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522id%2522%253A%2522ROOT_LAYOUT%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522UNKNOWN%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522clui1e20q0006356h3a79ctew%2522%252C%2522sizes%2522%253A%255B58.31413153482589%252C41.68586846517411%255D%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522EDITOR%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522EDITOR%2522%252C%2522id%2522%253A%2522clui1e20q0002356hbzx5738r%2522%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522direction%2522%253A%2522horizontal%2522%252C%2522id%2522%253A%2522SHELLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522SHELLS%2522%252C%2522id%2522%253A%2522clui1e20q0004356hjfeku72n%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%257D%252C%257B%2522type%2522%253A%2522PANEL_GROUP%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522direction%2522%253A%2522vertical%2522%252C%2522id%2522%253A%2522DEVTOOLS%2522%252C%2522panels%2522%253A%255B%257B%2522type%2522%253A%2522PANEL%2522%252C%2522contentType%2522%253A%2522DEVTOOLS%2522%252C%2522id%2522%253A%2522clui1e20q0005356hnel0oheo%2522%257D%255D%252C%2522sizes%2522%253A%255B100%255D%257D%255D%252C%2522sizes%2522%253A%255B40%252C60%255D%257D%252C%2522tabbedPanels%2522%253A%257B%2522clui1e20q0002356hbzx5738r%2522%253A%257B%2522id%2522%253A%2522clui1e20q0002356hbzx5738r%2522%252C%2522tabs%2522%253A%255B%255D%257D%252C%2522clui1e20q0005356hnel0oheo%2522%253A%257B%2522id%2522%253A%2522clui1e20q0005356hnel0oheo%2522%252C%2522activeTabId%2522%253A%2522clui1eg5000df356hrp75dnmt%2522%252C%2522tabs%2522%253A%255B%257B%2522type%2522%253A%2522TASK_PORT%2522%252C%2522taskId%2522%253A%2522Development%2522%252C%2522port%2522%253A5000%252C%2522id%2522%253A%2522clui1eg5000df356hrp75dnmt%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522path%2522%253A%2522%252F%2522%257D%255D%257D%252C%2522clui1e20q0004356hjfeku72n%2522%253A%257B%2522id%2522%253A%2522clui1e20q0004356hjfeku72n%2522%252C%2522tabs%2522%253A%255B%257B%2522id%2522%253A%2522clui1e20q0003356hz0ye2e6v%2522%252C%2522mode%2522%253A%2522permanent%2522%252C%2522type%2522%253A%2522TERMINAL%2522%252C%2522shellId%2522%253A%2522clui1e27o000nd8f8gt4p2fgs%2522%257D%252C%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522Development%2522%252C%2522id%2522%253A%2522clui1ecaf00ab356hrchhsizy%2522%252C%2522mode%2522%253A%2522permanent%2522%257D%255D%252C%2522activeTabId%2522%253A%2522clui1ecaf00ab356hrchhsizy%2522%257D%257D%252C%2522showDevtools%2522%253Atrue%252C%2522showShells%2522%253Atrue%252C%2522showSidebar%2522%253Atrue%252C%2522sidebarPanelSize%2522%253A15%257D)