How to Add Hamburger Menu in Html Using Javascript and Bulma CSS in Mobile View

In this tutorial, you'll learn to add hamburger menus to mobile views in your html using simple javascript and bulma css in less than 3 minutes.

Share:

May 1, 2023 516 Words

Read Time: 3 Minutes

A screenshot of hamburger menu in mobile view

Introduction

If you’re building a website or web application, adding a hamburger menu to your navbar can make your website look more modern and professional.

In this post, we’ll explore how to build a hamburger menu using Bulma CSS.1

Before we dive into the code, let’s take a moment to understand what a hamburger menu is.

What is a hamburger menu?

A hamburger menu is a button that, when clicked, opens a menu with additional navigation options. The button itself typically consists of three horizontal lines that resemble a hamburger, hence the name.

A screenshot of hamburger menu in mobile view
A screenshot of hamburger menu in mobile view
A screenshot of active hamburger menu in mobile view
A screenshot of active hamburger menu in mobile view

How to add an interactive hamburger menu?

To get started, we’ll need to include the Bulma CSS framework in our project.

You can do this by either downloading the CSS file and linking to it in your HTML file or by using a CDN. For this example, we’ll use the CDN method.

<link rel="stylesheet" href="<https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css>">

After including the CSS, we can start building our navbar.

<nav class="navbar" role="navigation" aria-label="main navigation">
  <div class="navbar-brand">
    <a role="button" class="navbar-burger" data-target="navMenu" aria-label="menu" aria-expanded="false">
      <span aria-hidden="true"></span>
      <span aria-hidden="true"></span>
      <span aria-hidden="true"></span>
    </a>
  </div>
  <div class="navbar-menu" id="navMenu">
    <div class="navbar-item has-dropdown is-hoverable">
      <a class="navbar-item" href="#">Home</a>
      <a class="navbar-item" href="#">About</a>
      <a class="navbar-item" href="#">Contact</a>
    </div>
  </div>
</nav>

In the above code, we first create a nav element with the class navbar. Inside this element, we create a div element with the class navbar-brand. This element will contain our hamburger button.

We create an anchor tag with the class navbar-burger inside the navbar-brand element. We also set the data-target attribute to "navMenu". This is the ID of the div element that contains our menu items.

Next, we create a div element with the class navbar-menu. Inside this element, we create a div element with the class navbar-start. This is where we’ll put our menu items.

Adding Javascript

To make the hamburger menu work, we need to add some Javascript. We’ll do this by adding the following script to our HTML file.

<script>
    document.addEventListener('DOMContentLoaded', () => {
      // Get all "navbar-burger" elements
      const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
      // Add a click event on each of them
      $navbarBurgers.forEach( el => {
        el.addEventListener('click', () => {
          // Get the target from the "data-target" attribute
          const target = el.dataset.target;
          const $target = document.getElementById(target);
          // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
          el.classList.toggle('is-active');
          $target.classList.toggle('is-active');
        });
      });
    });
</script>

This script gets all elements with the class navbar-burger and adds a click event listener to each of them. When the button is clicked, the script gets the ID of the menu from the data-target attribute and toggles the class is-active on both the button and the menu.

Conclusion

And that’s it! With just a few lines of HTML and Javascript, we were able to create a basic yet functional hamburger menu using Bulma CSS.

If you’re looking to add some additional features, such as animations or dropdown menus, be sure to check out the official documentation .2

👋 — @TnvMadhav

Reference

Find more posts from following topics

1
12
13
14
15
16
17
18
19
2
20
2024
21
22
23
24
26
27
28
29
3
30
4
5
6
8
9
@binding
abbacchio
accurate-requests
anime
api-development
api-testing
api-testing-tools
array
automated-testing
bad-habits
base64-decoder
base64-encoder
biography
blog
blogging
books
browser
bulma-css
bulma.io
bulmacss
button-swiftui
chatgpt
chrome
clipboard
code
code-block
code-navigation
code-snippet
codecoverage
comparison
compile
computer-networks
configuring-debugger-for-django-in-vs-code
configuring-launch.json-for-python-debugger
copy
copy-to-clipboard
copy-to-clipboard-neovim
css
current-date
current-time
current-timestamp
database
debugger-setup-in-visual-studio-code
debugging-django-app-in-visual-studio-code
debugging-python-code-in-visual-studio-code
debugging-python-programs-with-visual-studio-code
debugging-python-with-virtual-environment-in-vs-code
december
developer-productivity
developers
development-workflow
django
django-rest-framework
dom
dynamic-sitemap-in-nextjs
encryption
engineering-dashboard
enumerate
experience
fiction
flowcharts
generics
git
git-diff
github
global-keyboard-shorcut
global-shortcut
go-hugo
go-programming
go-test
go-to-line
go-tool
go1.18
golang
golang-development
golden-wind
good-habits
gorilla-websocket
gpt
gpt-3.5
gpt-4
gpt-4-api
guide
gumroad
habits
habits-tracker-notion-template
hamburger-menu
hotkeys
html
hugo
ide
image
image-sharing
image-tool-for-ios
imagerenderer
include-timestamp
integrated-development-environment
ios
ios-16
ios16
iphone
iphone-13
iphone-13-pro
iphone-13-pro-max
javascript
jojos-bizzarre-adventure
jump-to-definition
keyboard-shortcut
leonardo-da-vinci
lessons
linux
logging
ls
lsp
macos
map
markdown
markdown-code
mental-programming
menu
menubarextra
mergesort
mermaid-syntax
mistake-tracker-notion
mobile-view
modifier
modulo-operation
navbar
navigationlink
navigationstack
neovim
nested-functions
next.js
nextjs
nextjs-markdown
nextjs-sitemap
nextjs-sitemaps
nice-shot
nice-shot-pro
notion
notion-api
notion-api-python
notion-budget
notion-budget-template
notion-budget-tracker
notion-bug-report-tracker
notion-dashboard
notion-expense-manager
notion-habits
notion-habits-dashboard
notion-habits-template
notion-habits-tracker
notion-habits-tracker-template
notion-issue-tracker
notion-mistake-tracker
notion-product
notion-product-dashboard
notion-product-roadmap
notion-product-roadmap-dashboard
notion-tasks
notion-tasks-dashboard
notion-tasks-template
notion-tasks-tracker
notion-template
notionworkspaces
november
october
openai
osx
pagination
personal-ifttt-framework
photospicker
photospickeritem
phpickerfilter
postgres
postman-capabilities
postman-request
pre-request-script
product-roadmap-notion-template
product-roadmap-template
productivity
programming
python
python-api
python-debugger-tutorial-for-vs-code
python-debugging-mode-in-vs-code
python-notion-api
python3
reading
real-time-communication
rehype
remark
request-data
running-debugger-in-visual-studio-code
running-django-app-in-debugging-mode
running-program-in-debugging-mode-in-vs-code
running-python-code-in-debugging-mode
safari
screenshot-app-for-ios
screenshot-app-ios
screenshot-ios
screenshot-tool-for-ios
september
set-current-timestamp
setting-up-debugger-in-vs-code-for-python
share-extension
sharelink
sharepreview
sharesheet
simple-websocket-server
sitemap
slice
slices
slider
sort
sorting
space-complexity
sql
ssh
step-by-step-guide
stocks-profits-tracker
stocks-profits-tracker-template
stocks-tracker
struct
sustained-vigilance
swift
swiftui
swiftui-button
swiftui-button-action
swiftui-button-style
table-of-contents
tasks-tracker-notion-template
tcp
test
testing
textfield-swiftui
til
tim-sort
time-complexity
timeliness
timestamp-integration
timsort
transferable
triggers-and-actions
tutorial
unittest
unix
us-stocks
usa-stocks
useful-ios-features
using-breakpoints-in-python-debugger
using-virtual-environment-with-python-debugger
vanilla-javascript
variable
vim
visual-mode
visual-studio-code
vs-code
vscode
vscode-go-to-line
walter-isaacson
web-sockets-in-go
websocket-client
websocket-programming
websocket-server
white-screen-of-death
wsod
xcode
xss