ASPCode.net logo
  • Home 
  • Blogs 
  • Tags 
Blog
  1. Home
  2. Articles
  3. CSS Only - Hamburger Menu

CSS Only - Hamburger Menu

Posted on November 23, 2024  (Last modified on May 26, 2025) • 3 min read • 488 words
Share via
ASPCode.net
Link copied to clipboard

Video is in Swedish

On this page
  • Creating a CSS-Only Hamburger Menu
  • HTML Structure
  • CSS Magic
  • The Magic Happens
  • The Final Result
  • Video
  • Sourcecode

Creating a CSS-Only Hamburger Menu  

In recent years, hamburger menus have become a popular design element in web development. These menus are characterized by a three-line icon that, when clicked, reveals a list of navigation options. Traditionally, hamburger menus require JavaScript to function properly. However, with the advancements in CSS, it is now possible to create a hamburger menu using only CSS.

In this article, we will explore how to create a CSS-only hamburger menu using the #2024 design concept as our inspiration.

HTML Structure  

Before we dive into the CSS code, let’s first set up the HTML structure for our hamburger menu. We’ll use a simple unordered list (<ul>) with a few <li> elements inside:

<nav>
  <button class="hamburger">
    <span></span>
    <span></span>
    <span></span>
  </button>
  <ul class="menu">
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

CSS Magic  

Now, let’s add some CSS magic to our HTML structure. We’ll start by styling the hamburger button:

.hamburger {
  position: relative;
  display: inline-block;
  cursor: pointer;
}

.hamburger span {
  background-color: #2024; /* Our design concept color */
  display: block;
  width: 20px;
  height: 2px;
  margin-bottom: 5px;
  transition: transform 0.3s ease-in-out;
}

In the above code, we’re using the position property to position our hamburger button relative to its parent element. We’re also setting the cursor property to a pointer cursor to indicate that the button is clickable.

Next, we’ll style the menu:

.menu {
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #2024; /* Our design concept color */
  padding: 10px;
  border-radius: 5px;
  display: none;
}

.menu li {
  list-style: none;
  margin-bottom: 10px;
}

.menu a {
  text-decoration: none;
  color: #fff;
}

In the above code, we’re positioning our menu absolutely and setting its background color to match our design concept. We’re also adding some basic styling to our menu items.

The Magic Happens  

Now that we have our HTML and CSS in place, let’s add some JavaScript (or rather, no JavaScript!) to make our hamburger menu work. We’ll use the :target pseudo-class to toggle the visibility of our menu:

.hamburger[aria-expanded="true"] ~ .menu {
  display: block;
}

.hamburger[aria-expanded="false"] ~ .menu {
  display: none;
}

In the above code, we’re using the aria-expanded attribute to track the state of our hamburger button. When the button is clicked, the aria-expanded attribute changes from false to true, and vice versa.

The Final Result  

With our CSS-only hamburger menu in place, let’s take a look at the final result:

[Insert screenshot or demo link]

As you can see, our hamburger menu is fully functional without requiring any JavaScript. The menu icon toggles between its open and closed states when clicked, revealing a list of navigation options.

In conclusion, creating a CSS-only hamburger menu using the #2024 design concept is a great way to add some flair to your website’s navigation system. With this technique, you can create a responsive and accessible menu that works seamlessly across different devices and browsers.

Video  

Swedish

Sourcecode  

Sourcecode
 
 Javascript Form Validering med Pristine
MediaQueries Hamburger del 1 
On this page:
  • Creating a CSS-Only Hamburger Menu
  • HTML Structure
  • CSS Magic
  • The Magic Happens
  • The Final Result
  • Video
  • Sourcecode
Follow me

I code in Java, C#, Golang, SQL and more

     
© 2024 Systementor AB
ASPCode.net
Code copied to clipboard