Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    How to create Animated Ghost Toggle using HTML CSS and JS

    9 October 2025

    How to make Magnetic Button Hover Effect using HTML & CSS

    6 October 2025

    How to make Animated Electric Card using HTML & CSS

    4 October 2025
    Facebook X (Twitter) Instagram YouTube Telegram Threads
    Coding StellaCoding Stella
    • Home
    • Blog
    • HTML & CSS
      • Login Form
    • JavaScript
    • Hire us!
    Coding StellaCoding Stella
    Home - JavaScript - How to create Animated Ghost Toggle using HTML CSS and JS
    JavaScript

    How to create Animated Ghost Toggle using HTML CSS and JS

    Coding StellaBy Coding Stella9 October 2025Updated:9 October 2025No Comments5 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Email WhatsApp Copy Link

    Let’s create an Animated Ghost Toggle using HTML, CSS, and JavaScript. This fun and spooky project will feature a cute ghost that appears or disappears when toggled, adding a playful animation to your web page. 👻

    We’ll use:

    • HTML to structure the toggle and ghost elements.
    • CSS to style the ghost with smooth floating and glowing animations.
    • JavaScript to handle the toggle functionality that shows or hides the ghost with a fun effect.

    Whether you’re a beginner or an experienced developer, this project is a great way to practice combining creativity with interactivity. Let’s get started and bring this friendly ghost to life! 💫

    HTML :

    This code creates a fun ghost toggle switch animation. When you toggle the switch, a ghost appears with the text “Boo!”. The HTML sets up the structure – a checkbox inside a styled switch – while the linked CSS handles the design and animation of the ghost and slider. The JavaScript file adds the interactivity, making the ghost show or hide smoothly when the switch is clicked.

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <title>Animated Ghost Toggle | @coding.stella</title>
      <link rel="stylesheet" href="https://public.codepenassets.com/css/normalize-5.0.0.min.css"/>
      <link rel="stylesheet" href="./style.css" />
      <script src="https://public.codepenassets.com/js/prefixfree-1.0.7.min.js"></script>
    </head>
    
    <body>
      <div class="switch-container">
        <div id="boo" class="hide">Boo!</div>
        <label class="switch">
          <input id="toggle" type="checkbox" />
          <div class="slider round">
            <div><div class="ghost"/></div>
          </div>
        </label>
      </div>
      <script src="./script.js"></script>
    </body>
    </html>

    CSS :

    This CSS styles the animated ghost toggle. It gives the page a black background and centers the toggle switch in the middle. The switch has a slider that turns blue when toggled. The white ghost (a circle with a wave-like bottom) appears inside the slider and moves smoothly when toggled. Animations like bobble, sway, and blink make the ghost float, sway side to side, and blink, giving it a cute, ghostly motion effect.

    body {
      width: 100%;
      height: 100%;
      background: black;
    }
    
    #boo {
      color: white;
      margin-bottom: 12px;
    }
    
    #boo.hide {
      opacity: 0;
    }
    
    .switch-container {
      width: 100%;
      height: 100vh;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }
    
    .switch {
      position: relative;
      display: inline-block;
      width: 100px;
      height: 25px;
    }
    
    .switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: grey;
      transition: 0.4s;
    }
    
    input:checked + .slider {
      background-color: #459def;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #459def;
    }
    
    input:checked + .slider > div {
      transform: translateX(65px);
    }
    
    input + .slider > div {
      transition: transform 0.4s;
    }
    
    .slider.round {
      border-radius: 34px;
    }
    
    .ghost {
      position: absolute;
      left: 0;
      top: -10px;
      width: 40px;
      height: 40px;
      background-color: white;
      border-radius: 100px;
      animation: bobble 4.3s infinite;
      filter: drop-shadow(0px 5px 12px rgba(255, 255, 255, 0.6));
    }
    
    @keyframes bobble {
      0% {
        transform: translateY(0);
      }
      50% {
        transform: translateY(15%);
      }
      100% {
        transform: translateY(0);
      }
    }
    @keyframes sway {
      0% {
        transform: translateX(-2px);
      }
      50% {
        transform: translateX(2px);
      }
      100% {
        transform: translateX(-2px);
      }
    }
    @keyframes sway-more {
      0% {
        transform: translateX(-4px);
      }
      50% {
        transform: translateX(4px);
      }
      100% {
        transform: translateX(-4px);
      }
    }
    @keyframes sway-left {
      0% {
        transform: translateX(0px);
      }
      50% {
        transform: translateX(-4px);
      }
      100% {
        transform: translateX(0px);
      }
    }
    @keyframes sway-left-less {
      0% {
        transform: translateX(0px);
      }
      50% {
        transform: translateX(-2px);
      }
      100% {
        transform: translateX(0px);
      }
    }
    @keyframes sway-right {
      0% {
        transform: translateX(0px);
      }
      50% {
        transform: translateX(4px);
      }
      100% {
        transform: translateX(0px);
      }
    }
    @keyframes sway-right-less {
      0% {
        transform: translateX(0px);
      }
      50% {
        transform: translateX(2px);
      }
      100% {
        transform: translateX(0px);
      }
    }
    @keyframes blink {
      0% {
        top: 16px;
        height: 8px;
      }
      39% {
        top: 16px;
        height: 8px;
      }
      40% {
        top: 20px;
        height: 2px;
      }
      50% {
        top: 20px;
        height: 2px;
      }
      51% {
        top: 16px;
        height: 8px;
      }
      100% {
        top: 16px;
        height: 8px;
      }
    }
    .ghost:after {
      content: "";
      position: absolute;
      left: calc(50% - 7px);
      top: 16px;
      width: 10px;
      height: 8px;
      border-right: 3px solid black;
      border-left: 3px solid black;
      animation: bobble 2s infinite, sway-more 2s infinite 1s, blink 4.25s infinite;
    }
    
    /* 
    input:checked + .slider .ghost:after {
      animation: sway-right 0.5s;
    }
    
    
    input:checked + .slider .ghost:before{
      animation: sway-right 0.5s;
    }
    
    input:not(:checked) + .slider .ghost:after {
      animation: sway-left 0.5s;
    }
    
    
    input:not(:checked) + .slider .ghost:before{
      animation: sway-left 0.5s;
    }
     */
    .ghost:before {
      content: "";
      position: absolute;
      top: calc(50% - 6px);
      left: -10%;
      width: 120%;
      height: 12px;
      border-radius: 100px;
      background-color: white;
      animation: sway 2s infinite;
    }

    JavaScript:

    This JavaScript makes the “Boo!” text appear and disappear when you click the toggle switch. It listens for a click on the checkbox (#toggle) and uses a variable isClicked to track the state. When clicked, it removes the hide class (making the text visible) and adds show; clicking again does the opposite – hiding the text again. In short, it toggles the visibility of the “Boo!” text each time you flip the switch.

    document.getElementById("toggle").addEventListener("click", changeColor);
    
    isClicked = true;
    
    function changeColor() {
      if(isClicked){
      document.getElementById("boo").classList.remove("hide");
         document.getElementById("boo").classList.add("show");
        isClicked=false;
      } else{
      document.getElementById("boo").classList.remove("show");
         document.getElementById("boo").classList.add("hide");
        isClicked=true;
      }
    }

    In conclusion, building an Animated Ghost Toggle using HTML, CSS, and JavaScript has been a fun and creative project. By combining structure, styling, and interactivity, we’ve created a charming animation that reacts to user input. Keep experimenting and adding more spooky-cute features to your projects! 👻✨

    If your project has problems, don’t worry. Just click to download the source code and face your coding challenges with excitement. Have fun coding!

    Animation Button JavaScript order button Web Development
    Share. Copy Link Twitter Facebook LinkedIn Email WhatsApp
    Previous ArticleHow to make Magnetic Button Hover Effect using HTML & CSS
    Coding Stella
    • Website

    Related Posts

    JavaScript

    How to make Magnetic Button Hover Effect using HTML & CSS

    6 October 2025
    HTML & CSS

    How to make Animated Electric Card using HTML & CSS

    4 October 2025
    HTML & CSS

    How to make Next Level Electric Button Animation using HTML & CSS

    2 October 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Trending Post

    Master Frontend in 100 Days Ebook

    2 March 202429K Views

    How to make Modern Login Form using HTML & CSS | Glassmorphism

    11 January 202426K Views

    How to make I love you Animation in HTML CSS & JavaScript

    14 February 202421K Views

    How to make Valentine’s Day Card using HTML & CSS

    13 February 202414K Views
    Follow Us
    • Instagram
    • Facebook
    • YouTube
    • Twitter
    ads
    Featured Post

    How to create Netflix Intro Animation using HTML and CSS

    29 June 2025

    How to make Glowing Neon Clock using HTML CSS & JavaScript

    23 June 2024

    How to do Form Validation – Email and Password using HTML CSS & JavaScript

    12 January 2024

    How to make 3D Dot Preloader using HTML & CSS

    16 July 2024
    Latest Post

    How to create Animated Ghost Toggle using HTML CSS and JS

    9 October 2025

    How to make Magnetic Button Hover Effect using HTML & CSS

    6 October 2025

    How to make Animated Electric Card using HTML & CSS

    4 October 2025

    How to make Next Level Electric Button Animation using HTML & CSS

    2 October 2025
    Facebook X (Twitter) Instagram YouTube
    • About Us
    • Privacy Policy
    • Return and Refund Policy
    • Terms and Conditions
    • Contact Us
    • Buy me a coffee
    © 2025 Coding Stella. Made with 💙 by @coding.stella

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Looks like you're using an ad blocker. We rely on advertising to help fund our site.
    Okay! I understood