Skip to main content

How to create a simple shopping project using HTML, CSS and JavaScript?


 Below is the complete shopping project with all necessary features are required.

Let's follow the step by step process:

index. html page code:

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Online Shop</title>

  <link rel="stylesheet" href="style.css">

</head>

<body>

  <!-- Header -->

  <header class="header">

    <div class="logo">ShopLogo</div>

    <nav class="nav">

      <a href="#">Home</a>

      <a href="#products">Products</a>

      <a href="#cart">Cart</a>

      <a href="#contact">Contact</a>

    </nav>

  </header>


  <!-- Hero Section -->

  <section class="hero">

    <h1>Welcome to Our Shop</h1>

    <p>Find the best deals on your favorite products!</p>

    <button class="shop-now">Shop Now</button>

  </section>


  <!-- Filters -->

  <section class="filters">

    <div class="filter">

      <label for="category">Category:</label>

      <select id="category">

        <option value="all">All</option>

        <option value="electronics">Electronics</option>

        <option value="clothing">Clothing</option>

        <option value="accessories">Accessories</option>

      </select>

    </div>

    <div class="filter">

      <label for="sort">Sort By:</label>

      <select id="sort">

        <option value="default">Default</option>

        <option value="price-asc">Price: Low to High</option>

        <option value="price-desc">Price: High to Low</option>

      </select>

    </div>

    <div class="filter">

      <label for="search">Search:</label>

      <input type="text" id="search" placeholder="Search products">

    </div>

  </section>


  <!-- Product Section -->

  <section class="products" id="products">

    <div class="product-grid">

      <!-- Products dynamically generated -->

    </div>

  </section>


  <!-- Cart Section -->

  <section class="cart" id="cart">

    <h2>Your Cart</h2>

    <div class="cart-items">

      <p>No items in your cart.</p>

    </div>

    <div class="cart-summary">

      <p>Total Items: <span id="total-items">0</span></p>

      <p>Total Price: $<span id="total-price">0.00</span></p>

    </div>

  </section>


  <!-- Footer -->

  <footer class="footer">

    <p>© 2024 Online Shop. All rights reserved.</p>

    <p>Follow us: <a href="#">Facebook</a> | <a href="#">Twitter</a></p>

  </footer>


  <script src="main.js"></script>

</body>

</html>


style. css page code

*{
margin:0;
padding: 0;
box-sizing: border-box;
font-family: sans-serif;
}


body{
font-family: sans-serif;
line-height: 1.6;
color:#333;
}

.header{
display: flex;
justify-content: space-between;
align-items: center;
padding:10px 20px;
background-color:#333;
color: white;
}

.header .logo{
font-size: 1.5em;
font-weight: bold;
}

.header .nav{
display: flex;
font-weight: bold;

}

.header .nav a{
color: white;
text-decoration: none;
font-size: 1em;
padding:0 10px;
}

.header .nav a:hover{
text-decoration: underline;
}

.hero{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
padding: 50px 20px;
background-color: #f4f4f4;
}

.hero h1{
font-size: 2.5em;
margin-bottom: 10px;
}

.hero{
font-size: 1.2em;
margin-bottom: 20px;

}

.hero .shop-now{
padding:10px 20px;
background-color: #333;
color: white;
border: none;
font-size: 1em;
cursor: pointer;
}

.hero .shop-now:hover{
background-color: #555;
}

.products{
padding: 40px 20px;
}
.products h2{
text-align: center;
margin-bottom: 20px;
font-size: 2em;
}

.filters{
display: flex;
justify-content: space-between;
margin-bottom:20px;
}

.filters .filter{
flex: 1;
display: flex;
align-items: center;
gap:20px;
justify-content: center;


}

.product-grid{
display: flex;
flex-wrap: wrap;
gap:20px;
justify-content: center;
}

.product{
flex: 1 1 calc(25% - 20px);
background-color: #f9f9f9;
padding:15px;
border: 1px solid #dddd;
text-align: center;
transition:transform 0.3s ease box-shadow 0.3s ease;

}

.product:hover{
transform: scale(1.05);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
}

.product img{
max-width: 100%;
margin-bottom: 10px;


}

.product h3{
font-size:1.2em;
margin-bottom:5px;

}

.product p{
font-size: 1em;
margin-bottom: 10px;
}

.product button{
padding:10px 15px;
background-color:#333;
color: white;
border: none;
font-size: 1em;
cursor: pointer;
}

.product button:hover{
background-color: #555;

}

.cart{
padding: 20px;
background-color: #f9f9f9;
}

.cart h2{
text-align: center;
}

.cart-item{
margin-top: 20px;

}

.cart-summary{
margin-top:20px;
text-align: center;
}

.cart-item{
display: flex;
justify-content: space-between;
margin-bottom: 10px;
}

.cart-item button{
background-color: red;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
}

.footer{
width:100%;
height:200px;
background-color: black;
color: white;
text-align: center;
}


main. js page code


// Mock Product Data
const products = [
  { id: 1, name: "Smartphone", price: 299.99, category: "electronics", image: "https://via.placeholder.com/150" },
  { id: 2, name: "Headphones", price: 49.99, category: "electronics", image: "https://via.placeholder.com/150" },
  { id: 3, name: "T-Shirt", price: 19.99, category: "clothing", image: "https://via.placeholder.com/150" },
  { id: 4, name: "Sunglasses", price: 99.99, category: "accessories", image: "https://via.placeholder.com/150" },
  { id: 5, name: "Jeans", price: 39.99, category: "clothing", image: "https://via.placeholder.com/150" },
  { id: 6, name: "Smartwatch", price: 199.99, category: "electronics", image: "https://via.placeholder.com/150" },
];

let cart = [];

// Function to render products dynamically
function renderProducts(category = "all", sort = "default", search = "") {
  const productGrid = document.querySelector(".product-grid");
  productGrid.innerHTML = "";

  let filteredProducts = products;

  if (category !== "all") {
    filteredProducts = filteredProducts.filter(p => p.category === category);
  }

  if (search) {
    filteredProducts = filteredProducts.filter(p =>
      p.name.toLowerCase().includes(search.toLowerCase())
    );
  }

  if (sort === "price-asc") {
    filteredProducts.sort((a, b) => a.price - b.price);
  } else if (sort === "price-desc") {
    filteredProducts.sort((a, b) => b.price - a.price);
  }

  if (filteredProducts.length === 0) {
    productGrid.innerHTML = "<p>No products found.</p>";
    return;
  }

  filteredProducts.forEach(product => {
    const productCard = document.createElement("div");
    productCard.classList.add("product");
    productCard.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>
    `;
    productGrid.appendChild(productCard);
  });
}

// Function to handle adding a product to the cart
function addToCart(productId) {
  const product = products.find(p => p.id === productId);
  const cartItem = cart.find(item => item.id === productId);

  if (cartItem) {
    cartItem.quantity += 1;
  } else {
    cart.push({ ...product, quantity: 1 });
  }

  updateCart();
}

// Function to handle removing a product from the cart
function removeFromCart(productId) {
  cart = cart.filter(item => item.id !== productId);
  updateCart();
}

// Function to update the cart display
function updateCart() {
  const cartItems = document.querySelector(".cart-items");
  const totalItems = document.getElementById("total-items");
  const totalPrice = document.getElementById("total-price");

  cartItems.innerHTML = '';

  if (cart.length === 0) {
    cartItems.innerHTML = "<p>No items in your cart.</p>";
  } else {
    cart.forEach(item => {
      const cartItem = document.createElement("div");
      cartItem.classList.add("cart-item");
      cartItem.innerHTML = `
        <p>${item.name} (x${item.quantity}) - $${(item.price * item.quantity).toFixed(2)}</p>
        <button onclick="removeFromCart(${item.id})">Remove</button>
      `;
      cartItems.appendChild(cartItem);
    });
  }

  totalItems.textContent = cart.reduce((total, item) => total + item.quantity, 0);
  totalPrice.textContent = cart.reduce((total, item) => total + item.price * item.quantity, 0).toFixed(2);
}

// Event listeners for filters and search
document.getElementById("category").addEventListener("change", event => {
  const category = event.target.value;
  const sort = document.getElementById("sort").value;
  const search = document.getElementById("search").value;
  renderProducts(category, sort, search);
});

document.getElementById("sort").addEventListener("change", event => {
  const sort = event.target.value;
  const category = document.getElementById("category").value;
  const search = document.getElementById("search").value;
  renderProducts(category, sort, search);
});

document.getElementById("search").addEventListener("input", event => {
  const search = event.target.value;
  const category = document.getElementById("category").value;
  const sort = document.getElementById("sort").value;
  renderProducts(category, sort, search);
});

// Initial render of products
renderProducts();





Download source code link 🔗

Download





Comments

Popular posts from this blog

Creating a Student Marksheet using Microsoft Excel.

 Creating a Student Marksheet using Microsoft Excel.  Creating a student marks sheet in MS Excel is straightforward. Here's a step-by-step guide to design one: Step 1: Open MS Excel Launch MS Excel and open a new workbook. Step 2: Structure the Sheet Header Section: Use the first few rows to include the title, such as "Student Marks Sheet" and relevant details like the class, semester, or term. Column Headings: In Row 5 (or below the title), define your column headers: Column A: Roll Number Column B: Student Name Column C onward: Subjects (e.g., Mathematics, Science, English, etc.) Final Columns: Include Total Marks , Percentage , and Grade . Example: Roll No.Student Name Mathematics Science English Total Marks Percentage Grade Step 3: Input Data Enter the roll numbers, student names, and their marks under respective columns. Step 4: Add Formulas Total Marks: In the "Total Marks" column, use the formula to sum marks: =SUM(C2:E2) Copy this ...

Backup one folder to another location

 You can create a simple batch file (.bat) to back up a folder automatically in Windows. Here’s a step-by-step example: 🔹 Example: Backup one folder to another location @echo off set source="C:\MyData" set destination="D:\Backup\MyData_%date:~-4%-%date:~7,2%-%date:~4,2%" echo Backing up from %source% to %destination% xcopy %source% %destination% /E /H /C /I /Y echo Backup completed! pause 🔹 Explanation: set source= → Source folder you want to back up. set destination= → Destination folder where backup will be stored. %date:~-4%-%date:~7,2%-%date:~4,2% adds today’s date in YYYY-MM-DD format to the folder name (so each backup is stored separately). xcopy options: /E → Copy all subdirectories (including empty ones). /H → Copy hidden + system files. /C → Continue even if errors occur. /I → Assume destination is a folder. /Y → Suppress overwrite prompts. 🔹 Usage: Open Notepad . Paste the code above. Save it...

How to create a simple Shopping App with PHP AJAX MYSQL?

  Below is the complete code for the PHP, MySQL, and AJAX-based shopping cart application with features like adding products to the cart, updating cart quantities, and checkout with Cash on Delivery (COD). This includes all the necessary files and steps to set up the application. --- 1. Database Setup Run the following SQL queries to create the database and tables: ```sql CREATE DATABASE shopping_cart; USE shopping_cart; -- Products table CREATE TABLE products (     id INT AUTO_INCREMENT PRIMARY KEY,     name VARCHAR(255) NOT NULL,     price DECIMAL(10, 2) NOT NULL,     description TEXT,     image VARCHAR(255) ); -- Orders table CREATE TABLE orders (     id INT AUTO_INCREMENT PRIMARY KEY,     customer_name VARCHAR(255) NOT NULL,     customer_email VARCHAR(255) NOT NULL,     customer_address TEXT NOT NULL,     total_amount DECIMAL(10, 2) NOT NULL,     status ENUM('pending', ...