Certainly! Here's the full complete HTML, CSS, and JavaScript code for your responsive shopping cart with:
- Remove buttons on each item,
- Grouping of items by store name,
- Working quantity controls,
- Cart summary updating dynamically.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Responsive Shopping Cart with Stores</title>
<style>
/* Reset some default */
* {
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background: #f5f5f5;
}
.container {
max-width: 960px;
margin: 0 auto;
background: #fff;
padding: 20px;
border-radius: 6px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
h2 {
margin-top: 0;
margin-bottom: 20px;
font-weight: 600;
color: #333;
}
/* Store group styles */
.store-group {
margin-bottom: 30px;
border: 1px solid #ddd;
border-radius: 6px;
padding: 15px;
background: #fafafa;
}
.store-name {
font-size: 1.2rem;
font-weight: 700;
margin-bottom: 15px;
color: #222;
border-bottom: 2px solid #ccc;
padding-bottom: 5px;
}
/* Cart Items List */
.cart-items {
display: flex;
flex-direction: column;
gap: 20px;
}
.cart-item {
display: flex;
flex-wrap: wrap;
align-items: center;
border-bottom: 1px solid #ddd;
padding-bottom: 15px;
position: relative;
}
/* Remove button */
.remove-btn {
position: absolute;
top: 10px;
right: 10px;
border: none;
background: transparent;
font-size: 20px;
line-height: 1;
cursor: pointer;
color: #a00;
font-weight: 700;
user-select: none;
padding: 0;
transition: color 0.2s ease;
}
.remove-btn:hover {
color: #e00;
}
.cart-item img {
width: 80px;
height: 80px;
object-fit: cover;
border-radius: 4px;
flex-shrink: 0;
}
.item-details {
flex: 1 1 200px;
padding: 0 15px;
}
.item-title {
font-weight: 600;
margin: 0 0 8px;
}
.store-label {
font-size: 0.85rem;
color: #888;
margin-bottom: 4px;
}
.unit-price,
.total-price {
font-size: 0.9rem;
color: #555;
}
/* Quantity control */
.quantity-control {
display: flex;
align-items: center;
border: 1px solid #ccc;
border-radius: 4px;
overflow: hidden;
width: 120px;
margin-top: 8px;
}
.quantity-control button {
background: #eee;
border: none;
width: 30px;
height: 30px;
font-size: 18px;
cursor: pointer;
user-select: none;
transition: background-color 0.2s ease;
}
.quantity-control button:hover {
background: #ddd;
}
.quantity-control input {
border: none;
width: 60px;
text-align: center;
font-size: 1rem;
outline: none;
}
/* Total price */
.total-price {
flex: 0 0 100px;
font-weight: 700;
color: #000;
text-align: right;
min-width: 80px;
}
/* Cart Summary */
.cart-summary {
border-top: 2px solid #333;
padding-top: 20px;
max-width: 350px;
margin-left: auto;
}
.summary-row {
display: flex;
justify-content: space-between;
margin-bottom: 12px;
font-size: 1rem;
color: #333;
}
.summary-row.total {
font-weight: 700;
font-size: 1.2rem;
border-top: 2px solid #333;
padding-top: 12px;
margin-top: 12px;
}
/* Checkout button */
.checkout-button {
display: block;
width: 100%;
background-color: #28a745;
color: white;
font-weight: 700;
font-size: 1.1rem;
padding: 15px 0;
text-align: center;
border: none;
border-radius: 6px;
cursor: pointer;
margin-top: 25px;
transition: background-color 0.3s ease;
text-decoration: none; /* if you use <a> */
}
.checkout-button:hover {
background-color: #218838;
}
/* Responsive */
@media (max-width: 600px) {
.cart-item {
flex-direction: column;
align-items: flex-start;
padding-bottom: 10px; /* reduce space under each item */
margin-bottom: 10px; /* ensure items don't have big gaps */
}
.item-details {
padding: 0 5px 10px 5px; /* reduce horizontal padding */
width: 100%;
flex: 1 1;
}
/* Group quantity and total price row */
.quantity-and-total {
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.quantity-control {
margin: 0; /* remove top margin */
width: auto;
flex-shrink: 0;
}
.total-price {
text-align: right;
flex: 0 0 auto;
min-width: 70px;
font-weight: 700;
color: #000;
}
}
</style>
</head>
<body>
<div class="container">
<h2>Your Shopping Cart</h2>
<div id="cart-container">
<!-- Store groups and items will be injected here -->
</div>
<div class="cart-summary">
<div class="summary-row">
<span>Cart Total:</span>
<span id="cart-total">$0.00</span>
</div>
<div class="summary-row">
<span>Shipping:</span>
<span id="shipping-cost">$5.00</span>
</div>
<div class="summary-row total">
<span>Total Amount:</span>
<span id="total-amount">$0.00</span>
</div>
<button class="checkout-button">Checkout</button>
</div>
</div>
<script>
// Sample cart data including store name
const cartData = [
{
store: "Clothing Store",
items: [
{
id: 1,
img: "https://via.placeholder.com/80",
title: "Awesome T-Shirt",
price: 29.99,
quantity: 1,
},
{
id: 2,
img: "https://via.placeholder.com/80",
title: "Stylish Hat",
price: 15.5,
quantity: 2,
},
],
},
{
store: "Gadget Shop",
items: [
{
id: 3,
img: "https://via.placeholder.com/80",
title: "Wireless Mouse",
price: 19.99,
quantity: 1,
},
{
id: 4,
img: "https://via.placeholder.com/80",
title: "USB C Charger",
price: 25.0,
quantity: 1,
},
],
},
];
const cartContainer = document.getElementById('cart-container');
// Create store group container
function createStoreGroup(storeName) {
const storeGroup = document.createElement('div');
storeGroup.className = 'store-group';
storeGroup.dataset.store = storeName;
const storeHeader = document.createElement('div');
storeHeader.className = 'store-name';
storeHeader.textContent = storeName;
const cartItems = document.createElement('div');
cartItems.className = 'cart-items';
storeGroup.appendChild(storeHeader);
storeGroup.appendChild(cartItems);
return { storeGroup, cartItems };
}
// Create an individual cart item element
function createCartItem(item, storeName) {
const div = document.createElement('div');
div.className = 'cart-item';
div.dataset.price = item.price;
div.dataset.store = storeName;
div.dataset.id = item.id;
div.innerHTML = `
<button class="remove-btn" title="Remove item">×</button>
<img src="${item.img}" alt="${item.title}" />
<div class="item-details">
<p class="item-title">${item.title}</p>
<p class="unit-price">Unit Price: $${item.price.toFixed(2)}</p>
</div>
<div class="quantity-and-total">
<div class="quantity-control">
<button class="qty-minus">−</button>
<input type="number" min="1" value="${item.quantity}" class="qty-input" />
<button class="qty-plus">+</button>
</div>
<div class="total-price">$${(item.price * item.quantity).toFixed(2)}</div>
</div>
`;
return div;
}
// Render cart grouped by stores
function renderCart() {
cartContainer.innerHTML = ''; // clear container
cartData.forEach(storeGroup => {
const { storeGroup: storeEl, cartItems } = createStoreGroup(storeGroup.store);
storeGroup.items.forEach(item => {
const cartItem = createCartItem(item, storeGroup.store);
cartItems.appendChild(cartItem);
});
cartContainer.appendChild(storeEl);
});
attachEventListeners();
updateCartSummary();
}
// Attach event listeners for qty controls and remove buttons
function attachEventListeners() {
document.querySelectorAll('.qty-minus').forEach(button => {
button.onclick = () => {
const input = button.nextElementSibling;
let val = parseInt(input.value) || 1;
if (val > 1) {
input.value = val - 1;
updateItemTotal(input.closest('.cart-item'));
}
};
});
document.querySelectorAll('.qty-plus').forEach(button => {
button.onclick = () => {
const input = button.previousElementSibling;
let val = parseInt(input.value) || 1;
input.value = val + 1;
updateItemTotal(input.closest('.cart-item'));
};
});
document.querySelectorAll('.qty-input').forEach(input => {
input.oninput = () => {
let val = parseInt(input.value);
if (isNaN(val) || val < 1) val = 1;
input.value = val;
updateItemTotal(input.closest('.cart-item'));
};
});
document.querySelectorAll('.remove-btn').forEach(button => {
button.onclick = () => {
const cartItem = button.closest('.cart-item');
const storeGroup = cartItem.closest('.store-group');
// Remove from cartData
removeItemFromData(cartItem.dataset.store, cartItem.dataset.id);
// Remove from DOM
cartItem.remove();
// If no items left in store, remove store group
if (storeGroup.querySelectorAll('.cart-item').length === 0) {
storeGroup.remove();
}
updateCartSummary();
};
});
}
// Update item total based on quantity input
function updateItemTotal(cartItem) {
const price = parseFloat(cartItem.dataset.price);
const qtyInput = cartItem.querySelector('.qty-input');
let qty = parseInt(qtyInput.value);
if (isNaN(qty) || qty < 1) qty = 1;
qtyInput.value = qty;
const totalPriceEl = cartItem.querySelector('.total-price');
totalPriceEl.textContent = `$${(price * qty).toFixed(2)}`;
// Update data model
updateItemQuantity(cartItem.dataset.store, cartItem.dataset.id, qty);
updateCartSummary();
}
// Update quantity in data model
function updateItemQuantity(storeName, itemId, newQty) {
const store = cartData.find(s => s.store === storeName);
if (!store) return;
const item = store.items.find(i => i.id.toString() === itemId);
if (item) {
item.quantity = newQty;
}
}
// Remove item from data model by store and id
function removeItemFromData(storeName, itemId) {
const store = cartData.find(s => s.store === storeName);
if (!store) return;
store.items = store.items.filter(i => i.id.toString() !== itemId);
// If no items left, remove store
if (store.items.length === 0) {
const idx = cartData.findIndex(s => s.store === storeName);
if (idx !== -1) cartData.splice(idx, 1);
}
}
// Update cart summary totals
function updateCartSummary() {
const shippingCost = 5.00; // fixed
let cartTotal = 0;
cartData.forEach(storeGroup => {
storeGroup.items.forEach(item => {
cartTotal += item.price * item.quantity;
});
});
document.getElementById('cart-total').textContent = `$${cartTotal.toFixed(2)}`;
document.getElementById('shipping-cost').textContent = `$${shippingCost.toFixed(2)}`;
document.getElementById('total-amount').textContent = `$${(cartTotal + shippingCost).toFixed(2)}`;
}
// Initialize cart on page load
renderCart();
</script>
</body>
</html>
How to use:
- Load this HTML file in any modern browser.
- You see two store groups, each with their items.
- You can increment/decrement quantity, input quantity directly.
- The total updates accordingly.
- Clicking "×" removes the item; if that was the last item in the store, the store group disappears.
- The summary on the right updates automatically.
Let me know if you'd like me to help further customize or connect this to backend logic!