Introduction

Creating a food ordering website is a practical way to apply web development skills. This tutorial will guide you through the process of building a simple and functional food ordering website using HTML, CSS, and JavaScript. By the end of this tutorial, you’ll have a website where users can browse food items, add them to a cart, and view their order summary.

Project Overview

Features:

  1. A menu displaying different food items.
  2. An “Add to Cart” button for each item.
  3. A shopping cart that updates dynamically as items are added.
  4. An order summary to display the total items and price.

Tools and Technologies Used

  • HTML: To create the structure of the web pages.
  • CSS: To style the website and make it visually appealing.
  • JavaScript: To handle the functionality of adding items to the cart and displaying the order summary.

Step 1: Setting Up the Project

Create a directory for your project and create the following files inside it:

  • index.html – The HTML file for the structure of the website.
  • styles.css – The CSS file for styling the website.
  • script.js – The JavaScript file for functionality.

Step 2: Building the HTML Structure

Open the index.html file and set up the basic structure for our food ordering website.

				
					<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Food Ordering Website</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <header>
        <h1>Food Ordering Website</h1>
        <nav>
            <ul>
                <li><a href="#menu">Menu</a></li>
                <li><a href="#cart">Cart</a></li>
            </ul>
        </nav>
    </header>

    <section id="menu" class="section">
        <h2>Menu</h2>
        <div class="menu-container">
            <div class="menu-item">
                <h3>Pizza</h3>
                <p>$10.00</p>
                <button class="add-to-cart">Add to Cart</button>
            </div>
            <div class="menu-item">
                <h3>Burger</h3>
                <p>$5.00</p>
                <button class="add-to-cart">Add to Cart</button>
            </div>
            <div class="menu-item">
                <h3>Pasta</h3>
                <p>$7.00</p>
                <button class="add-to-cart">Add to Cart</button>
            </div>
            <!-- Add more menu items as needed -->
        </div>
    </section>

    <section id="cart" class="section">
        <h2>Shopping Cart</h2>
        <div id="cart-items">
            <!-- Cart items will be added here dynamically -->
        </div>
        <div id="total-price">
            Total Price: $0.00
        </div>
        <button id="checkout-btn">Checkout</button>
    </section>

    <footer>
        <p>&copy; 2024 Food Ordering Website. All rights reserved.</p>
    </footer>

    <script src="script.js"></script>
</body>
</html>
				
			

Step 3: Styling with CSS

Next, let’s add some CSS to style our food ordering website. Open the styles.css file and add the following styles:

				
					/* styles.css */

body, h1, h2, p, ul, li, button {
    margin: 0;
    padding: 0;
    list-style: none;
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
    background-color: #f9f9f9;
    color: #333;
    display: flex;
    flex-direction: column;
    align-items: center;
}

header {
    background-color: #ff6347;
    color: #fff;
    width: 100%;
    text-align: center;
    padding: 15px 0;
}

nav ul {
    display: flex;
    justify-content: center;
    margin-top: 10px;
}

nav ul li {
    margin: 0 15px;
}

nav ul li a {
    color: #fff;
    text-decoration: none;
    font-weight: bold;
}

.section {
    width: 80%;
    max-width: 1000px;
    margin: 20px auto;
    padding: 20px;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.menu-container {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
}

.menu-item {
    width: 200px;
    padding: 10px;
    margin: 10px;
    text-align: center;
    background-color: #f4f4f4;
    border-radius: 5px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.menu-item h3 {
    margin-bottom: 10px;
}

.menu-item p {
    margin-bottom: 10px;
    font-size: 1.2em;
    font-weight: bold;
}

.add-to-cart {
    padding: 8px 15px;
    background-color: #ff6347;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

.add-to-cart:hover {
    background-color: #e5533d;
}

#cart {
    text-align: center;
}

#cart-items {
    margin-bottom: 10px;
}

#total-price {
    font-weight: bold;
    margin-bottom: 10px;
}

#checkout-btn {
    padding: 10px 20px;
    background-color: #ff6347;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

#checkout-btn:hover {
    background-color: #e5533d;
}

footer {
    background-color: #333;
    color: #fff;
    width: 100%;
    text-align: center;
    padding: 10px 0;
    position: fixed;
    bottom: 0;
}
				
			

Step 4: Adding Interactivity with JavaScript

Now, let’s add some JavaScript to handle adding items to the cart and calculating the total price. Open the script.js file and add the following code:

				
					// script.js

const cartItemsContainer = document.getElementById('cart-items');
const totalPriceElement = document.getElementById('total-price');
let cart = [];
let totalPrice = 0;

const menuItems = [
    { name: 'Pizza', price: 10.00 },
    { name: 'Burger', price: 5.00 },
    { name: 'Pasta', price: 7.00 }
];

// Add event listeners to all "Add to Cart" buttons
document.querySelectorAll('.add-to-cart').forEach((button, index) => {
    button.addEventListener('click', () => {
        addToCart(menuItems[index]);
    });
});

function addToCart(item) {
    cart.push(item);
    totalPrice += item.price;
    updateCart();
}

function updateCart() {
    cartItemsContainer.innerHTML = ''; // Clear previous items
    cart.forEach((item, index) => {
        const cartItem = document.createElement('div');
        cartItem.textContent = `${item.name} - $${item.price.toFixed(2)}`;
        cartItemsContainer.appendChild(cartItem);
    });
    totalPriceElement.textContent = `Total Price: $${totalPrice.toFixed(2)}`;
}

// Clear the cart when the "Checkout" button is clicked
document.getElementById('checkout-btn').addEventListener('click', () => {
    alert(`Your total is $${totalPrice.toFixed(2)}. Thank you for your order!`);
    cart = [];
    totalPrice = 0;
    updateCart();
});
				
			

Explanation of the JavaScript Code

  1. Menu Items Array: We defined a menuItems array that contains objects representing each food item (name and price).
  2. Event Listeners: We added event listeners to each “Add to Cart” button using querySelectorAll(). When a button is clicked, the corresponding item is added to the cart.
  3. Adding to Cart: The addToCart() function adds the selected item to the cart array and updates the total price.
  4. Updating Cart Display: The updateCart() function clears the previous cart items display and updates it with the current items and total price.
  5. Checkout: Clicking the “Checkout” button displays an alert with the total price and clears the cart.

Step 5: Running the Food Ordering Website

  1. Save all the files: index.html, styles.css, and script.js.
  2. Open the index.html file in your browser.
  3. You should see a food ordering interface with a menu and a cart. You can add items to the cart, see the updated total price, and click the checkout button to complete the order.

Conclusion

Congratulations! You’ve successfully built a basic food ordering website using HTML, CSS, and JavaScript. This project is a great way to learn and practice web development skills. You can enhance this site further by:

  • Adding more items to the menu dynamically using JavaScript.
  • Implementing a database or server to handle real orders.
  • Adding user authentication for personalized experiences.

Happy coding, and enjoy building your food ordering website!