This tutorial is out of date and no longer maintained.
Recently I found this awesome dribbble shot by Vitaly Rubtsov, and I could not resist the urge to try to implement it.
In this tutorial, I’ll explain the entire process of how to do it with CSS only, without using a single line of JavaScript. So, we will be seeing some CSS (and SCSS) tricks that will allow us to achieve an animation almost as smooth as the animated gif.
Here’s the CodePen of what we’ll build:
https://codepen.io/lmgonzalves/pen/KaWaJO
Let’s start with the HTML structure we’ll be using. See the comments for a better understanding.
Now let’s add some basic styles to start getting the look and feel we want. The code is pretty straightforward.
Before you start building the rest of the interface, let’s add the toggle functionality to easily change from one state to another.
The HTML we need is already in place. And the style to make it works could be something like this:
To build the closed state, we need to transform the menu items into lines to make up the hamburger menu icon. We can come up with several ways to accomplish this transformation. We have decided to do it this way:
And this is the code to get it working:
Note that here we have posted only the main styles for the nav items, which is the most important part. You can find the full code on GitHub.
To build the open menu we need to restore the nav items from lines to normal text links, among other minor changes. Let’s see how to do it:
If we look closer at the gif, we can see that all the menu items do not move at the same time, but in a staggered way. We can do that in CSS too! Basically, we need to select each element (using :nth-child
) and set a progressively increased transition-delay
. But that is certainly repetitive work. And, what if we have more items? Don’t worry, we can do it better with a bit of SCSS magic:
Here we are using a loop, a variable interpolation, and some basic arithmetic operations. You can learn more about these and more awesome features in the Sass documentation site.
Please note that with this code we’ll get the desired staggered behavior for the close animation. We need to calculate the $delay
slightly different for the open animation, to get an inversely staggered transition. Just like this:
And we are done with our fancy menu! We have also included some dummy elements, like in the animated gif, and you can see the final demo here.
So far, we have built a cool and functional menu just with CSS. However, if you are not in favor of using the CSS toggle system, it can be perfectly replaced by a few lines of JavaScript without too much effort.
You can find the full code on the GitHub repository, and also play with it directly on CodePen.
We hope this tutorial has been to your liking and you have found it useful!
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!