Print Barcode Labels in HTML

How to Print Barcode Labels in HTML

Barcode labels are used to store and retrieve information quickly. They consist of vertical lines (bars) that represent data, such as product details. These bars can be scanned using a barcode reader, converting the pattern into readable information.

Barcode labels improve efficiency and accuracy in tracking products. They reduce human errors in data entry and make inventory management faster. By scanning barcodes, businesses can streamline processes like checkout, shipping, and inventory control. This saves time and reduces costs, enhancing overall productivity.

Getting Started with HTML for Barcode Labels

These steps will guide you in setting up the necessary components to start creating and printing barcode labels using HTML.

Basic HTML Structure

To create barcode labels using HTML, start with a basic HTML structure. This includes setting up the HTML document with necessary tags like <!DOCTYPE html>, <html>, <head>, and <body>. The head section contains meta information, such as character encoding and the title of the document, while the body section holds the content that will be displayed on the webpage. This foundational setup ensures your webpage is well-structured and ready for adding barcode elements.

Including CSS for Styling

CSS is essential for styling your barcode labels. It helps you define how the labels look, such as their size, margin, and alignment. With CSS, you can customize the appearance of your labels to fit your needs. For instance, you can center the barcode on the page, set specific dimensions, and adjust font sizes for any accompanying text. This makes your labels visually appealing and professional.

Adding JavaScript Libraries

JavaScript libraries simplify the process of generating barcodes. Libraries like JsBarcode or Barcode.js are popular choices because they offer ready-made functions to create barcodes. By including these libraries in your HTML file, you can easily generate and customize barcodes. These libraries support various barcode formats, such as CODE128 and EAN, and allow you to adjust settings like barcode width, height, and text display options.

Creating a Barcode Element

With the basic structure, CSS, and JavaScript library in place, you can create a barcode element. This element will display the barcode on your webpage. Using JavaScript, you can write a script that generates the barcode based on the data you provide. The library functions will render the barcode in the specified element, ensuring it is properly formatted and readable. You can also add text labels or additional elements to provide context or instructions for the barcode.

Generating Barcodes

Choosing a Barcode Format

The first step in generating barcodes is selecting the appropriate barcode format. Common formats include CODE128, EAN, UPC, and QR codes. Each format serves different purposes: CODE128 is versatile and widely used in logistics and inventory, EAN and UPC are common in retail, and QR codes are often used for quick access to URLs or other data. Choose the format that best suits your needs.

Using JavaScript Libraries

To generate barcodes efficiently, use JavaScript libraries like JsBarcode or Barcode.js. These libraries provide simple methods to create barcodes without extensive coding. They support various barcode formats and offer customization options for appearance and data encoding.

Creating a Barcode Element

Once you have chosen a format and included a library, the next step is to create a barcode element. This involves defining an HTML element (like a div or canvas) where the barcode will be displayed. The JavaScript library will render the barcode within this element based on the data you provide.

Generating Barcodes with JavaScript

Using your chosen library, write a JavaScript function to generate the barcode. This function typically includes the following steps. Select the HTML element where the barcode will appear. Call the library function to generate the barcode, passing the required data and configuration options. Customize the barcode’s appearance by adjusting settings like width, height, and text display.

Customizing Barcode Appearance

Most libraries allow you to customize the barcode’s appearance. You can set the width and height of the barcode, choose whether to display the data as text below the barcode, and adjust colors to match your design. These customization options help ensure your barcode labels are both functional and visually consistent with your branding.

Designing the Barcode Label Layout

  1. Structuring the Label with HTML and CSS: Designing a barcode label layout involves structuring the label using HTML and CSS. Start by creating a container element for your label. Inside this container, include elements for the barcode, any text, and additional graphics if needed. Use CSS to define the size, margins, padding, and overall layout to ensure the label is well-organized and visually appealing.
  2. Positioning the Barcode: Positioning the barcode correctly is crucial for readability and functionality. Typically, the barcode should be placed in a prominent area where it is easily scannable. Use CSS to center the barcode within the label or align it as required. Ensure there is enough space around the barcode to avoid any interference with other elements.
  3. Adding Text and Other Elements: In addition to the barcode, you might need to include text such as product names, descriptions, or prices. Use HTML to add these text elements within the label container. CSS can help you style the text, set font sizes, and adjust positioning to ensure it is readable and complements the barcode.
  4. Creating a Consistent Design: A consistent design is important for professional-looking labels. Use CSS to maintain uniform styles across all labels. This includes setting standard dimensions, fonts, and spacing. Consistency helps in creating a cohesive look and makes it easier to print multiple labels without discrepancies.
  5. Including Logos or Images: If your barcode labels need to include logos or images, make sure they are high-quality and properly sized. Use HTML to insert these images within the label container and CSS to position and scale them appropriately. Ensure the images do not overlap with the barcode and maintain a clean layout.
  6. Responsive Design for Different Label Sizes: If you need to create barcode labels of various sizes, consider using responsive design techniques. Use CSS media queries to adjust the layout based on the label size. This ensures your labels look good on different label sizes and maintain readability and functionality.

Printing the Barcode Labels

To ensure your barcode labels print correctly, you need to set up specific CSS styles for printing. This involves defining styles that apply only when printing, such as setting dimensions, margins, and removing unnecessary elements. You should hide navigation bars or background images that are not needed on the printed label to make the printout clean and professional. Use media queries to apply these print-specific styles, ensuring the labels look as intended when printed.

Using JavaScript can provide a user-friendly way to print barcode labels. Adding a print button and attaching a print function to it allows users to print the labels easily without navigating through browser settings. This approach simplifies the printing process, making it more accessible for users who might not be tech-savvy.

When printing multiple barcode labels on a single page, ensure they are arranged correctly. Define the layout using CSS, making sure each label is within its own container and properly spaced to avoid overlap. Utilize CSS grid or flexbox to create a responsive layout that adjusts to fit as many labels as possible on a page. This ensures efficient use of paper and a neat presentation of the labels.

Testing and Debugging

By thoroughly testing and debugging your barcode labels, you can ensure they are correctly formatted, readable, and print-ready.

Previewing the Barcode Labels

Before printing, always preview your barcode labels to ensure they appear correctly on the screen. Use your browser’s print preview feature to check the layout, alignment, and overall design. Make sure all elements, such as barcodes, text, and logos, are positioned correctly and are not cut off or misaligned. Adjust your HTML and CSS as needed based on this preview.

Ensuring Barcode Readability

To ensure your barcodes are readable, use a barcode scanner or a smartphone app to test the barcode on the screen. Verify that the scanner can accurately read the barcode and that the data encoded is correct. Check for any distortions or readability issues that might arise due to poor resolution or improper scaling.

Common Issues and Fixes

While testing, you might encounter common issues such as barcodes not displaying correctly, elements overlapping, or print layout problems. For barcodes not displaying, ensure that the JavaScript library is correctly link in HTML and the barcode data is properly formatted. For overlapping elements, adjust the CSS margins and padding. If the print layout is not as expected, refine the print-specific CSS styles to handle dimensions, spacing, and page breaks more effectively.

Testing Multiple Labels

When printing multiple labels on a single page, it’s essential to test this layout to ensure each label is correctly positioned and uniformly spaced. Print a test page with several labels to verify the layout, ensuring there are no issues like labels being cut off at the page edges or inconsistent spacing between labels. Make adjustments to the CSS grid or flexbox settings to achieve a consistent and professional appearance.

Verifying Print Quality

Print a test batch of labels to verify the print quality. Ensure the barcodes are sharp and clear, and the text is legible. High-quality printers and good resolution settings are crucial for maintaining print clarity. If the print quality is poor, consider using higher resolution images or vector graphics for the barcodes and adjusting the printer settings for better output.

Debugging Tips

If you encounter issues during testing, use browser developer tools to inspect elements and debug problems. Check the console for any JavaScript errors that might affect barcode generation. Use the elements inspector to review the HTML and CSS, ensuring styles are applied correctly and there are no unexpected overrides or conflicts. Debugging tools can help identify and resolve issues quickly, ensuring a smooth process from design to print.


Printing barcode labels in HTML involves a structured approach that combines HTML, CSS, and JavaScript. By starting with a clear HTML layout and incorporating CSS for styling, you can create visually appealing labels that are easy to customize. JavaScript libraries like JsBarcode simplify barcode generation, allowing you to encode data efficiently and display it on your labels.

Setting up print styles ensures your labels appear correctly when printed, while testing and debugging are crucial steps to verify barcode readability and print quality. Previewing labels, ensuring barcode scans work accurately, and addressing common issues like layout adjustments or print quality improvements are essential for a seamless printing process.

Share the article

Written By

Author Avatar

July 7, 2024

Ayesha Khan is a highly skilled technical content writer based in Pakistan, known for her ability to simplify complex technical concepts into easily understandable content. With a strong foundation in computer science and years of experience in writing for diverse industries, Ayesha delivers content that not only educates but also engages readers.

Launch Your Vision

Ready to start your project? Let's work together to make it happen! Get in touch with us today and let's bring your ideas to life.

Get In Touch