THE WORLD'S LARGEST WEB DEVELOPER SITE
×

HOW TO

HowTo Home

Menus

Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Top Navigation Responsive Topnav Side Navigation Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Fixed Menu Slide Down Bar on Scroll Sticky Navbar Hover Dropdowns Click Dropdowns Dropdown in Navbar Mega Menu Pagination Breadcrumbs Button Group Vertical Button Group

Images

Slideshow Modal Images Lightbox Responsive Image Grid Image Grid Image Overlay Image Effects Image Text Full Page Image Hero Image Rounded Images Meet the Team Flip an Image Shake an Image Portfolio Gallery Portfolio with Filtering

Buttons

Alert Buttons Animated Buttons Social Media Buttons Loading Buttons Next/prev Buttons Round Buttons Scroll To Top Button

Forms

Login Form Signup Form Contact Form Responsive Form Clear Input Field Copy Text to Clipboard Animated Search Custom Checkbox/Radio Toggle Switch Password Validation Toggle Password Visibility Multiple Step Form

Filters

Filter List Filter Table Filter Elements Sort List Sort Table

More

Modal Boxes Timeline Progress Bars Skill Bar Range Sliders Tooltips Popups Calendar Responsive Tables HTML Includes To Do List Loaders Star Rating User Rating Overlay Effect Contact Chips Cards Profile Card Alerts Notes Labels Circles Coupon Fixed Footer Equal Height Clearfix Snackbar Scroll Drawing Pricing Table Parallax Aspect Ratio Build a Web Site Toggle Like/Dislike Toggle Hide/Show Toggle Class Add Class Remove Class Arrows Shapes Vertical Line Animate Icons Countdown Timer Typewriter Coming Soon Page Center Website Thumbnails Chat Messages Testimonials Draggable HTML Element Syntax Highlighter JS Animations

Grid

2 Column Layout 3 Column Layout 4 Column Layout Mixed Column Layout

Google

Google Maps Google Translate Google Charts Google Fonts

Converters

Convert Weight Convert Temperature Convert Length Convert Speed

How TO - Menu Icon


Learn how to create a menu icon with CSS.


How To Create a Menu Icon

If you are not using an icon library, you can create a basic menu icon with CSS:

Menu Icon:

Animated Menu Icon (click on it):


Step 1) Add HTML:

Example

<div></div>
<div></div>
<div></div>
Step 2) Add CSS:

Example

div {
    width: 35px;
    height: 5px;
    background-color: black;
    margin: 6px 0;
}
Try it Yourself »

Example Explained

The width and the height property specifies the width and height of each bar.

We have added a black background-color, and the top and bottom margin is used to create some distance between each bar.


Animated Icon

Use CSS and JavaScript to change the menu icon to a "cancel/remove" icon when it is clicked on:

Step 1) Add HTML:

Example

<div class="container" onclick="myFunction(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>
Step 2) Add CSS:

Example

.container {
    display: inline-block;
    cursor: pointer;
}

.bar1, .bar2, .bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
}

/* Rotate first bar */
.change .bar1 {
    -webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
    transform: rotate(-45deg) translate(-9px, 6px) ;
}

/* Fade out the second bar */
.change .bar2 {
    opacity: 0;
}

/* Rotate last bar */
.change .bar3 {
    -webkit-transform: rotate(45deg) translate(-8px, -8px) ;
    transform: rotate(45deg) translate(-8px, -8px) ;
}
Step 3) Add JavaScript:

Example

function myFunction(x) {
    x.classList.toggle("change");
}
Try it Yourself »

Example Explained

HTML & CSS: We use the same styles as before, only this time, we wrap a container element around each <div> element and we specify a class name for each.

The container element is used to show a pointer symbol when the user moves the mouse over the divs (bars). When it is clicked on, it will execute a JavaScript function that adds a new class name to it, which will change the styles of each horizontal bar: the first and the last bar is transformed and rotated to the letter "x". The bar in the middle fades out and becomes invisible.