Dino Geek, try to help you

How to animate a hamburger menu icon in CSS?


Here is a simple example of how to animate a hamburger menu icon in CSS using the :before and :after pseudo-elements to create the lines of the hamburger. This animation will transform the hamburger icon into a cross when clicked.

Here’s the related HTML:

```

```

And the CSS:

```
body { display: -ms-flexbox; display: flex; -ms-flex-align: center; justify: content: center; height: 100vh;
}

.hamburger-menu { display: -ms-flexbox; display: flex; -ms-flex-align: center; justify-content: center; width: 50px; height: 50px; cursor: pointer; transform: rotate(0deg); transition: .5s ease-in-out;
}

.hamburger-menu .bar { width: 35px; height: 5px; background-color: #333; border-radius: 9px; opacity: 1; position: relative; transform: rotate(0deg); transition: .25s ease-in-out;
}

.hamburger-menu .bar:before, .hamburger-menu .bar:after { content: ‘’; position: absolute; width: 35px; height: 5px; background-color: #333; border-radius: 9px; opacity: 1; transition: .25s ease-in-out;
}

.hamburger-menu .bar:before { transform: translateY(-10px);
}

.hamburger-menu .bar:after { transform: translateY(10px);
}

/* Animation effect */
.hamburger-menu.active .bar { transform: rotate(45deg);
}

.hamburger-menu.active .bar:before { transform: rotate(45deg);
}

.hamburger-menu.active .bar:after { transform: rotate(-45deg);
}
```

And a bit of JavaScript to handle the click event:

```
document.querySelector(‘.hamburger-menu’).addEventListener(‘click’, function() { this.classList.toggle(‘active’);
});
```

The animation effect is created by rotating the lines of the hamburger icon when the button is clicked. The top and bottom lines are rotated by 45 degrees in opposite directions to create an X shape, and the middle line is hidden.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use