Skip to content

Cart promo code in HTML and CSS

A promo code field that checks the code, explains a refusal (unknown, minimum spend) and recalculates discount, shipping and total.

Here is an excerpt of this component's code in HTML and CSS. The full code is free for members, ready to copy into your project.

This component also comes in

Open in a new tab

The code in HTML and CSS

index.html
<!doctype html>
<html lang="fr">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Code promo dans le panier</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <main class="demo">
      <section class="cpr" data-cpr data-cpr-codes='{"BIENVENUE10":{"type":"percent","value":10,"label":"−10 % sur les articles"},"PORTOFFERT":{"type":"shipping","value":0,"label":"Livraison offerte"},"NOEL15":{"type":"amount","value":15,"min":80,"label":"−15 € dès 80 € d’achat"}}' data-cpr-sub="64" data-cpr-ship="4.9" aria-labelledby="cpr-titre">
        <h2 class="cpr-title" id="cpr-titre">Récapitulatif</h2>
script.js
document.querySelectorAll("[data-cpr]").forEach((root) => {
  const on = (el, type, fn) => el.addEventListener(type, fn);
  const codes = JSON.parse(root.getAttribute("data-cpr-codes"));
  const sub = Number(root.getAttribute("data-cpr-sub"));
  const shipping = Number(root.getAttribute("data-cpr-ship"));
  const euro = new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" });
  const form = root.querySelector("[data-cpr-form]");
  const input = form.querySelector("input");
  const msg = root.querySelector("[data-cpr-msg]");
  const chip = root.querySelector("[data-cpr-chip]");
  const out = (name) => root.querySelector(`[data-cpr-${name}]`);
  let applied = "";
style.css
:root {
  --cpr-card: #ffffff;
  --cpr-text: #13261f;
  --cpr-muted: #475a52;
  --cpr-line: #d3e0da;
  --cpr-field: #ffffff;
  --cpr-accent: #0f5c46;
  --cpr-accent-text: #ffffff;
  --cpr-chip: #e3f3ec;
  --cpr-good: #0f6b4f;
  --cpr-error: #b91c1c;
  --demo-bg: #e8f0ec;

Free component: copy it freely, including for commercial projects.

E-commerce components in HTML and CSS
  • A limited-time offer bar: countdown to a fixed date, one-click copyable promo code and a fallback message once the offer is over.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A sales card showing the discount that grows with the cart amount (-10% from €50, -15% from €100…) with a progress bar towards the next tier.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A Christmas gift guide filtered by budget and theme, with the number of ideas announced and an empty state offering to show everything again.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • The last order dates for delivery before Christmas, per shipping method, with the days left worked out from the visitor’s date.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A cart that shows live how much is left to add for free shipping, with a progress bar and recalculated totals.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component
  • A colour and size picker for a product page: the visual, price and stock follow the choice, and sold-out sizes are disabled.

    • Tailwind
    • React
    • CSS
    • WordPress
    Component