<div class="product-card">
<div class="image-box">
<img id="productImg" src="https://placehold.co/400x400/3b82f6/ffffff?text=Blue+Shirt" alt="Product">
</div>
<div class="details">
<h3>Classic T-Shirt</h3>
<p>$29.99</p>
<div class="colors">
<span class="swatch active" data-color="#3b82f6" data-img="https://placehold.co/400x400/3b82f6/ffffff?text=Blue+Shirt"></span>
<span class="swatch" data-color="#ef4444" data-img="https://placehold.co/400x400/ef4444/ffffff?text=Red+Shirt"></span>
<span class="swatch" data-color="#10b981" data-img="https://placehold.co/400x400/10b981/ffffff?text=Green+Shirt"></span>
<span class="swatch" data-color="#f59e0b" data-img="https://placehold.co/400x400/f59e0b/ffffff?text=Yellow+Shirt"></span>
</div>
<button>Add to Cart</button>
</div>
</div>
.product-card {
font-family: system-ui, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
width: 300px;
background: #fff;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 10px 30px rgba(0,0,0,0.05);
margin: 0 auto;
border: 1px solid #f3f4f6;
}
.image-box {
height: 250px;
overflow: hidden;
}
.image-box img {
width: 100%;
height: 100%;
object-fit: cover;
transition: transform 0.3s;
}
.details {
padding: 20px;
text-align: center;
}
.details h3 {
margin: 0 0 5px 0;
color: #1f2937;
}
.details p {
color: #6b7280;
font-size: 1.1rem;
font-weight: 600;
margin-bottom: 15px;
}
.colors {
display: flex;
justify-content: center;
gap: 10px;
margin-bottom: 20px;
}
.swatch {
width: 24px;
height: 24px;
border-radius: 50%;
cursor: pointer;
border: 2px solid #fff;
box-shadow: 0 0 0 1px #d1d5db;
transition: transform 0.2s;
}
.swatch.active {
transform: scale(1.2);
box-shadow: 0 0 0 2px #3b82f6;
}
button {
width: 100%;
padding: 12px;
background: #111827;
color: white;
border: none;
border-radius: 8px;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
button:hover {
background: #000;
}
const swatches = document.querySelectorAll(".swatch");
const productImg = document.getElementById("productImg");
swatches.forEach(swatch => {
// Set background color from data attribute
swatch.style.backgroundColor = swatch.getAttribute("data-color");
swatch.addEventListener("click", function() {
// Remove active class from all
swatches.forEach(s => s.classList.remove("active"));
// Add active to clicked
this.classList.add("active");
// Update Image
const newSrc = this.getAttribute("data-img");
productImg.src = newSrc;
});
});