Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Design a School Website in HTML CSS and JavaScript
Next article icon

Design a Recipe App in HTML CSS & JavaScript

Last Updated : 23 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We will create an attractive and styled application that gives the recipe of food, and dishes entered by the user. The user needs to enter the required food name and click on the button. The application uses an External API to fetch the food recipe and represent it in an attractive form. The user can view the detailed Recipe of Dish in the modal component which makes the application more beautiful.

Prerequisites

  • HTML
  • CSS
  • JavaScript

Approach

  • Create an HTML file with all the essential UI structures of the application. Also, include all the external CDN links to use Fonts, Icons, Animation, etc.
  • By using the <div> with the class container to encapsulate all the card components into it. This includes an input field and buttons.
  • Once the structure is created, then we need to style the application using CSS styling properties. Some of the properties include font-family, flex-direction, etc.
  • In the main JavaScript file, we will define the external API link which gives the recipe data as per user search. The defaultFn() renders the recipe of the default search query which is directly passed in the code.
  • The searchFn() mainly sends the request to the API along with the search string and gets the response of a maximum of two recipes related to the search query. The showRecpsFn() is used to show the fetched recipe to the user in UI format.
  • There is modalFn() which is responsible for representing the complete recipe instructions in the modal form to the user.

Example: We will see the implementation of the above-explained approach.

HTML
<!DOCTYPE html> <html lang="en">  <head> 	<link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"> 	<link rel="stylesheet" href= "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"> 	<link href= "https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" 	rel="stylesheet"> 	<link rel="stylesheet" href="style.css"> 	<title>GeeksforGeeks Recipe Application</title> </head>  <body> 	<div class="container"> 		<h1>GeeksforGeeks Recipe Application</h1> 		<div class="search-container"> 			<i class="fas fa-search"></i> 			<input type="text" id="searchInput" 				placeholder="Search for recipes"> 			<button id="searchBtn">Search</button> 		</div> 		<div class="recipe-container" id="recipeContainer"></div> 	</div> 	<div class="modal" id="recipeModal"> 		<div class="modal-content"> 			<span class="close" id="closeBtn">×</span> 			<div id="modalContent"></div> 		</div> 	</div> 	<script src="script.js"></script> </body>  </html> 
CSS
body { 	margin: 0; 	padding: 0; 	font-family: 'Montserrat', sans-serif; 	background: linear-gradient(to right, 			#3494E6, #EC6EAD); 	display: flex; 	flex-direction: column; 	align-items: center; 	justify-content: center; 	height: 100vh; 	overflow-x: hidden; }  .container { 	background: rgba(255, 255, 255, 0.9); 	padding: 20px; 	border-radius: 10px; 	box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 	width: 100%; 	overflow: hidden; 	text-align: center; 	max-width: 1700px; 	margin: auto; }  h1 { 	font-size: 24px; 	color: green; 	margin-bottom: 20px; }  .search-container { 	display: flex; 	align-items: center; 	margin-bottom: 20px; }  i { 	font-size: 24px; 	margin-right: 10px; 	color: #333; }  input { 	flex: 1; 	padding: 10px; 	font-size: 16px; 	border: none; 	border-radius: 5px; 	border-color: #FF4848; }  button { 	padding: 10px 20px; 	font-size: 16px; 	border: none; 	border-radius: 5px; 	cursor: pointer; 	background: #FF4848; 	color: #fff; }  #searchBtn:hover { 	background: #FF6565; }  .recipe-container { 	display: grid; 	grid-template-columns: repeat(auto-fill, 			minmax(200px, 1fr)); 	gap: 10px; 	max-width: 100%; 	overflow-y: auto; 	max-height: 70vh; }  .recipe-card { 	background: linear-gradient(to right, 			#a0f8a4, rgb(160, 212, 255), 90%, 54%); 	border-radius: 20px; 	padding: 8px; 	box-shadow: 0 2px 5px rgb(255, 3, 3); 	transition: transform 0.3s ease-in-out; 	overflow: hidden; 	text-align: center; }  .recipe-card:hover { 	transform: scale(1.03); }  .recipe-card img { 	max-width: 70%; 	height: 40%; 	border-radius: 5px; 	margin-bottom: 10px; }  .recipe-card h3 { 	margin-bottom: 5px; 	font-size: 14px; }  .recipe-card p { 	font-size: 14px; 	color: rgb(0, 0, 0); 	margin-bottom: 10px; }  .show-recipe-btn { 	background: #FF4848; 	color: #fff; 	padding: 8px; 	border: none; 	border-radius: 5px; 	cursor: pointer; 	margin-top: 10px; }  .show-recipe-btn:hover { 	background: #FF6565; }  .modal { 	display: none; 	position: fixed; 	z-index: 1000; 	top: 0; 	left: 0; 	width: 100%; 	height: 100%; 	background: rgba(0, 0, 0, 0.5); }  .modal-content { 	position: absolute; 	top: 50%; 	left: 50%; 	transform: translate(-50%, -50%); 	background: #fff; 	padding: 20px; 	border-radius: 10px; 	box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 	max-width: 400px; 	width: 100%; }  .close { 	position: absolute; 	top: 10px; 	right: 10px; 	font-size: 20px; 	color: #555; 	cursor: pointer; }  @media screen and (max-width: 600px) { 	.recipe-container { 		grid-template-columns: repeat(auto-fill, 				minmax(100%, 1fr)); 	} }  .search-container { 	display: flex; 	align-items: center; 	margin-bottom: 20px; }  i { 	font-size: 24px; 	margin-right: 10px; 	color: #333; }  input { 	flex: 1; 	padding: 10px; 	font-size: 16px; 	border: none; 	border-radius: 5px; 	border-color: #FF4848; }  button { 	padding: 10px 20px; 	font-size: 16px; 	border: none; 	border-radius: 5px; 	cursor: pointer; 	background: #FF4848; 	color: #fff; }  #searchBtn:hover { 	background: #FF6565; }  .recipe-container { 	display: grid; 	grid-template-columns: repeat(auto-fill, 			minmax(200px, 1fr)); 	gap: 10px; 	max-width: 100%; 	overflow-y: auto; 	max-height: 70vh; }  .recipe-card { 	background: linear-gradient(to right, 			#a0f8a4, 			rgb(160, 212, 255), 90%, 54%); 	border-radius: 20px; 	padding: 8px; 	box-shadow: 0 2px 5px rgb(255, 3, 3); 	transition: transform 0.3s ease-in-out; 	overflow: hidden; 	text-align: center; }  .recipe-card:hover { 	transform: scale(1.03); }  .recipe-card img { 	max-width: 70%; 	height: 40%; 	border-radius: 5px; 	margin-bottom: 10px; }  .recipe-card h3 { 	margin-bottom: 5px; 	font-size: 14px; }  .recipe-card p { 	font-size: 14px; 	color: rgb(0, 0, 0); 	margin-bottom: 10px; }  .show-recipe-btn { 	background: #FF4848; 	color: #fff; 	padding: 8px; 	border: none; 	border-radius: 5px; 	cursor: pointer; 	margin-top: 10px; }  .show-recipe-btn:hover { 	background: #FF6565; }  .modal { 	display: none; 	position: fixed; 	z-index: 1000; 	top: 0; 	left: 0; 	width: 100%; 	height: 100%; 	background: rgba(0, 0, 0, 0.5); }  .modal-content { 	position: absolute; 	top: 50%; 	left: 50%; 	transform: translate(-50%, -50%); 	background: #fff; 	padding: 20px; 	border-radius: 10px; 	box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 	max-width: 400px; 	width: 100%; }  .close { 	position: absolute; 	top: 10px; 	right: 10px; 	font-size: 20px; 	color: #555; 	cursor: pointer; }  @media screen and (max-width: 600px) { 	.recipe-container { 		grid-template-columns: repeat(auto-fill, 				minmax(100%, 1fr)); 	} } 
JavaScript
// script.js const apiUrl = 	'...'; function defaultFn() { 	const defaultFood = 'chicken'; 	searchFn(defaultFood); } document.getElementById('searchBtn') 	.addEventListener('click', () => { 		const userIn = document.getElementById('searchInput') 			.value.trim(); 		if (userIn !== '') { 			searchFn(userIn); 		} else { 			alert('Please enter a recipe name.'); 		} 	}); document.addEventListener('click', (event) => { 	if (event.target.className === 'show-recipe-btn') { 		const rId = event.target.getAttribute('data-id'); 		modalFn(rId); 	} 	if (event.target.id === 'closeBtn') { 		closeModalFn(); 	} }); defaultFn(); function searchFn(query) { 	const url = `${apiUrl}${query}`; 	fetch(url) 		.then(res => res.json()) 		.then(tmp => { 			if (tmp.meals) { 				showRecpsFn(tmp.meals); 			} else { 				noRecFn(); 			} 		}) 		.catch(error => console 			.error('Error fetching recipes:', error)); } function showRecpsFn(r) { 	const rCont = document.getElementById('recipeContainer'); 	rCont.innerHTML = ''; 	r.slice(0, 20).forEach(recipe => { 		const c = document.createElement('div'); 		c.classList.add('animate__animated', 			'animate__fadeIn', 'recipe-card'); 		c.innerHTML = ` 			<h3>${recipe.strMeal}</h3> 			<img src="${recipe.strMealThumb}" 			alt="${recipe.strMeal}"> 			<p>${recipe.strArea}</p> 			<p>${recipe.strCategory}</p> 			<button class="show-recipe-btn" 			data-id="${recipe.idMeal}">Show Recipe</button> 		`;  		rCont.appendChild(c); 	}); 	if (r.length === 1) { 		const card = rCont.firstChild; 		card.style.margin = 'auto'; 	} } function noRecFn() { 	const rCont = document.getElementById('recipeContainer'); 	rCont.innerHTML = '<p>No Recipe found</p>'; } function modalFn(recipeId) { 	const mData = document.getElementById('modalContent'); 	mData.innerHTML = ''; 	fetch(` ...`) 		.then(response => response.json()) 		.then(data => { 			const rep = data.meals[0]; 			mData.innerHTML = ` 				<h2>${rep.strMeal}</h2> 				<h3>Instructions:</h3> 				<p>${formatFn(rep.strInstructions)}</p> 			`; 			document.getElementById('recipeModal') 				.style.display = 'block'; 		}) 		.catch(error => console.error('Error fetching recipe details:', 			error)); } function formatFn(instructions) { 	return instructions.split('\r\n').filter(instruction => 		instruction.trim() !== '').join('<br>'); } function closeModalFn() { 	document.getElementById('recipeModal').style.display = 'none'; } 

Output:


Next Article
Design a School Website in HTML CSS and JavaScript

G

gauravggeeksforgeeks
Improve
Article Tags :
  • Project
  • JavaScript
  • Web Technologies
  • Dev Scripter
  • JavaScript-Projects
  • Dev Scripter 2024

Similar Reads

  • Design a School Website in HTML CSS and JavaScript
    A School Website developed using HTML, CSS, and JavaScript is an interactive platform that provides information about the school, its faculty, courses, events, and other relevant details. It incorporates responsive design principles to ensure compatibility across various devices, offering an engagin
    8 min read
  • Design Joke Generator App in HTML CSS & JavaScript
    We will go to learn how can we create a Joke generator app using HTML, CSS, and JavaScript. We will also add a feature to copy the generated joke. We will use API to fetch the jokes and will show those jokes on the screen. PrerequisitesHTMLCSSJavaScriptApproachCreate the Joke Generator Application U
    3 min read
  • Design a Online Cake Shop Website in HTML CSS & JavaScript
    Every business is going online nowadays for better growth and productivity. We are going to build an online cake shop website that will represent the shop in a good manner having features of online ordering and views and many more. PrerequisitesHTMLCSSJavaScriptApproachWe first create an HTML file w
    14 min read
  • Build A Weather App in HTML CSS & JavaScript
    A weather app contains a user input field for the user, which takes the input of the city name. Once the user enters the city name and clicks on the button, then the API Request is been sent to the OpenWeatherMap and the response is been retrieved in the application which consists of weather, wind s
    7 min read
  • Design a Survey Form using HTML CSS and JavaScript
    In this article, we are going to implement a survey form using HTML, CSS, and JavaScript. In this form, we will get data about the interest of people in sports. In this implementation, the data will be stored in a CSV file after successful form validations. Preview of final output: Let us have a loo
    5 min read
  • Design a Online Grocery Website in HTML CSS & JavaScript
    Online grocery shopping has become increasingly popular, especially in recent times. This project aims to create a user-friendly and responsive website for purchasing groceries online. Users can browse through the various categories, add items to their cart, and proceed to checkout making their groc
    5 min read
  • Design a Student Attendance Portal in HTML CSS & JavaScript
    The Student Attendance Portal is a web application that allows users to mark attendance for students in different classes, add class and students according to requirements, providing a user-friendly interface to update and visualize attendance records. Approach:In the first step, we will create a fo
    10 min read
  • Design Responsive Flower Store in HTML CSS & JavaScript
    In this article, we will see how can we design a Responsive Flower Store with the help of HTML, CSS, and JavaScript. Prerequisites:HTMLCSSJAVASCRIPT Approach:Created a login form using basic html CSS Created Signup form .Created a Sign out Cart page on the main page first Created a navbar with home
    15+ min read
  • Create a Lyrics Search App in HTML CSS & JavaScript
    A Lyrics Search Application contains an input field, which takes the input as Song Name or Artist Name. Once the user enters the search query, the API Request is made to lyrics.ovh, and then the response is retrieved with the first 5 songs list of the search query. Users can view the lyrics of any o
    5 min read
  • Design a Tip Calculator using HTML, CSS and JavaScript
    The tip is the money given as a gift for good service, to the person who serves you in a restaurant. In this project, a simple tip calculator is made which takes the billing amount, type of service, and the number of persons as input. As per the three inputs, it generates a tip for the serving perso
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences