Shopping cart with items – add and remove functionality: accessibility best practices

A shopping cart with add/remove functionality presents a dynamic interface where users can modify quantities or remove items. From an accessibility perspective, it’s crucial to ensure that all users, including those relying on assistive technologies, can easily interact with the cart and understand changes made to it.

Key considerations include:

  1. Keyboard Accessibility: All interactive elements, such as buttons and quantity controls, must be operable using a keyboard.
  2. Screen Reader Support: Assistive technologies should convey:
    • Item details (e.g., name, price, quantity).
    • Updates to the cart when items are added, removed, or quantities are adjusted.
  3. Accessible Buttons: Use clear labels for “Add” and “Remove” buttons, such as “Add one more item” or “Remove this item from cart.”
  4. Dynamic Updates: Notify users of real-time changes (e.g., when an item is removed) using ARIA live regions.
  5. Focus Management: Ensure that focus moves to relevant elements after changes (e.g., focus shifts to a confirmation message after removing an item).
  6. Error Prevention: Avoid accidental actions (e.g., removing an item by mistake) by providing confirmation prompts or undo functionality.

An accessible shopping cart ensures all users can modify their cart confidently and without confusion.

Good Practice: Uses semantic HTML for structure, clear ARIA attributes for dynamic content, and keyboard-operable controls for adding/removing items.Bad Practice: Missing or unclear labels, poor keyboard accessibility, lack of updates for screen reader users, and inadequate focus management.

Bad Practice: Missing or unclear labels, poor keyboard accessibility, lack of updates for screen reader users, and inadequate focus management.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Shopping Cart</title>
  <style>
    .cart {
      max-width: 600px;
      margin: 20px auto;
      font-family: Arial, sans-serif;
      color: #333;
    }
    .cart-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      padding: 10px;
      border-bottom: 1px solid #ccc;
    }
    .cart-item button {
      margin-left: 10px;
      padding: 5px 10px;
      font-size: 14px;
    }
    .cart-total {
      text-align: right;
      font-weight: bold;
    }
  </style>
</head>
<body>
  <main>
    <section class="cart">
      <h1>Your Shopping Cart</h1>
      <ul aria-live="polite">
        <li class="cart-item">
          <span>Item 1 - $20</span>
          <div>
            <label for="quantity-item-1">Quantity:</label>
            <input 
              type="number" 
              id="quantity-item-1" 
              name="quantity-item-1" 
              value="1" 
              aria-label="Quantity of Item 1">
            <button aria-label="Add one more of Item 1">Add</button>
            <button aria-label="Remove Item 1 from cart">Remove</button>
          </div>
        </li>
        <li class="cart-item">
          <span>Item 2 - $15</span>
          <div>
            <label for="quantity-item-2">Quantity:</label>
            <input 
              type="number" 
              id="quantity-item-2" 
              name="quantity-item-2" 
              value="2" 
              aria-label="Quantity of Item 2">
            <button aria-label="Add one more of Item 2">Add</button>
            <button aria-label="Remove Item 2 from cart">Remove</button>
          </div>
        </li>
      </ul>
      <p class="cart-total">Total: $50</p>
    </section>
  </main>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Shopping Cart</title>
  <style>
    .cart {
      max-width: 600px;
      margin: 20px auto;
      font-family: Arial, sans-serif;
      color: #333;
    }
    .cart-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      padding: 10px;
      border-bottom: 1px solid #ccc;
    }
  </style>
</head>
<body>
  <main>
    <section class="cart">
      <h1>Cart</h1>
      <ul>
        <li class="cart-item">
          <span>Item 1 - $20</span>
          <div>
            <input type="number" value="1">
            <button>+</button>
            <button>-</button>
          </div>
        </li>
        <li class="cart-item">
          <span>Item 2 - $15</span>
          <div>
            <input type="number" value="2">
            <button>+</button>
            <button>-</button>
          </div>
        </li>
      </ul>
      <p>Total: $50</p>
    </section>
  </main>
</body>
</html>
  1. Semantic HTML:
    • Proper structure with <main>, <section>, <ul>, and <li> elements for logical grouping and navigation.
  2. Accessible Labels:
    • Buttons have aria-label attributes to provide clear descriptions, such as “Add one more of Item 1” and “Remove Item 1 from cart.”
    • Inputs for quantity include aria-label to describe their purpose.
  3. Dynamic Content:
    • The cart updates dynamically with aria-live="polite", ensuring screen readers notify users of changes without being intrusive.
  4. Keyboard Accessibility:
    • Buttons and inputs are keyboard-operable, allowing users to adjust quantities or remove items without a mouse.
  5. Focus Management:
    • By default, focus remains logical. With additional scripting, focus can move to feedback messages when actions occur (e.g., removing an item).
  1. Non-Descriptive Labels:
    • Buttons only display symbols (+ and -), leaving screen reader users guessing their purpose.
  2. Missing ARIA Live Regions:
    • Screen reader users are not notified of dynamic cart updates, causing potential confusion.
  3. Lack of Context:
    • The <input> fields lack proper labels, making it unclear what they control.
  4. Generic Heading:
    • The <h1> simply says “Cart,” which is less descriptive compared to “Your Shopping Cart.”
  5. No Error Prevention:
    • Removing an item or adjusting quantities can be accidental, and no confirmation prompts or undo options are provided.

  1. Use descriptive labels for buttons and inputs to guide all users, including screen reader users.
  2. Implement dynamic updates using ARIA live regions (e.g., aria-live="polite") to notify users about cart changes.
  3. Ensure keyboard operability for all interactive elements.
  4. Provide clear feedback when actions occur, such as removing an item, with focus management for improved usability.
  5. Avoid generic labels or vague descriptions, and test with assistive technologies to ensure the cart is fully accessible.

What are your feelings

Updated on March 5, 2025