search
HomeWeb Front-endHTML Tutorial6 CSS hamburger animations_html/css_WEB-ITnose

I used CSS and a little JavaScript to demonstrate how to generate a "hamburger" animation.

In a previous article, I posted my inspiration for the animation of the navigation bar menu (also known as the "hamburger icon"). As a result, I received many compliments from fans. I would like to thank you in advance.

I decided to work more on this, so I wrote a series of different animation implementations. In this article, you will see enhanced versions of 6 different "hamburger" animations.

I used a little JavaScript to trigger the animation. Before continuing to study, you can take a look at the Demo (Translator: If it cannot be opened, please click on the Demo at the bottom of the article)

Changes

The code I wrote before was not very neat. The main purpose of the changes is to make the code more tidy and orderly.

First, I replaced the meaningless div with a button element. This makes the code more readable.

Then I named the wrapper class name .hamburger. Inside .hamburger, I have another element (a span tag), and I positioned its class name as .icon

Essence

Our .icon element is wrapped to form a complete icon.

In order to make our animation smoother, the middle bar needs to be flexible enough, so the .icon element is used to act as the patty part of the hamburger.

As for the icon wrapping part, .hamburger not only acts as a wrapper, it also assists in completing the entire animation. Therefore, the upper and lower parts of the hamburger are filled by the .hamburger pseudo-class.

HTML

<button class="hamburger hamburger-cancel">  <span class="icon"></span></button>

As you can see, the button element plays the .hamburger role. Another class name (.hamburger-cancel) is used to distinguish what kind of burger it is (pan-fried chicken drumstick or Big Mac or something). We have 6 different flavors of burgers here.

JavaScript

As I said at the beginning, we use a small piece of JS code to switch, the following is my code snippet

var el = document.querySelectorAll('.hamburger');for(i=0; i<=el.length; i++) {  el[i].addEventListener('click', function() {    this.classList.toggle('active');  }, false);}

The above JavaScript is mainly By binding the click time, add the .active class to the corresponding element.

Code

The following CSS code sets the default state of our hamburger element. You can adjust the font-size appropriately. Of course, the maximum font-size cannot exceed our icon

In addition to structural specifications, we also use the CSS transition property to make the animation smoother.

.hamburger { font-size: 60px; display: inline-block; width: 1em; height: 1em; padding: 0; cursor: pointer; transition: transform .2s ease-in-out; vertical-align: middle; border: 0 none; background: transparent; }/** * Button height fix for Firefox */.hamburger::-moz-focus-inner { padding: 0; border: 0 none; }/** * Focus fix for Chrome */.hamburger:focus { outline: 0; }

Now, it’s time to draw our burger icon. The following definition can give us a clearer understanding of the entire process.

  • Top bun: hamburger:before
  • Middle Orleans chicken leg: .icon
  • Bottom bun: .hamburger:after
  • Obviously, all three of the icons The bars all have some common characteristics that we can extract.

    .hamburger:before,.hamburger:after { content: ""; }.hamburger:before,.hamburger .icon,.hamburger:after { display: block; width: 100%; height: .2em; margin: 0 0 .2em; transition: transform .2s ease-in-out; border-radius: .05em; background: #596c7d; }/** * Styles for the active `.hamburger` icon */.hamburger.active:before,.hamburger.active .icon,.hamburger.active:after { background: #2c3e50; }

    In the above code, we drew three bars in .hamburger. If we give it a little gap, we can clearly see the hamburger icon.

    In order to look sleeker, we set the border-radius attribute. Since our elements have no text, we set a background color.

    As expected, we have drawn a Shandong pancake. Let's move on to the next animation part.

    Animation

    6 different flavors of burgers, let’s analyze them one by one.

    Vertical

    In the simplest way, to rotate the burger, we only need to rotate it 90 degrees in the .active state, or 270 degrees, which will look cooler. a little.

    /** * VERTICAL HAMBURGER */.hamburger.hamburger-vertical.active { transform: rotate(270deg); }

    Rotate the bread and an 'X' shape will appear.

    Of course, just spinning is not enough. While rotating, you also need to make changes on the Y axis, otherwise the distance will be a bit far.

    Plus sign
    /** * CLOSE/CANCEL/CROSS */.hamburger.hamburger-cancel.active .icon { transform: scale(0); }.hamburger.hamburger-cancel.active:before { transform: translateY(.4em) rotate(135deg); }.hamburger.hamburger-cancel.active:after { transform: translateY(-.4em) rotate(-135deg); }

    This animation is like this:

    - The patty in the middle disappears

    - The upper bun moves and rotates to act as a vertical bar
    - The bun at the bottom moves and rotates to act as a horizontal line

    These 3 actions can complete the animation from hamburger to plus

    Minus sign
    /** * PLUS */.hamburger.hamburger-plus.active .icon { transform: scale(0); }.hamburger.hamburger-plus.active:before { transform: translateY(.4em) rotate(90deg); }.hamburger.hamburger-plus.active:after { transform: translateY(-.4em) rotate(180deg); }

    Minus sign The change is like this, the middle layer still disappears, the other two pieces move up and down, and finally merge into one piece. Then let them rotate 180 degrees in the same way, which will be very cool~

    Left arrow
    /** * MINUS/DASH */.hamburger.hamburger-minus.active { transform: rotate(180deg); }.hamburger.hamburger-minus.active .icon { transform: scale(0); }.hamburger.hamburger-minus.active:before { transform: translateY(.4em); }.hamburger.hamburger-minus.active:after { transform: translateY(-.4em); }

    This includes moving, rotating and adjusting the upper and lower pieces of bread. Finally, add a 180-degree rotation

    right arrow
    /** * LEFT ARROW */.hamburger.hamburger-arrow-left.active { transform: rotate(180deg); }.hamburger.hamburger-arrow-left.active:before { width: .6em; transform: translateX(.4em) translateY(.2em) rotate(45deg); }.hamburger.hamburger-arrow-left.active .icon { border-radius: .1em .25em .25em .1em; }.hamburger.hamburger-arrow-left.active:after { width: .6em; transform: translateX(.4em) translateY(-.2em) rotate(-45deg); }

    This is the mirror copy of the left arrow. You can copy the code of the left arrow and make appropriate changes.

    Summary
    /** * RIGHT ARROW */.hamburger.hamburger-arrow-right.active { transform: rotate(180deg); }.hamburger.hamburger-arrow-right.active:before { width: .6em; transform: translateX(0) translateY(.2em) rotate(-45deg); }.hamburger.hamburger-arrow-right.active .icon { border-radius: .25em .1em .1em .25em; }.hamburger.hamburger-arrow-right.active:after { width: .6em; transform: translateX(0) translateY(-.2em) rotate(45deg); }

    I looked for similar implementations online and I found Sara’s Navicon transformicons to be cool, robust and more appealing.

    I hope you enjoyed this article and feel free to share your thoughts and suggestions below. Thanks for reading.

    http://helkyle.com/demos/hamburger-icons.html’ target=’__blank’>Click to view Demo

    This article is based on the translation by @Rahul Arora. The entire translation contains my own understanding and meaning. If there is anything that is not translated well or is wrong, please give me some advice. If you want to reprint this translation, please indicate the English source: http://w3bits.com/animated-hamburger-icons/

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

    The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

    The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

    The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

    HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

    The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

    The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

    The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

    HTML vs. CSS vs. JavaScript: A Comparative OverviewHTML vs. CSS vs. JavaScript: A Comparative OverviewApr 16, 2025 am 12:04 AM

    The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

    HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

    HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

    HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

    HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

    From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

    HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor