gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
Make a ecommerce site
- User
Make a ecommerce site
- Assistant
Absolutely! I can help you create a basic e-commerce website using modern web technologies. Below is a simple example using HTML, CSS, and JavaScript for the frontend. For a real-world application, you’d want to use a backend (like Node.js, Django, or PHP) and a database, but this will give you a great starting point.
1. Project Structure
ecommerce-site/ │ ├── index.html ├── style.css └── script.js
2. index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Simple E-Commerce Site</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>My Shop</h1> <div id="cart"> 🛒 Cart (<span id="cart-count">0</span>) </div> </header> <main> <section id="products"></section> <aside id="cart-modal" class="hidden"> <h2>Your Cart</h2> <ul id="cart-items"></ul> <p>Total: $<span id="cart-total">0.00</span></p> <button id="close-cart">Close</button> </aside> </main> <script src="script.js"></script> </body> </html>
3. style.css
body { font-family: Arial, sans-serif; margin: 0; background: #f4f4f4; } header { background: #333; color: #fff; padding: 1em; display: flex; justify-content: space-between; align-items: center; } #products { display: flex; flex-wrap: wrap; gap: 2em; padding: 2em; justify-content: center; } .product { background: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 1em; width: 200px; text-align: center; } .product img { width: 100%; height: 150px; object-fit: cover; } button { background: #007bff; color: #fff; border: none; padding: 0.5em 1em; border-radius: 4px; cursor: pointer; } button:hover { background: #0056b3; } #cart-modal { position: fixed; top: 10%; right: 10%; background: #fff; border-radius: 8px; box-shadow: 0 2px 16px rgba(0,0,0,0.2); padding: 2em; z-index: 1000; width: 300px; } .hidden { display: none; }
4. script.js
const products = [ { id: 1, name: "T-Shirt", price: 19.99, image: "https://via.placeholder.com/200x150?text=T-Shirt" }, { id: 2, name: "Sneakers", price: 49.99, image: "https://via.placeholder.com/200x150?text=Sneakers" }, { id: 3, name: "Backpack", price: 29.99, image: "https://via.placeholder.com/200x150?text=Backpack" } ]; let cart = []; function renderProducts() { const productsDiv = document.getElementById('products'); productsDiv.innerHTML = ''; products.forEach(product => { const div = document.createElement('div'); div.className = 'product'; div.innerHTML = ` <img src="${product.image}" alt="${product.name}"> <h3>${product.name}</h3> <p>$${product.price.toFixed(2)}</p> <button onclick="addToCart(${product.id})">Add to Cart</button> `; productsDiv.appendChild(div); }); } function addToCart(id) { const product = products.find(p => p.id === id); const item = cart.find(i => i.id === id); if (item) { item.qty += 1; } else { cart.push({ ...product, qty: 1 }); } updateCartCount(); } function updateCartCount() { document.getElementById('cart-count').textContent = cart.reduce((sum, item) => sum + item.qty, 0); } function showCart() { document.getElementById('cart-modal').classList.remove('hidden'); const cartItems = document.getElementById('cart-items'); cartItems.innerHTML = ''; let total = 0; cart.forEach(item => { total += item.price * item.qty; const li = document.createElement('li'); li.textContent = `${item.name} x${item.qty} - $${(item.price * item.qty).toFixed(2)}`; cartItems.appendChild(li); }); document.getElementById('cart-total').textContent = total.toFixed(2); } document.getElementById('cart').addEventListener('click', showCart); document.getElementById('close-cart').addEventListener('click', () => { document.getElementById('cart-modal').classList.add('hidden'); }); renderProducts(); updateCartCount();
5. How to Run
- Create the files as shown above.
- Open
index.htmlin your browser. - You’ll see a simple e-commerce site with products and a working cart!
Next Steps
- Add a backend (Node.js, Django, etc.) for real product data and checkout.
- Integrate payment gateways (Stripe, PayPal).
- Add user authentication.
- Deploy to a platform (Vercel, Netlify, etc.).
Let me know if you want a more advanced version (React, backend, etc.) or help with deployment!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.