Build an AI Image Generator Website in HTML CSS and JavaScript Last Updated : 31 May, 2025 Summarize Comments Improve Suggest changes Share 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:AI Image generator Website PreviewStep-by-Step Guide to Building an AI-Powered Image Generator WebsiteThe 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: Comment More infoAdvertise with us A abhish8rzd Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Projects Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version 7 min read JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q 15+ min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel 7 min read CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or 7 min read Like