project demo
Introduction
In this project, we are building a useful Barcode Generator application. It allows users to enter any text or URL to create a barcode. You can choose different barcode formats and change the colors. This tool is great for learning how to use JavaScript libraries. We will combine HTML, CSS, and JavaScript to make it interactive.
Scrolldown to download zip file of this project 👇
html code
The HTML code builds the skeleton of our barcode generator app. It creates the input box where users type their text or data. It also adds dropdown menus for selecting formats and color pickers. We include a special SVG tag where the barcode will appear. Finally, it links the external JsBarcode library that does the heavy lifting.
Barcode Generator
Barcode Generator
css code
The CSS code is used to style the application and make it look good. It centers the main box on the screen and adds a nice shadow effect. We style the input fields and buttons to make them easy to click. It arranges the settings options in a neat row using layout techniques. The background colors and fonts are chosen to be clear and readable. We also ensure that the barcode area has enough space to display correctly. This code makes sure the app looks professional and is easy to use.
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap');
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html,
body {
display: grid;
height: 100%;
background: #f0f2f5;
place-items: center;
font-family: 'Poppins', sans-serif;
}
.wrapper {
background: #fff;
width: 500px;
padding: 30px;
border-radius: 10px;
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
gap: 20px;
}
.wrapper h1 {
font-size: 24px;
font-weight: 600;
text-align: center;
color: #333;
}
.form-group input {
width: 100%;
height: 50px;
font-size: 16px;
padding: 0 15px;
border: 1px solid #ddd;
border-radius: 5px;
outline: none;
transition: border-color 0.3s;
}
.form-group input:focus {
border-color: #4a90e2;
}
.settings {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 15px;
}
.control {
display: flex;
flex-direction: column;
gap: 5px;
}
.control label {
font-size: 14px;
color: #666;
}
.control select,
.control input[type="color"] {
width: 100%;
height: 40px;
border: 1px solid #ddd;
border-radius: 5px;
padding: 0 10px;
outline: none;
cursor: pointer;
background: #fff;
}
button {
width: 100%;
height: 50px;
border: 0;
outline: none;
background: #4a90e2;
border-radius: 5px;
color: #fff;
font-size: 16px;
font-weight: 500;
cursor: pointer;
transition: background 0.3s;
}
button:hover {
background: #357abd;
}
#barcode-box {
display: none;
/* Hidden until generated */
text-align: center;
margin-top: 10px;
border: 1px solid #eee;
padding: 10px;
border-radius: 5px;
}
#barcode {
max-width: 100%;
height: auto;
}
javascript code
The JavaScript code provides the logic to make the app function correctly. It waits for the user to click the generate button to start working. It reads the text, format, and color choices the user has selected. Then, it calls the JsBarcode library to draw the barcode on the screen. It updates the SVG element with the correct lines and numbers dynamically. If the user changes a setting, the script ensures the barcode updates. This code connects the user interface with the barcode generation engine.
function generateBarcode() {
const url = document.getElementById("urlInput").value.trim();
const format = document.getElementById("barcodeFormat").value;
const lineColor = document.getElementById("lineColor").value;
const background = document.getElementById("bgColor").value;
const displayValue = document.getElementById("displayValue").value === "true";
if (url) {
try {
JsBarcode("#barcode", url, {
format: format,
lineColor: lineColor,
background: background,
displayValue: displayValue,
margin: 10,
});
document.getElementById("barcode-box").style.display = "block";
} catch (e) {
document.getElementById("barcode-box").style.display = "none";
let msg = "Invalid input for " + format + " format.";
if (format === "EAN13") {
msg += "\n\nEAN-13 requires exactly 12 or 13 numeric digits.";
} else if (format === "UPC") {
msg += "\n\nUPC requires exactly 11 or 12 numeric digits.";
} else if (format === "CODE39") {
msg +=
"\n\nCode 39 supports uppercase letters, numbers, and symbols (- . $ / + % space).";
}
alert(msg);
}
}
}