• ;
  • A Coding Boy
  • Blog
  • Videos
  • Projects
  • Home
HTML AND CSS
LOGIN PAGE
WEBSITE DESGIN
API PROJECTS
CARD DESGIN
JAVASCRIPTS GAMES
JAVASCRIPT PROJECTS
JAVA PROJECTS
PYTHON PROJECTS
demo post
only for demo nasa prospect
only for demo the gonnies
most popular
how to create parallax website
how to create a beautiful card
how to create a netflix login page
how to create flipping ui card
how to create image generator website
Build An AI Image Generator Website in HTML CSS and JavaScript
On this custom AI image generation website, users enter their prompt, choose the number of images they want to generate, and click the “Generate” button. The AI will then create images based on the user prompt. To download each image, the user can click on the corresponding download button provided.
If you’re unfamiliar, Midjourny and DALL-E are AI image-generation websites. They use a machine-learning model to create images based on text descriptions. Users can input their desired image prompt, and these websites will generate a corresponding image that matches the description.
AI Image Generator Website [Source Code]
First, paste the following codes into your index.html file.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<!-- Coding By CodingNepal - www.codingnepalweb.com -->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Image Generator HTML CSS and JavaScript | CodingNepal</title>
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
</head>
<body>
<section class="image-generator">
<div class="content">
<h1>AI Image Generator Tool JavaScript</h1>
<p>Convert your text into an image within a second using this
JavaScript-powered AI Image Generator tool.</p>
<form action="#" class="generate-form">
<input class="prompt-input" type="text" placeholder="Describe what you want to see" required>
<div class="controls">
<select class="img-quantity">
<option value="1">1 Image</option>
<option value="2">2 Images</option>
<option value="3">3 Images</option>
<option value="4" selected>4 Images</option>
</select>
<button type="submit" class="generate-btn">Generate</button>
</div>
</form>
</div>
</section>
<section class="image-gallery">
<div class="img-card"><img src="images/img-1.jpg" alt="image"></div>
<div class="img-card"><img src="images/img-2.jpg" alt="image"></div>
<div class="img-card"><img src="images/img-3.jpg" alt="image"></div>
<div class="img-card"><img src="images/img-4.jpg" alt="image"></div>
</section>
</body>
</html>
<!DOCTYPE html> <!-- Coding By CodingNepal - www.codingnepalweb.com --> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>AI Image Generator HTML CSS and JavaScript | CodingNepal</title> <link rel="stylesheet" href="style.css"> <script src="script.js" defer></script> </head> <body> <section class="image-generator"> <div class="content"> <h1>AI Image Generator Tool JavaScript</h1> <p>Convert your text into an image within a second using this JavaScript-powered AI Image Generator tool.</p> <form action="#" class="generate-form"> <input class="prompt-input" type="text" placeholder="Describe what you want to see" required> <div class="controls"> <select class="img-quantity"> <option value="1">1 Image</option> <option value="2">2 Images</option> <option value="3">3 Images</option> <option value="4" selected>4 Images</option> </select> <button type="submit" class="generate-btn">Generate</button> </div> </form> </div> </section> <section class="image-gallery"> <div class="img-card"><img src="images/img-1.jpg" alt="image"></div> <div class="img-card"><img src="images/img-2.jpg" alt="image"></div> <div class="img-card"><img src="images/img-3.jpg" alt="image"></div> <div class="img-card"><img src="images/img-4.jpg" alt="image"></div> </section> </body> </html>
Second, paste the following codes into your style.css file
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
/* Importing Google font - Poppins */
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
.image-generator {
height: 40vh;
display: flex;
align-items: center;
justify-content: center;
position: relative;
background: url("images/bg.jpg");
background-size: cover;
background-position: center;
}
.image-generator::before {
content: "";
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
opacity: 0.5;
background: #121212;
}
.image-generator .content {
position: relative;
color: #fff;
padding: 0 15px;
max-width: 760px;
text-align: center;
}
.image-generator h1 {
font-size: 2.5rem;
font-weight: 700;
}
.image-generator p {
margin-top: 10px;
font-size: 1.35rem;
}
.image-generator .generate-form {
height: 56px;
padding: 6px;
display: flex;
margin-bottom: 15px;
background: #fff;
align-items: center;
border-radius: 30px;
margin-top: 45px;
justify-content: space-between;
}
.generate-form .prompt-input {
width: 100%;
height: 100%;
outline: none;
padding: 0 17px;
border: none;
background: none;
font-size: 1rem;
border-radius: 30px;
}
.generate-form .controls {
display: flex;
height: 100%;
gap: 15px;
}
.generate-form .img-quantity {
outline: none;
border: none;
height: 44px;
background: none;
font-size: 1rem;
}
.generate-form .generate-btn {
font-size: 1rem;
outline: none;
border: none;
font-weight: 500;
color: #fff;
cursor: pointer;
height: 100%;
padding: 0 25px;
border-radius: 30px;
background: #4949E7;
}
.generate-form .generate-btn[disabled] {
opacity: 0.6;
pointer-events: none;
}
.generate-form button:hover {
background: #1d1de2;
}
.image-gallery {
display: flex;
gap: 15px;
padding: 0 15px;
flex-wrap: wrap;
justify-content: center;
margin: 50px auto;
max-width: 1250px;
}
.image-gallery .img-card {
display: flex;
position: relative;
align-items: center;
justify-content: center;
background: #f2f2f2;
border-radius: 4px;
overflow: hidden;
aspect-ratio: 1 / 1;
width: 285px;
}
.image-gallery .img-card img {
height: 100%;
width: 100%;
object-fit: cover;
}
.image-gallery .img-card.loading img {
width: 80px;
height: 80px;
}
.image-gallery .img-card .download-btn {
bottom: 15px;
right: 15px;
height: 36px;
width: 36px;
display: flex;
align-items: center;
justify-content: center;
text-decoration: none;
background: #fff;
border-radius: 50%;
position: absolute;
opacity: 0;
pointer-events: none;
transition: 0.2s ease;
}
.image-gallery .img-card .download-btn img {
width: 14px;
height: 14px;
}
.image-gallery .img-card:not(.loading):hover .download-btn {
opacity: 1;
pointer-events: auto;
}
@media screen and (max-width: 760px) {
.image-generator {
height: 45vh;
padding-top: 30px;
align-items: flex-start;
}
.image-generator h1 {
font-size: 1.8rem;
}
.image-generator p {
font-size: 1rem;
}
.image-generator .generate-form {
margin-top: 30px;
height: 52px;
display: block;
}
.generate-form .controls {
height: 40px;
margin-top: 15px;
justify-content: end;
align-items: center;
}
.generate-form .generate-btn[disabled] {
opacity: 1;
}
.generate-form .img-quantity {
color: #fff;
}
.generate-form .img-quantity option {
color: #000;
}
.image-gallery {
margin-top: 20px;
}
.image-gallery .img-card:not(.loading) .download-btn {
opacity: 1;
pointer-events: auto;
}
}
@media screen and (max-width: 500px) {
.image-gallery .img-card {
width: 100%;
}
}
/* Importing Google font - Poppins */ @import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } .image-generator { height: 40vh; display: flex; align-items: center; justify-content: center; position: relative; background: url("images/bg.jpg"); background-size: cover; background-position: center; } .image-generator::before { content: ""; position: absolute; left: 0; top: 0; width: 100%; height: 100%; opacity: 0.5; background: #121212; } .image-generator .content { position: relative; color: #fff; padding: 0 15px; max-width: 760px; text-align: center; } .image-generator h1 { font-size: 2.5rem; font-weight: 700; } .image-generator p { margin-top: 10px; font-size: 1.35rem; } .image-generator .generate-form { height: 56px; padding: 6px; display: flex; margin-bottom: 15px; background: #fff; align-items: center; border-radius: 30px; margin-top: 45px; justify-content: space-between; } .generate-form .prompt-input { width: 100%; height: 100%; outline: none; padding: 0 17px; border: none; background: none; font-size: 1rem; border-radius: 30px; } .generate-form .controls { display: flex; height: 100%; gap: 15px; } .generate-form .img-quantity { outline: none; border: none; height: 44px; background: none; font-size: 1rem; } .generate-form .generate-btn { font-size: 1rem; outline: none; border: none; font-weight: 500; color: #fff; cursor: pointer; height: 100%; padding: 0 25px; border-radius: 30px; background: #4949E7; } .generate-form .generate-btn[disabled] { opacity: 0.6; pointer-events: none; } .generate-form button:hover { background: #1d1de2; } .image-gallery { display: flex; gap: 15px; padding: 0 15px; flex-wrap: wrap; justify-content: center; margin: 50px auto; max-width: 1250px; } .image-gallery .img-card { display: flex; position: relative; align-items: center; justify-content: center; background: #f2f2f2; border-radius: 4px; overflow: hidden; aspect-ratio: 1 / 1; width: 285px; } .image-gallery .img-card img { height: 100%; width: 100%; object-fit: cover; } .image-gallery .img-card.loading img { width: 80px; height: 80px; } .image-gallery .img-card .download-btn { bottom: 15px; right: 15px; height: 36px; width: 36px; display: flex; align-items: center; justify-content: center; text-decoration: none; background: #fff; border-radius: 50%; position: absolute; opacity: 0; pointer-events: none; transition: 0.2s ease; } .image-gallery .img-card .download-btn img { width: 14px; height: 14px; } .image-gallery .img-card:not(.loading):hover .download-btn { opacity: 1; pointer-events: auto; } @media screen and (max-width: 760px) { .image-generator { height: 45vh; padding-top: 30px; align-items: flex-start; } .image-generator h1 { font-size: 1.8rem; } .image-generator p { font-size: 1rem; } .image-generator .generate-form { margin-top: 30px; height: 52px; display: block; } .generate-form .controls { height: 40px; margin-top: 15px; justify-content: end; align-items: center; } .generate-form .generate-btn[disabled] { opacity: 1; } .generate-form .img-quantity { color: #fff; } .generate-form .img-quantity option { color: #000; } .image-gallery { margin-top: 20px; } .image-gallery .img-card:not(.loading) .download-btn { opacity: 1; pointer-events: auto; } } @media screen and (max-width: 500px) { .image-gallery .img-card { width: 100%; } }
Last, paste the following codes into your script.js file. Please note that your website is still unable to generate AI images because you have not provided your API key in the OPENAI_API_KEY variable. We’re using OpenAI API to generate images. So to get a free API key from OpenAI, sign up for an account at https:// platform.openai. com/account/ api-keys.
Plain text
Copy to clipboard
Open in new window
EnlighterJS 3 Syntax Highlighter
const generateForm = document.querySelector(".generate-form");
const generateBtn = generateForm.querySelector(".generate-btn");
const imageGallery = document.querySelector(".image-gallery");
const OPENAI_API_KEY = "YOUR-OPENAI-API-KEY-HERE"; // Your OpenAI API key here
let isImageGenerating = false;
const updateImageCard = (imgDataArray) => {
imgDataArray.forEach((imgObject, index) => {
const imgCard = imageGallery.querySelectorAll(".img-card")[index];
const imgElement = imgCard.querySelector("img");
const downloadBtn = imgCard.querySelector(".download-btn");
// Set the image source to the AI-generated image data
const aiGeneratedImage = `data:image/jpeg;base64,${imgObject.b64_json}`;
imgElement.src = aiGeneratedImage;
// When the image is loaded, remove the loading class and set download attributes
imgElement.onload = () => {
imgCard.classList.remove("loading");
downloadBtn.setAttribute("href", aiGeneratedImage);
downloadBtn.setAttribute("download", `${new Date().getTime()}.jpg`);
}
});
}
const generateAiImages = async (userPrompt, userImgQuantity) => {
try {
// Send a request to the OpenAI API to generate images based on user inputs
const response = await fetch("https://api.openai.com/v1/images/generations", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_API_KEY}`,
},
body: JSON.stringify({
prompt: userPrompt,
n: userImgQuantity,
size: "512x512",
response_format: "b64_json"
}),
});
// Throw an error message if the API response is unsuccessful
if(!response.ok) throw new Error("Failed to generate AI images. Make sure your API key is valid.");
const { data } = await response.json(); // Get data from the response
updateImageCard([...data]);
} catch (error) {
alert(error.message);
} finally {
generateBtn.removeAttribute("disabled");
generateBtn.innerText = "Generate";
isImageGenerating = false;
}
}
const handleImageGeneration = (e) => {
e.preventDefault();
if(isImageGenerating) return;
// Get user input and image quantity values
const userPrompt = e.srcElement[0].value;
const userImgQuantity = parseInt(e.srcElement[1].value);
// Disable the generate button, update its text, and set the flag
generateBtn.setAttribute("disabled", true);
generateBtn.innerText = "Generating";
isImageGenerating = true;
// Creating HTML markup for image cards with loading state
const imgCardMarkup = Array.from({ length: userImgQuantity }, () =>
`<div class="img-card loading">
<img src="images/loader.svg" alt="AI generated image">
<a class="download-btn" href="#">
<img src="images/download.svg" alt="download icon">
</a>
</div>`
).join("");
imageGallery.innerHTML = imgCardMarkup;
generateAiImages(userPrompt, userImgQuantity);
}
generateForm.addEventListener("submit", handleImageGeneration);
const generateForm = document.querySelector(".generate-form"); const generateBtn = generateForm.querySelector(".generate-btn"); const imageGallery = document.querySelector(".image-gallery"); const OPENAI_API_KEY = "YOUR-OPENAI-API-KEY-HERE"; // Your OpenAI API key here let isImageGenerating = false; const updateImageCard = (imgDataArray) => { imgDataArray.forEach((imgObject, index) => { const imgCard = imageGallery.querySelectorAll(".img-card")[index]; const imgElement = imgCard.querySelector("img"); const downloadBtn = imgCard.querySelector(".download-btn"); // Set the image source to the AI-generated image data const aiGeneratedImage = `data:image/jpeg;base64,${imgObject.b64_json}`; imgElement.src = aiGeneratedImage; // When the image is loaded, remove the loading class and set download attributes imgElement.onload = () => { imgCard.classList.remove("loading"); downloadBtn.setAttribute("href", aiGeneratedImage); downloadBtn.setAttribute("download", `${new Date().getTime()}.jpg`); } }); } const generateAiImages = async (userPrompt, userImgQuantity) => { try { // Send a request to the OpenAI API to generate images based on user inputs const response = await fetch("https://api.openai.com/v1/images/generations", { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${OPENAI_API_KEY}`, }, body: JSON.stringify({ prompt: userPrompt, n: userImgQuantity, size: "512x512", response_format: "b64_json" }), }); // Throw an error message if the API response is unsuccessful if(!response.ok) throw new Error("Failed to generate AI images. Make sure your API key is valid."); const { data } = await response.json(); // Get data from the response updateImageCard([...data]); } catch (error) { alert(error.message); } finally { generateBtn.removeAttribute("disabled"); generateBtn.innerText = "Generate"; isImageGenerating = false; } } const handleImageGeneration = (e) => { e.preventDefault(); if(isImageGenerating) return; // Get user input and image quantity values const userPrompt = e.srcElement[0].value; const userImgQuantity = parseInt(e.srcElement[1].value); // Disable the generate button, update its text, and set the flag generateBtn.setAttribute("disabled", true); generateBtn.innerText = "Generating"; isImageGenerating = true; // Creating HTML markup for image cards with loading state const imgCardMarkup = Array.from({ length: userImgQuantity }, () => `<div class="img-card loading"> <img src="images/loader.svg" alt="AI generated image"> <a class="download-btn" href="#"> <img src="images/download.svg" alt="download icon"> </a> </div>` ).join(""); imageGallery.innerHTML = imgCardMarkup; generateAiImages(userPrompt, userImgQuantity); } generateForm.addEventListener("submit", handleImageGeneration);
If you encounter any problems while building your AI-powered image generation website, you can download the source code files for this project for free by clicking the Download button. Remember that after downloading the file, you’ll have to provide your valid OpenAI API key into the OPENAI_API_KEY variable in the script.js file.
live demo
Download files
Info
Home
Projects
Blogs
Videos
About us
Follow us for more!
© 2023 All Rights Reserved A Coding Boy

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Glassmorphism Login Form | CodingNepal</title> <link rel="stylesheet" href="style.css"> </head> <body> <div class="wrapper"> <form action="#"> <h2>Login</h2> <div class="input-field"> <input type="text" required> <label>Enter your email</label> </div> <div class="input-field"> <input type="password" required> <label>Enter your password</label> </div> <div class="forget"> <label for="remember"> <input type="checkbox" id="remember"> <p>Remember me</p> </label> <a href="#">Forgot password?</a> </div> <button type="submit">Log In</button> <div class="register"> <p>Don't have an account? <a href="#">Register</a></p> </div> </form> </div> </body> </html>

@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Open Sans", sans-serif; } body { display: flex; align-items: center; justify-content: center; min-height: 100vh; width: 100%; padding: 0 10px; } body::before { content: ""; position: absolute; width: 100%; height: 100%; background: url("https://www.codingnepalweb.com/demos/create-glassmorphism-login-form-html-css/hero-bg.jpg"), #000; background-position: center; background-size: cover; } .wrapper { width: 400px; border-radius: 8px; padding: 30px; text-align: center; border: 1px solid rgba(255, 255, 255, 0.5); backdrop-filter: blur(9px); -webkit-backdrop-filter: blur(9px); } form { display: flex; flex-direction: column; } h2 { font-size: 2rem; margin-bottom: 20px; color: #fff; } .input-field { position: relative; border-bottom: 2px solid #ccc; margin: 15px 0; } .input-field label { position: absolute; top: 50%; left: 0; transform: translateY(-50%); color: #fff; font-size: 16px; pointer-events: none; transition: 0.15s ease; } .input-field input { width: 100%; height: 40px; background: transparent; border: none; outline: none; font-size: 16px; color: #fff; } .input-field input:focus~label, .input-field input:valid~label { font-size: 0.8rem; top: 10px; transform: translateY(-120%); } .forget { display: flex; align-items: center; justify-content: space-between; margin: 25px 0 35px 0; color: #fff; } #remember { accent-color: #fff; } .forget label { display: flex; align-items: center; } .forget label p { margin-left: 8px; } .wrapper a { color: #efefef; text-decoration: none; } .wrapper a:hover { text-decoration: underline; } button { background: #fff; color: #000; font-weight: 600; border: none; padding: 12px 20px; cursor: pointer; border-radius: 3px; font-size: 16px; border: 2px solid transparent; transition: 0.3s ease; } button:hover { color: #fff; border-color: #fff; background: rgba(255, 255, 255, 0.15); } .register { text-align: center; margin-top: 30px; color: #fff;}