Incorporating a hamburger symbol into an established menu for easy navigation

I have come across a tutorial on creating a navigation menu with a logo positioned to the left. However, I now wish to incorporate a hamburger icon for mobile devices and I am unsure about the process. Despite my efforts to find a suitable tutorial online that caters to my specific requirements, I failed in my attempt. I did try using a tutorial for implementing a hamburger icon but faced challenges and had to start over.

<div class="container">
    <div class="logo">
        <img src="images/logo-large.png" alt="Nathan Ashbury Logo" class="image-1">

    </div>  
<nav>
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Coding</a></li>
        <li><a href="#">Photography</a></li>
        <li><a href="#">Contact Us</a></li>
    </ul>
</nav>  
</div>

Answer №1

Creating a Hamburger Menu

<html>
     <head>
    <link rel="stylesheet" type="text/css" href="css/style.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
 </head>

<body>
    <header>
        <h1>Example: Building a hamburger menu</h1>
        <div class="top">
            <a href="#" class="menu_icon"><i class="material-icons">dehaze</i></a>
        </div>
    </header>
    <nav class="menu">
        <a href="#" class="item_menu">home</a>
        <a href="#" class="item_menu">about</a>
        <a href="#" class="item_menu">products</a>
        <a href="#" class="item_menu">services</a>
        <a href="#" class="item_menu">contact</a>
    </nav>
    <main>
        Content of the website.
    </main>

    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script type="text/javascript" src="js/script.js"></script>
</body>

CSS Styles


* { margin: 0 auto; font-family: sans-serif; }

body { margin: 0 auto; }

header {
    height: 70px;
    background-color: #3e739d;
    border-bottom: 1px solid #494949;
    display: flex;
    align-items: center;
    justify-content: center;
}
header > h1 {
    width: calc(100% - 160px);
    text-align: center;
    font-size: 20px;
    color: white;
}
header > .top {
    position: absolute;
    left: 20px;
}
header > .top a.menu_icon i {
    color: #000;
    font-size: 40px;
    padding-top: 5px;
    transition: .2s ease;
}
header > .top a.menu_icon:hover i {
    color: #fff;
}
nav.menu {
    width: 300px;
    min-height: calc(100vh - 121px);
    background-color: #03a9f4;
    position: absolute;
    left: -300px;
    transition: .3s all;
}
nav.menu > a {
    display: block;
    padding: 5px;
    margin: 15px 0 0px 20px;
    color: #494949;
    text-transform: uppercase;
}
main {
    width: 100%;
    padding: 30px;
    box-sizing: border-box;
}
footer {
    height: 50px;
    background-color: #494949;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    bottom: 0;
    position: fixed;
    width: 100%;
}

.menu_show {
    left: 0!important;
}

@media screen and (max-width: 425px) {
    header h1 {
        font-size: 16px;
    }
}
@media screen and (max-width: 360px) {
    nav.menu {
        width: 100%;
        left: -100%; 
    }
}

To enable the menu, showing or hiding the navigation list when the button is clicked, jQuery library is used. Here is the click event of the button with the top class and the toggleClass function of jQuery, adding or removing the menu_show class from the menu to make it visible or invisible.

$(document).ready(function() {
    $("body").on('click', '.top', function() {
        $("nav.menu").toggleClass("menu_show");
    });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am looking to create a split div with a diagonal line running through

Is there a way I can create this Mockup using html5 and css3, but also add diagonal lines to divide the div? Any suggestions on how to achieve this? ...

Obtain the content from a dynamically generated dropdown menu and incorporate it into a separate file

My website features two dropdown menus - one for selecting countries and another for cities. The country menu is populated with data from a database, and once a country is selected, the city dropdown is dynamically filled with corresponding cities using a ...

What could be causing my display: table-cell element to not take up the entire available space?

After consideration, I have decided to avoid using a JavaScript solution for my problem. The only way it seems to work consistently is by using setInterval() at regular intervals, which I prefer not to do. I am aware that achieving the same effect with CSS ...

Modifying a Flash Video through JavaScript

Similar Question: How can I change the source of a flash object using jQuery? UPDATE I have resolved the initial loading issue, so I removed that part from the code. I've created a flash player like this: <div id="video_content"><div i ...

One Functionality on Button is Failing to Execute among Several Tasks

I've encountered an issue with one of the tasks triggered by a button click. The button successfully executes two out of three tasks (hides them on click), except for the last task which involves a text-blinking JavaScript function. I've used the ...

Converting units to rem dynamically in CSS: a comprehensive guide

Hey there, I'm currently trying to dynamically convert units into rem in CSS and facing some issues. I have set the root font-size as 23px The current font-size is 16px The expected result should be 16 / 23 => 0.695rem Question: I am looking for ...

Column-oriented and Slim-fit packaging

I have been working on designing a layout with a fixed height that will display multiple columns of specified size to accommodate flowing text. While I have successfully implemented this, I am facing an issue where the enclosing div does not adjust its siz ...

Preventing data loss upon submitting a form (PHP)

Currently, I have an HTML file that allows users to select appointment times for specific days of the week. Once they click submit, a PHP file generates and displays a calendar with the chosen times as checkboxes. My goal is to enable users to input their ...

What is the best way to retrieve a variable in AngularJS1 after the HTML has been divided into multiple child HTML files?

I have segmented my main HTML page into multiple subpages and included them in the main file. However, it seems that each subpage is referencing different '$scope' variables. I am trying to reference ng-modle="My-model" from one subpage to anothe ...

What are the best ways to enhance change detection efficiency in Angular?

One issue I am facing involves two components and a service. It appears that when moving from the view of a routed component to elements in different components like a matMenu and an input field, the routed component seems to refresh itself. This becomes p ...

Automate populating input fields with data

I need help with a form that has four input boxes. Is it possible to automatically fill the remaining three input boxes with the value entered in the first box when the user clicks a button using JavaScript? Or should I aim to prefill the textboxes with ...

The candy stripes on a loading bar zebra assist in creating a unique and

I'm in the process of creating my portfolio and I'd like to incorporate unique animated loading bars, such as vertical or horizontal candy stripes, to showcase my coding skills. Can anyone suggest a specific programming language or tool that woul ...

What are some ways to design scrollbars with a Google Wave-like style?

Is there a way to design scrollbars similar to the ones found in Google Wave? They seem to save space and have an appealing look. I am interested in implementing these customized scrollbars on a div element, just like how Google Wave utilizes them. (im ...

Error: The property 'length' cannot be read from an undefined parent causing Uncaught TypeError

Hey there, take a look at this cool stuff http://jsfiddle.net/J9Tza/ <form class="validation"> <div> <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" r ...

Challenges of Using Flexbox in Media Queries

How can I use flex boxes in CSS to ensure that from a min-width of 769px to 1025px, the third figure is displayed on a new line while the first and second figures remain on the top line taking up equal space? <div class="mid-col-section-2"> <figu ...

Personalizing the mat-checkbox

I'm trying to customize the checked icon in angular material mat-checkbox. Currently, it displays a white tick icon inside a colored background box, but I want to replace the tick with a cross when it is checked. After spending all day searching, I ha ...

Having trouble applying CSS while printing using the ngx-print library in Angular 14. Can anyone help me out with this issue

The table shown in the image is experiencing issues with applying CSS properties when printing. While the background graphics feature has been enabled, the preview section still does not reflect the CSS styling. What could be causing this discrepancy? Cli ...

What is the most efficient method to match a list of titles with a distinct list of their associated links using Beautiful Soup 4 (bs

Update: I've found the solution! list_c = [[x, y] for x, y in zip(titleList, linkList)] Initial inquiry: In my project to scrape a recipe website using bs4, I encountered a challenge where the titles and links were not directly paired. After extracti ...

Modify the appearance of the post-content's color and background when hovering

I am struggling to achieve the goal of changing the background of the search box to transparent and making the search icon white on hover. Any help or suggestions would be greatly appreciated! Below is the code where I want to apply the hover CSS effect: ...

Adjust the size of a menu by modifying its width

Hey everyone, I recently joined this website and I'm still learning how to code. My current project involves creating an off-canvas menu, but I've come across a few challenges. Firstly, does anyone know how to adjust the width of the menu when it ...