One of the most common design components in web apps—and even full web pages—is the ☰ icon. These three horizontal, parallel lines are sometimes called the tribar, triple bar, pancake, or double Oreo, but these days the most common name, by far, is hamburger. The purpose of the hamburger icon is to toggle a menu of commands or options when clicked, so the hamburger is an efficient (although some critics would substitute the word “obscure”) use of space, particularly when either screen real estate is limited or the menu you want to display is large.
You can add the ☰ icon by using the HTML code ☰. However, if you want to manipulate the lines (for example, to animate them when clicked, as I show below), then you need create the individual lines using HTML and CSS. Let’s see how it’s done:
In the HTML Editor, we have a div
element with class="hamburger"
, and its children are three div
elements with class="hamburger-line"
. Surprisingly, we need just a few lines of CSS to turn this into a hamburger menu:
.hamburger
{…
<div
class="hamburger"> tag.
cursor: pointer;
width: 25px;
.hamburger-line
{…
<div
class="hamburger-line"> tag.
background-color: black;
height: 4px;
margin-top: 4px;
To add some “wow” factor to your interface, you can animate the hamburger lines when the user clicks the icon. My favorite effect here is to animate the lines so that they form an “X” (the more-or-less universal symbol for “Close”) when first clicked, and then animate the lines back to normal when clicked a second time. Check it out:
The JavaScript tab includes an event handler that watches for clicks on the .hamburger
element, and that handler toggles the active
class on the .hamburger-line
elements. Here’s what’s new in the CSS tab:
transition: opacity .5s ease-in, transform .5s ease-out;
.hamburger-line
class.
.hamburger-line:first-child.active {
transform: rotate(45deg) translate(6px, 6px);
}
active
class is applied. The line gets rotated 45 degrees clockwise and shifted right and down by 6px.
.hamburger-line:nth-child(2).active {
opacity: 0;
}
opacity
to 0 when the active
class is applied.
.hamburger-line:last-child.active {
transform: rotate(-45deg) translate(5px, -5.5px);
}
active
class is applied. The line gets rotated 45 degrees counter-clockwise and shifted right by 5px and up by 5.5px.