<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Simple Product Cards</title> <style> /* Styling for the container */ .container { max-width: 800px; margin: 20px auto; padding: 10px; text-align: center; } /* Grid layout */ .product-grid { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; } /* Card styling */ .card { border: 1px solid #ccc; border-radius: 8px; width: 200px; padding: 10px; text-align: center; background: #fff; } .card img { max-width: 100%; border-radius: 8px; } .card-title { font-size: 1.2rem; margin: 10px 0; } .card-price { color: green; font-weight: bold; } </style> </head> <body> <div class="container"> <h1>Product Cards</h1> <div class="product-grid" id="product-container"></div> </div> <script> // Array of products const products = [ { name: "Laptop", price: "$999", image: "https://via.placeholder.com/150" }, { name: "Phone", price: "$699", image: "https://via.placeholder.com/150" }, { name: "Headphones", price: "$199", image: "https://via.placeholder.com/150" } ]; // Display products const container = document.getElementById('product-container'); products.forEach(product => { // Create card const card = document.createElement('div'); card.className = 'card'; // Add content to card card.innerHTML = ` <img src="${product.image}" alt="${product.name}"> <h2 class="card-title">${product.name}</h2> <p class="card-price">${product.price}</p> `; // Append card to container container.appendChild(card); }); </script> </body> </html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter