Toggle navigation
☰
Home
HTML
CSS
Scripting
Database
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Add to Cart Button</title> <style> /* Component: Interactive Add to Cart Button Description: An e-commerce button with an icon and a JavaScript-powered success state. */ :root { --cart-add-bg: #007bff; --cart-add-text: #ffffff; --cart-add-bg-hover: #0069d9; --cart-added-bg: #28a745; --cart-added-text: #ffffff; --cart-focus-outline: #007bff; } .add-to-cart-container-1 { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; display: flex; justify-content: center; align-items: center; padding: 2rem; background-color: #f8f9fa; /* Example background */ } .add-to-cart-btn-1 { display: inline-flex; align-items: center; justify-content: center; gap: 0.75rem; padding: 0.8rem 1.6rem; font-size: 1rem; font-weight: 600; color: var(--cart-add-text); background-color: var(--cart-add-bg); border: none; border-radius: 8px; cursor: pointer; transition: background-color 0.2s ease-in-out; min-width: 150px; /* Prevents button from shrinking/growing during text change */ } .add-to-cart-btn-1 svg { width: 1.2em; height: 1.2em; fill: currentColor; transition: transform 0.3s ease; } .add-to-cart-btn-1:hover { background-color: var(--cart-add-bg-hover); } .add-to-cart-btn-1:focus-visible { outline: 3px solid var(--cart-focus-outline); outline-offset: 2px; } .add-to-cart-btn-1 .icon-check { display: none; } /* --- State when item is added --- */ .add-to-cart-btn-1.is-added { background-color: var(--cart-added-bg); color: var(--cart-added-text); cursor: not-allowed; } .add-to-cart-btn-1.is-added .icon-cart { display: none; } .add-to-cart-btn-1.is-added .icon-check { display: inline-block; } </style> </head> <body> <div class="add-to-cart-container-1"> <button type="button" class="add-to-cart-btn-1" id="addToCartBtn1"> <!-- Cart Icon --> <svg class="icon-cart" aria-hidden="true" height="24" width="24" viewBox="0 0 24 24"><path d="M11 9h2V6h3V4h-3V1h-2v3H8v2h3v3zm-4 9c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2zm10 0c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2zm-9.83-3.25l.03-.12.9-1.63h7.45c.75 0 1.41-.41 1.75-1.03l3.86-7.01L19.42 4h-.01l-1.1 2-2.76 5H8.53L4.27 2H1v2h2l3.6 7.59-1.35 2.44C7.54 15.36 7 16.1 7 17h12v-2H8.17c-.09 0-.17-.04-.25-.12z" fill="currentColor"/></svg> <!-- Checkmark Icon (hidden by default) --> <svg class="icon-check" aria-hidden="true" height="24" width="24" viewBox="0 0 24 24"><path d="M9 16.17L4.83 12l-1.42 1.41L9 19 21 7l-1.41-1.41z" fill="currentColor"/></svg> <span class="btn-text">Add to Cart</span> </button> </div> <script> document.addEventListener('DOMContentLoaded', function() { const addToCartButton = document.getElementById('addToCartBtn1'); const buttonText = addToCartButton.querySelector('.btn-text'); const originalText = buttonText.textContent; addToCartButton.addEventListener('click', function() { // Prevent interaction if already in added state if (this.classList.contains('is-added')) { return; } this.classList.add('is-added'); this.disabled = true; buttonText.textContent = 'Added'; // Revert button to original state after 2 seconds setTimeout(() => { this.classList.remove('is-added'); this.disabled = false; buttonText.textContent = originalText; }, 2000); }); }); </script> </body> </html>