QR codes are widely used in our digital world, whether it’s for product labeling, websites, or even Wi-Fi sharing. In this article, firstly, we’ll walk you through the process of building a QR code generator from scratch using HTML, CSS, and JavaScript.
To start with, you’ll need three files:
First, create your index.html
file. This file will include an input box for users to enter text or URLs and a button to generate the QR code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QR Code Generator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Create Your QR Code</h1>
<input type="text" id="text-input" placeholder="Enter text or URL" />
<button id="generate-btn">Generate QR Code</button>
<div id="qr-container"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qrcodejs/1.0.0/qrcode.min.js"></script>
<script src="script.js"></script>
</body>
</html>
<div>
with the id qr-container
where the generated QR code will be displayed.Image 1: Basic HTML Structure
Next, let’s make the page more visually appealing with some CSS. Create a styles.css file with the following content:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f7fa;
}
.container {
background-color: #fff;
padding: 30px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
text-align: center;
}
h1 {
margin-bottom: 20px;
}
input[type="text"], button {
padding: 10px;
margin-bottom: 15px;
font-size: 16px;
border-radius: 6px;
border: 1px solid #ccc;
}
button {
background-color: #4CAF50;
color: white;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
#qr-container {
margin-top: 20px;
}
flexbox
, so the QR code generator form is displayed in the middle of the screen.#qr-container
is where the QR code will be displayed.Image 2: Styled Webpage
Next, we need to add functionality to our QR code generator using JavaScript. Create a script.js file with the following content:
document.getElementById('generate-btn').addEventListener('click', function() {
const textInput = document.getElementById('text-input').value;
const qrContainer = document.getElementById('qr-container');
// Clear the previous QR code
qrContainer.innerHTML = '';
if (textInput) {
// Generate the QR code
const qrCode = new QRCode(qrContainer, {
text: textInput,
width: 200,
height: 200,
});
} else {
alert('Please enter a valid text or URL.');
}
});
QRCode
class from the qrcode.min.js
library.Image 3: Functional QR Code Generator
Once implemented, the page will generate a QR code when the user enters a URL or text and clicks the button.
Ultimately, you’ve now created a fully functional QR code generator using just HTML, CSS, and JavaScript! With this tool, users can easily input text or URLs to generate QR codes instantly. This project is a great example of how JavaScript can bring interactive elements to web pages, and you can customize it further to fit your specific needs. Whether you’re using it for personal projects or business purposes, this simple QR code generator is a useful tool to have in your web development toolkit. Happy coding!
The difference between == and === in JavaScript
When debugging JavaScript, you’ll often encounter ReferenceError and TypeError. While both indicate something went wrong,…
When selecting DOM elements in JavaScript, two common methods are document.querySelector() and document.getElementById(). But which…
When starting a JavaScript project, one of the first decisions you’ll face is: Should I…
Software development is one of the most valuable skills you can learn. From building websites…
In JavaScript, arrays are used to store multiple values in a single variable. While JavaScript…
Containerization is a lightweight form of virtualization that packages an application and its dependencies into…