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:
- Keyboard Accessibility: All interactive elements, such as buttons and quantity controls, must be operable using a keyboard.
- 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.
- Accessible Buttons: Use clear labels for “Add” and “Remove” buttons, such as “Add one more item” or “Remove this item from cart.”
- Dynamic Updates: Notify users of real-time changes (e.g., when an item is removed) using ARIA live regions.
- Focus Management: Ensure that focus moves to relevant elements after changes (e.g., focus shifts to a confirmation message after removing an item).
- 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.
Sample #
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.
Sample – Good Practice #
<!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>
Sample – Bad Practice #
<!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>
Explanation #
Good Sample: #
- Semantic HTML:
- Proper structure with
<main>,<section>,<ul>, and<li>elements for logical grouping and navigation.
- Proper structure with
- Accessible Labels:
- Buttons have
aria-labelattributes to provide clear descriptions, such as “Add one more of Item 1” and “Remove Item 1 from cart.” - Inputs for quantity include
aria-labelto describe their purpose.
- Buttons have
- Dynamic Content:
- The cart updates dynamically with
aria-live="polite", ensuring screen readers notify users of changes without being intrusive.
- The cart updates dynamically with
- Keyboard Accessibility:
- Buttons and inputs are keyboard-operable, allowing users to adjust quantities or remove items without a mouse.
- Focus Management:
- By default, focus remains logical. With additional scripting, focus can move to feedback messages when actions occur (e.g., removing an item).
Bad Sample: #
- Non-Descriptive Labels:
- Buttons only display symbols (
+and-), leaving screen reader users guessing their purpose.
- Buttons only display symbols (
- Missing ARIA Live Regions:
- Screen reader users are not notified of dynamic cart updates, causing potential confusion.
- Lack of Context:
- The
<input>fields lack proper labels, making it unclear what they control.
- The
- Generic Heading:
- The
<h1>simply says “Cart,” which is less descriptive compared to “Your Shopping Cart.”
- The
- No Error Prevention:
- Removing an item or adjusting quantities can be accidental, and no confirmation prompts or undo options are provided.
Key Takeaways #
- Use descriptive labels for buttons and inputs to guide all users, including screen reader users.
- Implement dynamic updates using ARIA live regions (e.g.,
aria-live="polite") to notify users about cart changes. - Ensure keyboard operability for all interactive elements.
- Provide clear feedback when actions occur, such as removing an item, with focus management for improved usability.
- Avoid generic labels or vague descriptions, and test with assistive technologies to ensure the cart is fully accessible.