Thu Jun 23 2022

Transform Hamburger Menu

CSS5469 views

File Name: CSS-Hamburger-menu-button.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Transform Hambuger Menu</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            background-color: #383838;
        }

        section {
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100vh;
        }

        a#menu {
            position: relative;
            cursor: pointer;
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100px;
            height: 100px;
        }

        a#menu span {
            position: absolute;
            width: 100%;
            height: 6px;
            background-color: #fff;
            transition: 0.2s all ease;
        }

        a#menu span:nth-child(1) {
            transform: translateY(-25px);
        }

        a#menu span:nth-child(2) {
            transform: translateY(25px);
        }

        a#menu:hover span:nth-child(1) {
            transform: translateY(-35px);
        }

        a#menu:hover span:nth-child(2) {
            transform: translateY(35px);
        }

        a#menu.active span:nth-child(1) {
            transform: translateY(-50px);
            opacity: 0;
        }

        a#menu.active span:nth-child(2) {
            transform: translateY(50px);
            opacity: 0;
        }

        a#menu span:nth-child(3),
        a#menu span:nth-child(4) {
            transform: rotate(0deg);
        }

        a#menu.active span:nth-child(3) {
            transform: rotate(-45deg);
        }
        a#menu.active span:nth-child(4) {
            transform: rotate(45deg);
        }
        
    </style>

    <script>
        function toggleMenu() {
            let menuBtn = document.getElementById('menu');
            menuBtn.classList.toggle('active');
        }
    </script>
</head>
<body>
    <section>
        <a onclick="toggleMenu()" id="menu">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
        </a>
    </section>
</body>
</html>

Result Screenshot(s)

Transform Hamburger MenuWorking Sample0

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.