Captcha Generator using HTML, CSS & JavaScript

Captcha Generator using HTML, CSS & JavaScript

Captcha Generator using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Captcha Generator which will generates a which is a verification process to check whether the surfing user is a human or robot. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Captcha Generator Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Captcha Generator Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Captcha Generator Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Captcha Generator Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Captcha Generator

First we’ll start with creating the structure of the Captcha Generator project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Captcha Generator</title>
    <!-- Font Awesome Icons -->
    <!-- Google Fonts -->
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
    <div class="container">
      <div class="wrapper">
        <canvas id="canvas" width="200" height="70"></canvas>
        <button id="reload-button">
          <i class="fa-solid fa-arrow-rotate-right"></i>
        placeholder="Enter the text in the image"
      <button id="submit-button">Submit</button>
    <!-- Script -->
    <script src="script.js"></script>

Weather App Using Html,Css And JavaScript 

CSS Code for Captcha Generator

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Captcha Generator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
body {
  height: 100vh;
  background: linear-gradient(135deg, #8052ec, #d161ff);
.container {
  width: 32em;
  background-color: #ffffff;
  padding: 5em;
  border-radius: 0.6em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  box-shadow: 0 1em 2em rgba(0, 0, 0, 0.25);
.wrapper {
  display: flex;
  align-content: center;
  justify-content: space-between;
  margin: 1em 0;
canvas {
  border: 1px solid #000000;
  border-radius: 0.4em;
button#reload-button {
  font-size: 26px;
  width: 4.6em;
  background-color: #8052ec;
  border: none;
  border-radius: 0.4em;
  color: #ffffff;
input[type="text"] {
  font-family: "Roboto Mono", monospace;
  font-size: 1.05em;
  width: 100%;
  padding: 1em 0.7em;
  border: 1px solid #000000;
  border-radius: 0.4em;
button#submit-button {
  width: 100%;
  background-color: #8052ec;
  color: #ffffff;
  font-size: 1.5em;
  border: none;
  margin-top: 1em;
  padding: 0.8em 0;
  border-radius: 0.4em;
  font-family: "Roboto Mono", monospace;

Portfolio Website using HTML and CSS (Source Code)

JavaScript Code for Captcha Generator

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Captcha Generator using HTML, CSS & JavaScript (Source Code).

//Initial References
let submitButton = document.getElementById("submit-button");
let userInput = document.getElementById("user-input");
let canvas = document.getElementById("canvas");
let reloadButton = document.getElementById("reload-button");
let text = "";

//Generate Text
const textGenerator = () => {
  let generatedText = "";
  /* String.fromCharCode gives ASCII value from a given number */
  // total 9 letters hence loop of 3
  for (let i = 0; i < 3; i++) {
    //65-90 numbers are capital letters
    generatedText += String.fromCharCode(randomNumber(65, 90));
    //97-122 are small letters
    generatedText += String.fromCharCode(randomNumber(97, 122));
    //48-57 are numbers from 0-9
    generatedText += String.fromCharCode(randomNumber(48, 57));
  return generatedText;

//Generate random numbers between a given range
const randomNumber = (min, max) =>
  Math.floor(Math.random() * (max - min + 1) + min);

//Canvas part
function drawStringOnCanvas(string) {
  //The getContext() function returns the drawing context that has all the drawing properties and functions needed to draw on canvas
  let ctx = canvas.getContext("2d");
  //clear canvas
  ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  //array of text color
  const textColors = ["rgb(0,0,0)", "rgb(130,130,130)"];
  //space between letters
  const letterSpace = 150 / string.length;
  //loop through string
  for (let i = 0; i < string.length; i++) {
    //Define initial space on X axis
    const xInitialSpace = 25;
    //Set font for canvas element
    ctx.font = "20px Roboto Mono";
    //set text color
    ctx.fillStyle = textColors[randomNumber(0, 1)];
      xInitialSpace + i * letterSpace,
      randomNumber(25, 40),

//Initial Function
function triggerFunction() {
  //clear Input
  userInput.value = "";
  text = textGenerator();
  //Randomize the text so that everytime the position of numbers and small letters is random
  text = [...text].sort(() => Math.random() - 0.5).join("");

//call triggerFunction for reload button
reloadButton.addEventListener("click", triggerFunction);

//call triggerFunction when page loads
window.onload = () => triggerFunction();

//When user clicks on submit
submitButton.addEventListener("click", () => {
  //check if user input  == generated text
  if (userInput.value === text) {
  } else {

10+ Javascript Projects For Beginners With Source Code

Final Output

We have Successfully created our Captcha Generator using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9

Leave a Reply