How To Create AN HTML5 Spinning Image CAPTCHA

Rotate to Unlock!
Rotate to Unlock!

Live stream set for 2025-09-29 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.

Build a Simple Spinning Image CAPTCHA with HTML5 and JavaScript

Want to add a fun and user-friendly CAPTCHA to your website without relying on third-party services? In this beginner-level tutorial, we will walk through building a spinning image CAPTCHA using only HTML5, CSS, and JavaScript.

This type of CAPTCHA asks the user to rotate a small part of an image until it matches the background. It is simple, effective, and a great exercise if you are just starting to learn JavaScript and front-end development.

What You Will Build

We will create a CAPTCHA that:

  • Displays a full background image
  • Rotates a small “puzzle piece” of the image
  • Lets the user rotate the piece using a button
  • Validates the solution when the piece is aligned correctly

This project is great for learning about:

  • HTML5 layout and image manipulation
  • CSS positioning and transformations
  • JavaScript event handling and logic

Code Walkthrough

Here is the full HTML5, CSS, and JavaScript code for your spinning image CAPTCHA. You can copy and paste this into an .html file and open it in your browser to test.

  <h2>Rotate the piece to complete the image</h2>

  <div id="captcha-container">
    <img id="main-image" src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Fronalpstock_big.jpg/300px-Fronalpstock_big.jpg" alt="Main Image">
    <div id="rotated-piece"></div>
  </div>

  <br>
  <button id="rotate-btn">Rotate Piece</button>
  <button id="verify-btn">Verify</button>
  <div id="result"></div>

CSS For Styling

    body {
      font-family: Arial, sans-serif;
      text-align: center;
      padding: 40px;
      background-color: #f9f9f9;
    }

    #captcha-container {
      display: inline-block;
      position: relative;
      width: 300px;
      height: 200px;
      border: 2px solid #ccc;
      background: #fff;
      border-radius: 10px;
      overflow: hidden;
    }

    #main-image {
      width: 100%;
      height: 100%;
      display: block;
    }

    #rotated-piece {
      position: absolute;
      width: 100px;
      height: 100px;
      top: 15px;
      left: 100px;
      background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/3/3f/Fronalpstock_big.jpg/300px-Fronalpstock_big.jpg');
      background-size: 300px 200px;
      background-position: -100px -50px;
      transform-origin: center center;
      transition: transform 0.3s ease;
      pointer-events: none;
    }

    #rotate-btn, #verify-btn {
      margin-top: 15px;
      padding: 10px 20px;
      font-size: 16px;
      cursor: pointer;
    }

    #result {
      margin-top: 15px;
      font-weight: bold;
    }

JavaScript For Interactivity

    const rotatedPiece = document.getElementById('rotated-piece');
    const rotateBtn = document.getElementById('rotate-btn');
    const verifyBtn = document.getElementById('verify-btn');
    const resultDiv = document.getElementById('result');

    // Initial random rotation (90, 180, 270)
    let currentRotation = [90, 180, 270][Math.floor(Math.random() * 3)];
    rotatedPiece.style.transform = `rotate(${currentRotation}deg)`;

    rotateBtn.addEventListener('click', () => {
      currentRotation = (currentRotation + 90) % 360;
      rotatedPiece.style.transform = `rotate(${currentRotation}deg)`;
    });

    verifyBtn.addEventListener('click', () => {
      if (currentRotation === 0) {
        resultDiv.textContent = '✅ CAPTCHA passed!';
        resultDiv.style.color = 'green';
      } else {
        resultDiv.textContent = '❌ Incorrect. Try rotating again.';
        resultDiv.style.color = 'red';
      }
    });

Interactive Demo

Here’s a live example of the Stunning Gallery in action:

HTML5 Stunning Gallery Demo
HTML5 Spinning Image CAPTCHA
Web Browser Displaying HTML5 Spinning Image CAPTCHA

HTML5 Spinning Image CAPTCHA Fail
Web Browser Displaying HTML5 Spinning Image CAPTCHA Failed

HTML5 Spinning Image CAPTCHA Pass
Web Browser Displaying HTML5 Spinning Image CAPTCHA Passed

HTML5 Spinning Image CAPTCHA Video

Want to Learn More?

If you are just getting started with JavaScript and want to build real-world projects like this, check out the following resources I created to help you level up:

Book:
Learning JavaScript on Amazon
A beginner-friendly guide with practical examples to help you master the fundamentals.

Course:
Learning JavaScript – Full Online Course
Interactive lessons and projects to build confidence through practice.

1-on-1 Programming Tutorials:
Need personalized help or mentorship? I am available for one-on-one sessions covering JavaScript, HTML5, and more.
Book a session here

Final Thoughts

Building a custom CAPTCHA like this not only improves your front-end skills but also helps you understand how visual feedback and interactivity work in real applications.

Try modifying the code to make the puzzle harder, or even use your own images for a personalized CAPTCHA experience.

Let me know in the comments how it worked for you, or what you would like to build next.

Recommended Resources:

Disclosure: Some of the links above are referral (affiliate) links. I may earn a commission if you purchase through them - at no extra cost to you.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP, Mastering Blender Python API, and fiction The Algorithmic Serpent, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📚 Explore His Books – Visit the Book Shop to grab your copies today.
💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.

Leave a Reply

Your email address will not be published. Required fields are marked *