How to Create an Animated Menu Icon with HTML, CSS & JavaScript

 


An animated menu icon, often referred to as a "hamburger menu," is a popular design feature in modern web development. It provides a sleek and interactive way to toggle menus on websites, especially in responsive designs. Here's a step-by-step guide to creating one using HTML, CSS, and JavaScript.

Key Features of the Animated Menu Icon

  1. HTML Structure: The base structure consists of a container and three bars representing the icon.
  2. CSS Styling: Styles ensure the bars look uniform and position them correctly. Animations are added to create the interactive effect.
  3. JavaScript Functionality: Adds logic to toggle classes and activate animations.

Steps to Create the Animated Menu Icon

  1. HTML

    • Define a container for the menu icon.
    • Include three <div> elements for the bars.
index.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animated Icon in HTML CSS</title>
   <link rel="stylesheet" href="style.css">
</head>

<body>

    <div class="conatiner" onclick="menuIconToggle(this)">
        <div class="bar1"></div>
        <div class="bar2"></div>
        <div class="bar3"></div>
    </div>

<script src="main.js"></script>
</body>

</html>

style.css

.conatiner{
    display: inline-block;
    cursor: pointer;
}
.bar1, .bar2, .bar3{
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
}
.change .bar1{
    transform: translate(0, 11px) rotate(-45deg);
}
.change .bar2{
    opacity: 0;
}
.change .bar3{
    transform: translate(0, -11px) rotate(45deg);
}

main.js

function menuIconToggle(x){
    x.classList.toggle("change")
}


Video Tutorial

For a complete walkthrough, check out the video on CodeLys Learning:
How to Create an Animated Menu Icon with HTML, CSS & JavaScript

This video demonstrates the process in detail, with clear examples and tips for customization.





Comments

Popular posts from this blog

How to Create Amazing HTML, CSS, and JavaScript Projects | CodeLys Learning

Learn CSS Box Model In 3 Minutes

Beginner project CSS radio buttons