QR Code Generator
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
padding: 20px;
width: 100%;
max-width: 500px;
text-align: center;
}
h1 {
color: #4CAF50;
}
.input-container {
margin: 20px 0;
}
input[type="text"] {
padding: 10px;
width: 70%;
border: 2px solid #ddd;
border-radius: 4px;
}
button {
padding: 10px 15px;
border: none;
background-color: #4CAF50;
color: white;
cursor: pointer;
border-radius: 4px;
margin-top: 10px;
font-size: 16px;
}
button:hover {
background-color: #45a049;
}
#qrcode {
margin-top: 20px;
}
#qr-options {
margin-top: 20px;
}
input[type="color"] {
margin-left: 10px;
cursor: pointer;
}
#download-btn {
margin-top: 20px;
padding: 10px 15px;
background-color: #2196F3;
}
#download-btn:hover {
background-color: #1976D2;
}
@media (max-width: 600px) {
.container {
padding: 15px;
}
input[type="text"] {
width: 100%;
}
}
document.getElementById('generate-btn').addEventListener('click', function() {
const text = document.getElementById('qr-text').value.trim();
const color = document.getElementById('color-picker').value;
if (text === '') {
alert("Please enter some text or a URL!");
return;
}
// Clear previous QR code
document.getElementById('qrcode').innerHTML = '';
// Generate new QR code
new QRCode(document.getElementById('qrcode'), {
text: text,
width: 200,
height: 200,
colorDark: color,
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
});
// Function to download the QR code
document.getElementById('download-btn').addEventListener('click', function() {
const qrCodeElement = document.getElementById('qrcode');
const canvas = qrCodeElement.querySelector('canvas');
if (canvas) {
const link = document.createElement('a');
link.href = canvas.toDataURL('image/png');
link.download = 'qr_code.png';
link.click();
} else {
alert("Please generate a QR code first.");
}
});