Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create an Animated Hamburger using HTML5 and CSS3

    htmlHTML 5

    We will create an interesting animated hamburger that can be used in websites. In this process, we will look at transitions and transformations in CSS3.

    Further Links:

    1. https://codepen.io/tag/hamburger%20menu/
    2. https://codepen.io/designcouch/pen/Atyop


    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Let's start with an empty HTML template. I'll name this animated hamburger. We'll make a base container, and give it an ID of ham. We'll add three divisions, and give them a class of line, which represents the three lines of our hamburger.

    Now, let's start styling our hamburger. Let's add a few styles to our page. We'll add a style for the container and for each line. You can see that we have our three lines displayed in the page. The CSS for the container is straightforward.

    For each line, we've added a width of 40 pixel and a height of 5 pixel, and gave it a background of black, and a border radius of 2 pixel to make it slightly rounded. We've also given it a margin of 6 pixel to get the gap between each line.

    We'll add some interactivity to the hamburger on hover. Now, we'll take the hover event on the container using the CSS solo class, hover, and change the CSS property of each eye line.

    We'll change the background color to be teal, and then we'll add a transition property so that the change isn't abrupt, and it will happen over a period of time. We are using 04 seconds for the time, and easing out for the smoothing function.

    Now, if we refresh our page and hover on the hamburger, you can see that the background color seamlessly transitions to teal.

    We'll add a script to animate the hamburger on click. First, we'll get the container by referencing the ID ham. Then we'll hook into the onClick function of the division. Now, we'll toggle a class called clicked on our hamburger for each click. Now, we can animate each line whenever the container has the class clicked attached to it.

    We'll change the width of each line to be 32 pixel. If we go ahead and refresh the browser and click on the hamburger, you can notice that that width has changed. Click on it again, the width will divert back.

    Let's make it a bit more interesting by animating each line separately. We've added three classes -- line one, line two, line three -- for each line. Let's make a copy of this CSS for each line. We have changed the class names and our widths to 18 pixel, 24 pixel.

    Now, if we refresh our browser and test this, you can see that the animation looks much more smoother and nice.