Build an AI Image Generator Website in HTML CSS and JavaScript
Last Updated : 31 May, 2025
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 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:
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