Recipe App using HTML, CSS & JavaScript

Recipe App using HTML, CSS & JavaScript

Recipe App using HTML, CSS & JavaScript

Introduction

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Recipe App which will some recipes when the user will search in the box. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Recipe App Project.

Recipe App Javascript

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Recipe App Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Recipe App Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Recipe App Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Recipe App

First we’ll start with creating the structure of the Recipe App project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Recipe App</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="search-container">
        <input
          type="text"
          placeholder="Type A Dish Name Here.."
          id="user-inp"
        />
        <button id="search-btn">Search</button>
      </div>
      <div id="result"></div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

5+ HTML CSS Projects With Source Code

CSS Code for Recipe App

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Recipe App project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  background-color: #f4c531;
}
.container {
  background-color: #ffffff;
  font-size: 16px;
  padding: 3em 2.8em;
  width: 90vw;
  max-width: 32em;
  position: absolute;
  transform: translate(-50%, -50%);
  left: 50%;
  top: 50%;
  border-radius: 0.6em;
}
.search-container {
  width: 100%;
  display: grid;
  grid-template-columns: 9fr 3fr;
  gap: 1.2em;
}
.search-container input {
  font-size: 1em;
  padding: 0.6em;
  border: none;
  outline: none;
  border-bottom: 2px solid #202030;
}
.search-container button {
  font-size: 1em;
  background-color: #f4c531;
  border: none;
  font-weight: 600;
  border-radius: 0.3em;
}
img {
  display: block;
  width: 50%;
  margin: 1.8em auto 0 auto;
}
.details {
  background-color: #f4c531;
  position: relative;
  margin: -4.3em 0 0 0;
  text-align: center;
  padding: 0.6em 0;
}
.details h2 {
  font-size: 1.2em;
  font-weight: 600;
}
.details h4 {
  font-size: 1em;
  font-weight: 400;
}
#show-recipe {
  font-size: 1em;
  position: relative;
  left: 75%;
  padding: 0.9em 0.6em;
  background-color: #f4c531;
  border: none;
  top: 1.5em;
  border-radius: 0.3em;
  font-weight: 600;
}
#recipe {
  position: absolute;
  background-color: #ffffff;
  min-height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 2;
  border-radius: 0.6em;
  display: none;
}
#recipe pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  padding: 2.5em 1.2em 1.2em 1.2em;
  font-size: 0.92em;
  color: #303040;
}
#hide-recipe {
  font-size: 1em;
  position: relative;
  width: 1.8em;
  height: 1.8em;
  background-color: #f4c531;
  border: none;
  top: 1.2em;
  left: 90%;
  border-radius: 0.3em;
}
ul {
  font-size: 1em;
  position: relative;
  display: grid;
  grid-template-columns: auto auto;
  gap: 0.8em 1.1em;
  padding: 1.2em 0 0 1.2em;
  color: #303040;
  text-transform: capitalize;
}
h3 {
  text-align: center;
  margin-top: 1.8em;
  color: #202030;
}
@media screen and (max-width: 500px) {
  .container {
    font-size: 14px;
  }
}

25+ HTML & CSS Card Design Layout Style

JavaScript Code for Recipe App

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Recipe App using HTML, CSS & JavaScript (Source Code).

//Initial References
let result = document.getElementById("result");
let searchBtn = document.getElementById("search-btn");
let url = "https://www.themealdb.com/api/json/v1/1/search.php?s=";

searchBtn.addEventListener("click", () => {
  let userInp = document.getElementById("user-inp").value;
  if (userInp.length == 0) {
    result.innerHTML = `<h3>Input Field Cannot Be Empty</h3>`;
  } else {
    fetch(url + userInp)
      .then((response) => response.json())
      .then((data) => {
        let myMeal = data.meals[0];
        console.log(myMeal);
        console.log(myMeal.strMealThumb);
        console.log(myMeal.strMeal);
        console.log(myMeal.strArea);
        console.log(myMeal.strInstructions);
        let count = 1;
        let ingredients = [];
        for (let i in myMeal) {
          let ingredient = "";
          let measure = "";
          if (i.startsWith("strIngredient") && myMeal[i]) {
            ingredient = myMeal[i];
            measure = myMeal[`strMeasure` + count];
            count += 1;
            ingredients.push(`${measure} ${ingredient}`);
          }
        }
        console.log(ingredients);

        result.innerHTML = `
    <img src=${myMeal.strMealThumb}>
    <div class="details">
        <h2>${myMeal.strMeal}</h2>
        <h4>${myMeal.strArea}</h4>
    </div>
    <div id="ingredient-con"></div>
    <div id="recipe">
        <button id="hide-recipe">X</button>
        <pre id="instructions">${myMeal.strInstructions}</pre>
    </div>
    <button id="show-recipe">View Recipe</button>
    `;
        let ingredientCon = document.getElementById("ingredient-con");
        let parent = document.createElement("ul");
        let recipe = document.getElementById("recipe");
        let hideRecipe = document.getElementById("hide-recipe");
        let showRecipe = document.getElementById("show-recipe");

        ingredients.forEach((i) => {
          let child = document.createElement("li");
          child.innerText = i;
          parent.appendChild(child);
          ingredientCon.appendChild(parent);
        });

        hideRecipe.addEventListener("click", () => {
          recipe.style.display = "none";
        });
        showRecipe.addEventListener("click", () => {
          recipe.style.display = "block";
        });
      })
      .catch(() => {
        result.innerHTML = `<h3>Invalid Input</h3>`;
      });
  }
});

Output

Live Preview of Recipe App using HTML, CSS & JavaScript

See the Pen
Recipe
by Harsh Sawant (@harshh9)
on CodePen.

We have Successfully created our Recipe App using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply