E-commerce & Retail

Free Product Badge Widget

Add Sale, New, or Bestseller corner badges to any product or page element.

A tiny product badge that renders inline as a row of labels, or overlays any element on the page — a product image, card, or banner — pinned to the corner you choose. It is built for e-commerce stores, landing pages, and product grids that need quick visual callouts. Install it with a single script tag — no dependencies, no accounts.

Live demo

Embed this widget

Paste either snippet into your page — a Custom HTML block on WordPress, Shopify, or Squarespace works too. Both snippets update live as you customize, so what you copy is exactly what you configured.

Hosted (recommended)html
<script src="https://hayneslab.dev/widgets/product-badge.js" data-hl-text="Sale, New, Bestseller" async></script>
<!-- Free Product Badge widget by haynes lab — https://hayneslab.dev/tools/widgets/product-badge -->
Inline (self-contained)html
<!-- Free Product Badge widget by haynes lab — https://hayneslab.dev/tools/widgets/product-badge -->
<script data-hl-text="Sale, New, Bestseller">
/*!
 * haynes lab — Product Badge widget (free)
 * Docs & embed code: https://hayneslab.dev/tools/widgets/product-badge
 */
(function () {
  'use strict';

  var PREFIX = 'hlw-product-badge';
  var STYLE_ID = 'hlw-product-badge-styles';

  var PRESETS = {
    sale: '#dc2626',
    new: '#16a34a',
    bestseller: '#d97706',
    'best seller': '#d97706',
    hot: '#ea580c',
    limited: '#7c3aed',
    sold: '#6b7280',
    'sold out': '#6b7280',
  };

  var POSITIONS = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];

  function findScript() {
    return document.currentScript ||
      document.querySelector('script[src*="widgets/product-badge.js"]');
  }

  function injectStyles() {
    if (document.getElementById(STYLE_ID)) return;
    var css =
      '.' + PREFIX + ', .' + PREFIX + ' * { box-sizing: border-box; }' +
      '.' + PREFIX + ' { font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }' +
      '.' + PREFIX + '--inline { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 8px; }' +
      '.' + PREFIX + '__tag { display: inline-block; padding: 4px 10px; border-radius: 4px;' +
      ' font-size: 12px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase;' +
      ' line-height: 1.4; white-space: nowrap; }' +
      '.' + PREFIX + '__tag--overlay { position: absolute; z-index: 10; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }' +
      '.' + PREFIX + '__tag--top-left { top: 8px; left: 8px; }' +
      '.' + PREFIX + '__tag--top-right { top: 8px; right: 8px; }' +
      '.' + PREFIX + '__tag--bottom-left { bottom: 8px; left: 8px; }' +
      '.' + PREFIX + '__tag--bottom-right { bottom: 8px; right: 8px; }' +
      '.' + PREFIX + '__credit { font-size: 11px; display: inline-flex; align-items: center; gap: 4px;' +
      ' color: #9ca3af; text-decoration: none; }' +
      '.' + PREFIX + '__credit svg { display: block; }' +
      '.' + PREFIX + '__credit:hover { color: #6b7280; text-decoration: underline; }' +
      '.' + PREFIX + '__credit--overlay { position: absolute; right: 6px; bottom: 4px; z-index: 10; opacity: 0.8; }';
    var style = document.createElement('style');
    style.id = STYLE_ID;
    style.textContent = css;
    document.head.appendChild(style);
  }

  function el(tag, className, text) {
    var node = document.createElement(tag);
    if (className) node.className = className;
    if (text != null) node.textContent = text;
    return node;
  }

  function logoSvg() {
    var svgNS = 'http://www.w3.org/2000/svg';
    var svg = document.createElementNS(svgNS, 'svg');
    svg.setAttribute('viewBox', '0 0 32 32');
    svg.setAttribute('width', '13');
    svg.setAttribute('height', '13');
    svg.setAttribute('aria-hidden', 'true');
    svg.setAttribute('focusable', 'false');
    function circle(cx, cy, r, attrs) {
      var c = document.createElementNS(svgNS, 'circle');
      c.setAttribute('cx', cx);
      c.setAttribute('cy', cy);
      c.setAttribute('r', r);
      for (var key in attrs) c.setAttribute(key, attrs[key]);
      svg.appendChild(c);
    }
    circle(16, 16, 16, { fill: '#0f0a0a' });
    circle(16, 16, 12, { fill: 'none', stroke: '#fff5f5', 'stroke-width': 1, opacity: 0.3 });
    circle(16, 16, 9, { fill: 'none', stroke: '#fff5f5', 'stroke-width': 1, opacity: 0.5 });
    circle(16, 16, 6, { fill: 'none', stroke: '#fff5f5', 'stroke-width': 1.2, opacity: 0.9 });
    circle(16, 16, 3, { fill: '#06b6d4' });
    circle(24.5, 7.5, 2, { fill: '#f97316' });
    circle(7, 16, 1.8, { fill: '#f97316' });
    circle(20, 20, 1.5, { fill: '#f97316' });
    return svg;
  }

  function makeCredit(overlay) {
    var credit = el('a', PREFIX + '__credit' + (overlay ? ' ' + PREFIX + '__credit--overlay' : ''));
    credit.appendChild(logoSvg());
    credit.appendChild(document.createTextNode('Powered by haynes lab'));
    credit.href = 'https://hayneslab.dev/tools/widgets/product-badge';
    credit.target = '_blank';
    credit.rel = 'noopener';
    return credit;
  }

  function badgeColor(label, bg) {
    if (bg) return bg;
    var preset = PRESETS[String(label).trim().toLowerCase()];
    return preset || '#f97316';
  }

  function makeTag(label, bg, textColor, position) {
    var cls = PREFIX + '__tag';
    if (position) cls += ' ' + PREFIX + '__tag--overlay ' + PREFIX + '__tag--' + position;
    var tag = el('span', cls, label);
    tag.style.backgroundColor = badgeColor(label, bg);
    tag.style.color = textColor;
    return tag;
  }

  function build(script) {
    var text = script.getAttribute('data-hl-text') || 'Sale';
    var bg = script.getAttribute('data-hl-bg') || '';
    var textColor = script.getAttribute('data-hl-text-color') || '#ffffff';
    var position = script.getAttribute('data-hl-position') || 'top-right';
    if (POSITIONS.indexOf(position) === -1) {
      console.warn('[haynes lab product-badge] Unknown data-hl-position "' + position + '". Using "top-right".');
      position = 'top-right';
    }

    var labels = text.split(',').map(function (s) { return s.trim(); }).filter(Boolean);
    if (!labels.length) {
      console.warn('[haynes lab product-badge] data-hl-text is empty. Nothing rendered.');
      return;
    }

    injectStyles();

    var targetSelector = script.getAttribute('data-hl-target');
    var target = null;
    if (targetSelector) {
      try {
        target = document.querySelector(targetSelector);
      } catch (e) {
        target = null;
      }
      if (!target) {
        console.warn('[haynes lab product-badge] data-hl-target "' + targetSelector + '" matched nothing. Rendering inline instead.');
      }
    }

    if (target) {
      // Overlay mode: pin one badge to a corner of the target element.
      if (window.getComputedStyle(target).position === 'static') {
        target.style.position = 'relative';
      }
      target.appendChild(makeTag(labels[0], bg, textColor, position));
      target.appendChild(makeCredit(true));
      return;
    }

    // Inline mode: render a row of badges right after the script tag.
    var root = el('span', PREFIX + ' ' + PREFIX + '--inline');
    labels.forEach(function (label) {
      root.appendChild(makeTag(label, bg, textColor, null));
    });
    root.appendChild(makeCredit(false));

    if (script.parentNode && script.parentNode.nodeName !== 'HEAD') {
      script.insertAdjacentElement('afterend', root);
    } else {
      document.body.appendChild(root);
    }
  }

  function init() {
    var script = findScript();
    if (!script) {
      console.warn('[haynes lab product-badge] Could not locate its own script tag. Nothing rendered.');
      return;
    }
    build(script);
  }

  if (document.body) {
    init();
  } else {
    document.addEventListener('DOMContentLoaded', init);
  }
})();

</script>

Configuration

Every option is an attribute on the script tag — the same attributes the customizer above exposes. All attributes are optional unless marked required — defaults apply when omitted.

AttributeDefaultDescription
data-hl-text"Sale"Badge label. Use a comma-separated list (e.g. "Sale, New, Bestseller") to render a row of badges inline; overlay mode uses the first label. Sale, New, and Bestseller get preset colors.
data-hl-bg"#f97316"Badge background color. Overrides the preset color for known labels like Sale or New.
data-hl-text-color"#ffffff"Badge text color.
data-hl-position"top-right"Corner used in overlay mode: top-left, top-right, bottom-left, or bottom-right.
data-hl-target(none)CSS selector of the element to overlay (e.g. ".product-card img"). When set, the badge is pinned to a corner of that element; when omitted, badges render inline after the script tag.

Frequently asked questions

How do I install the Product Badge?

Paste the script snippet where you want the badges to appear — it renders an inline row right after the tag. To overlay a product image or card instead, add data-hl-target with a CSS selector for that element.

Can I change the badge colors and position?

Yes — set data-hl-text for the label (comma-separated for a row), data-hl-bg and data-hl-text-color for colors, and data-hl-position to pick the corner (top-left, top-right, bottom-left, bottom-right) in overlay mode.

Does it work on WordPress, Shopify, or Squarespace?

Yes — it is a single vanilla JS file with no dependencies, so it works on WordPress, Shopify, Squarespace, or any site that can embed a script tag.

More free widgets

Need a custom widget?

We build custom widgets, integrations, and whole products. Tell us what your site needs and we'll scope it.

Get in Touch