Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Build an AI Image Generator Website in HTML CSS and JavaScript
Next article icon

Build an AI Image Generator Website in HTML CSS and JavaScript

Last Updated : 31 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Create an AI image generator website using HTML, CSS, and JavaScript by developing a user interface that lets users input text prompts and generate images by AI.

We incorporated API integration to fetch data, providing users with an effortless and dynamic experience in generating AI-driven images. An AI image generator website should have an input bar at the top of it, which simply accepts the text entered by the user and generates an image with the help of AI related to the entered text once the user submits the form or clicks the button to generate the image.

Project Preview:

image Generator Preview
AI Image generator Website Preview

Step-by-Step Guide to Building an AI-Powered Image Generator Website

The below approach can be utilized to build an AI image generator website using HTML, CSS and JavaScript:

  • Define a webpage with meta tags, title, and sections for headings, input form, and image display.
  • Styles the webpage layout, form elements, and adjusts container and image styles responsively.
  • Manages form submission, fetches random images based on entered text, and handles errors.
  • Utilizes media queries to adjust container width and image height for different screen sizes.
  • Provides error messages for failed image fetch requests and empty input fields.

Example: The below example will explain you the process and the practical implementation of creating an AI Image generator website with the help of HTML, CSS, and JavaScript:

index.html
<!DOCTYPE html> <html lang="en">  <head>     <!-- Define the character encoding and viewport settings -->     <meta charset="UTF-8">     <meta name="viewport" content="width=device-width, initial-scale=1.0">          <!-- Page title -->     <title>       AI Image generator Website using HTML, CSS and JavaScript     </title>          <!-- Link to external CSS file for styling -->     <link rel="stylesheet" href="style.css"> </head>  <body>     <!-- Main container for all the content -->     <div class="main-container">          <!-- Container for the heading and form -->         <div class="container">                          <!-- Section for page headings -->             <div class="headings-container">                 <h1>GeeksforGeeks</h1> <!-- Main heading -->                 <h2 class="heading">                   	AI Image generator website using JavaScript                 </h2> <!-- Secondary heading explaining the purpose -->                 <h5 class="sub-heading">                   Enter the text in the below input bar and                    <br />                   get the AI generated image related to this text.                 </h5> <!-- Subheading with instructions -->             </div>              <!-- Form container for input and submit button -->             <div id="generate-image-form" class="form-container">                                  <!-- Form to input text and generate image -->                 <form class="my-form">                     <!-- Text input for the user to enter some text -->                     <input id="input-value"                             placeholder="Enter some text..."                             type="text"                            class="form-input form-controls">                      <!-- Button to submit and generate image -->                     <button type="submit"                              class="image-generate-btn                                     form-controls">                       	Generate Image                     </button>                 </form>             </div>              <!-- Section to display the generated image -->             <div id="images-visible" class="image-container">                 <!-- Placeholder text that will be updated with the result -->                 <p id="imageContainerText"></p>                                  <!-- Image tag to display the AI generated image -->                 <img id="generated-image"                       class="my-generated-image"                       src='' alt="AI Generated Image">             </div>         </div>     </div>      <script src="index.js"></script> </body>  </html> 
style.css
/* style.css */  body {     padding: 0;     margin: 0;     box-sizing: border-box; }  .main-container {     display: flex;     align-items: center;     justify-content: center; }  .container {     padding: 20px;     border: 2px solid #ccc;     width: 50%;     border-radius: 10px;     box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);     display: flex;     flex-direction: column;     align-items: center;     justify-content: center;     background-repeat: no-repeat;     background-size: cover;     color: #fff; }  .heading {     color: #318C46; }  .headings-container {     text-align: center;     color: #000; }  .form-controls {     padding: 10px;     border-radius: 5px;     border: none; }  .form-input:focus {     outline: none; }  .image-generate-btn {     background-color: #318C46;     cursor: pointer;     color: #fff; }  #imageContainerText {     color: #000; }  .image-container {     margin: 50px 0;     display: none;     text-align: center; }  .my-generated-image {     width: 100%;     height: 350px; }  @media screen and (min-width: 280px) and (max-width: 920px) {     .container {         width: 100%;     }      .my-generated-image {         width: 100%;         height: 300px;     } } 
index.js
// index.js  let generateImageForm =      document.getElementById('generate-image-form'); let formInput =      document.getElementById('input-value'); let imageContainerText =      document.getElementById('imageContainerText'); let imageGenerated =      document.getElementById('generated-image'); let imageContainer =      document.getElementById('images-visible');  async function fetchImages(category) {     try {         let response =          await fetch(`use a API`);         if (!response.ok) {             throw new Error('Unable to fetch the data');         }         imageContainerText.innerText =          "Below is your generated Image:";         imageContainer.style.display = "block";         imageGenerated.src = response.url;         console.log(response.url);     }     catch (error) {         console.log(error);     } }  generateImageForm.addEventListener('submit', (e) => {     e.preventDefault();     let enteredText = formInput.value;     if (enteredText !== "") {         fetchImages(enteredText);     }     else {         imageContainerText.innerText =              "Input field can not be empty!";     } }) 

Output:


Next Article
Build an AI Image Generator Website in HTML CSS and JavaScript

A

abhish8rzd
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Projects

Similar Reads

    Create a Resize and Compress Images in HTML CSS & JavaScript
    While using the GeeksforGeeks Write Portal to write articles, we need to upload the images. As we need to resize the image as per GeeksforGeeks's requirement, we search for different tools and websites on the internet to resize and compress the image. But, as a web developer, we can create our own i
    7 min read
    Design an Image Search App in HTML CSS & JavaScript
    Image Search Application contains an input field, which takes the choice or type of the image for which the user is searching. When the user enters the search string and clicks on the button, the top 10 images are shown to the user. If the user wants more images, then there is a Generate More button
    4 min read
    Captcha Generator using HTML CSS and JavaScript
    A captcha is a way of verifying whether a user is human or not. A captcha is made up with the help of combining letters and digits. It ensures that the user attempting to access the platform is a human. So, without wasting time, let's get started.Application of CaptchaForm Authentication: For login
    3 min read
    How to create a Blur Mask Image Website using HTML CSS and JavaScript ?
    In this article, we will see how to create a website with a blur mask image using HTML, CSS, and JavaScript. Generally, we often see this kind of effect on many websites. When clicking the button, the box immediately becomes hidden, displaying the blurred content. The website is responsive and works
    4 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
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