Build an AI Image Generator Website in HTML CSS and JavaScript (original) (raw)

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:

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:

**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 `

<!-- 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">
    <!-- 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>

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: