Skip to content
Graphics
Photos
Logo’s
Fonts
Codes
Audios
Graphics
Photos
Logo’s
Fonts
Codes
Audios
Facebook
X-twitter
Youtube
Pinterest
Submit
HTML
CSS
JS
Copy
Run
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Random Gradient Generator</title> <!-- Google Font --> <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap" rel="stylesheet"> <!-- html2canvas Library for taking screenshots --> <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script> <!-- Stylesheet --> <style> /* Basic Reset */ * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { height: 100vh; /* The gradient will be set by JavaScript */ transition: background 0.5s; } .wrapper { background-color: #ffffff; width: 90%; max-width: 500px; /* Added max-width for better responsiveness on large screens */ padding: 50px 30px; border-radius: 8px; position: absolute; transform: translate(-50%, -50%); left: 50%; top: 50%; box-shadow: 0 20px 25px rgba(0, 0, 0, 0.15); } #output-color { width: 100%; height: 35vmin; max-height: 250px; border-radius: 5px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } #output-code { background-color: #f1f5fc; font-size: 16px; font-weight: 400; color: #3f415f; width: 100%; padding: 15px 10px; border-radius: 5px; margin: 20px 0 40px 0; border: 1px solid #e0e0e0; text-align: center; user-select: all; /* Make text easily selectable */ } .btn-container { display: flex; justify-content: space-between; /* Changed for better spacing */ gap: 15px; /* Added gap between buttons */ } .btn-container button { background-color: #587ef4; color: #ffffff; width: 100%; /* Make buttons fill the container */ padding: 15px 0; border-radius: 30px; font-size: 16px; font-weight: 500; cursor: pointer; border: none; outline: none; transition: background-color 0.3s; } .btn-container button:hover { background-color: #4a6ada; } </style> </head> <body> <div class="wrapper"> <div id="output-color"></div> <input type="text" id="output-code" readonly> <div class="btn-container"> <button id="generate-btn">Generate</button> <button id="copy-btn">Copy</button> <button id="download-btn">Download</button> </div> </div> <!-- Script --> <script> // Get references to all the DOM elements const generateBtn = document.getElementById("generate-btn"); const copyBtn = document.getElementById("copy-btn"); const downloadBtn = document.getElementById("download-btn"); const outputColor = document.getElementById("output-color"); const outputCode = document.getElementById("output-code"); const hexString = "0123456789abcdef"; /** * Generates a random hexadecimal color code. * @returns {string} A random color code, e.g., "#1a2b3c". */ const randomColor = () => { let hexCode = "#"; for (let i = 0; i < 6; i++) { hexCode += hexString[Math.floor(Math.random() * hexString.length)]; } return hexCode; }; /** * Generates a two-color linear gradient with a random angle and applies it. */ const generateGrad = () => { const colorOne = randomColor(); const colorTwo = randomColor(); const angle = Math.floor(Math.random() * 360); const gradient = `linear-gradient(${angle}deg, ${colorOne}, ${colorTwo})`; // Apply the generated gradient to the body background and the color preview box document.body.style.background = gradient; outputColor.style.background = gradient; // Display the CSS code in the input field outputCode.value = `background: ${gradient};`; }; /** * Copies the generated CSS code to the clipboard. */ copyBtn.addEventListener("click", () => { outputCode.select(); document.execCommand("copy"); // A non-blocking notification would be better, but alert is simple alert("Code Copied To Clipboard!"); }); /** * Downloads the generated gradient as a high-quality PNG image. */ downloadBtn.addEventListener("click", () => { // Use html2canvas to capture the outputColor div. // The 'scale' option increases the resolution of the output image. // A scale of 4 provides a high-quality image. html2canvas(outputColor, { scale: 4 }).then(canvas => { // Create a temporary anchor element to trigger the download const link = document.createElement("a"); link.download = "gradient-hq.png"; // Convert the canvas to a PNG image data URL link.href = canvas.toDataURL("image/png"); // Programmatically click the link to start the download link.click(); }); }); // Add event listener to the generate button generateBtn.addEventListener("click", generateGrad); // Generate a gradient when the page loads initially window.onload = generateGrad; </script> </body> </html>
Code copied to clipboard!