Creating a flexible and adaptive navigation menu

Struggling with making the navigation bar responsive. The toggle and positioning are not working properly. See the example below. Can't figure out what's missing?

<script type="text/javascript">
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "gnav") {
        x.className += " responsive";
    } else {
        x.className = "gnav";
    }
}
    
</script>
#header .gnav {
position: relative;
left: 0;
bottom: 0;
z-index: 1000;
width: 587px;
height: 75px;
overflow: hidden; /**/
}

.gnav a {
    float: left;
    display: block;
}

.gnav .icon {
    display: none;
}


#header .gnav h1 {
float: left;
}
#header .gnav ul {
float: left;
width: 351px;
}
#header .gnav li {
float: left;
}

#ul_menu{
/*
    position:relative;
    width:400px;
    list-style:none;
    text-align:center;
    top:100px;
*/
}
#ul_menu li{
/*
    width:200px;
    float:left;
 */
   position:relative;
}
#ul_menu li a{
/*
    width:198px;
    background:#EE0088;
    float:none;
    height:30px;
    display:block;
    border:1px solid #FCF;
    color:#FFF;
*/
}
#header .gnav li .sub_list {
position: absolute;
bottom: 28px;
    left: 0;
width: 140px;
padding: 10px 0 0 0;
background: #1e2d68;
}
#header .gnav li .sub_list li {
float: none;
padding: 0 5px 10px 20px;
background: #1e2d68;
color: #fff;
font-size: 11px
}
#header .gnav li .sub_list li a {
color: #fff;
}
#header .gnav li .sub_list.sub01 {
width: 100px;
}

@media screen and (max-width: 600px) {
  .gnav a:not(:first-child) {display: none;}
  .gnav .icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .gnav.responsive {position: relative;}
  .gnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .gnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}
<div class="gnav clearfix" id="myTopnav">
<h1><a href="index.html"><img src="images/index/logo.gif" width="236" height="75" alt="株式会社ジェイウィル J-WILL CORPORATION"></a></h1>
<ul id="ul_menu" class="clearfix">
<li><a href="index.html"><img src="images/common/gnav01.gif" width="111" height="38" alt="HOME"></a></li>
<li>
<a href="company/index.html"><img src="images/common/gnav02.gif" width="100" height="38" alt="企業情報"></a>
<ul class="sub_list sub01">
<li><a href="company/index.html">代表ご挨拶</a></li>
<li><a href="company/index.html#sc02">行動指針</a></li>
<li><a href="company/index.html#sc03">企業概要</a></li>
</ul>
</li>
<li>
<a href="business/index.html"><img src="images/common/gnav03.gif" width="140" height="38" alt="事業内容"></a>
<ul class="sub_list sub02">
<li><a href="business/index.html">ジェイウィルの強み</a></li>
<li><a href="business/index.html#sc03">販売実績</a></li>
<li><a href="business/index.html#sc06">マンション一棟<br>仲介実績</a></li>
<li><a href="business/index.html#sc02">マンション用地<br>仲介実績</a></li>
<li><a href="business/index.html#sc05">戸建用地仲介実績</a></li>
<li><a href="business/index.html#sc01">事業概要</a></li>
</ul>
</li>
<li><a href="staff/index.html"><img src="images/common/gnav04.gif" width="111" height="37" alt="社員紹介"></a></li>
<li><a href="careers/index.html"><img src="images/common/gnav05.gif" width="100" height="37" alt="採用情報"></a></li>
<li><a href="contact/"><img src="images/common/gnav06.gif" width="140" height="37" alt="お問い合わせ"></a></li>
<li><a href="javascript:void(0);" class="icon" onclick="myFunction()"><i class="fa fa-bars"></i></a></li>
</ul>
</div>

The icon is not toggling and when I make the screen smaller, the navbar does not disappear. Is the issue related to media queries or is there something wrong with the main CSS? Appreciate any help in identifying the problem! Thanks for your assistance!

Answer №1

If you're looking to quickly build a responsive navbar, I suggest checking out Bootstrap 4. It's easy to implement and will have your navbar up and running in no time.

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

Utilize the $.each method to incorporate a value to an outside variable

Within my codebase, I handle an Array of Strings by utilizing the following function: $.each(lines, processLine); The challenge I faced was that while the processLine function returns a string, I needed to merge all the strings into one final result. How ...

Changing colors in the rows of a table

Here is a fiddle I created to demonstrate my issue. https://jsfiddle.net/7w3c384f/8/ In the fiddle, you can see that my numbered list has alternating colors achieved through the following jQuery code: $(document).ready(function(){ $("tr:even").css("ba ...

Tips for implementing a live filter on an HTML table using JavaScript without the need to refresh the webpage

I successfully implemented these table filtering codes using plain JavaScript that I found on W3schools. The code filters table data based on input text and includes a select dropdown for additional filtering options. However, I encountered some issues whe ...

Monitor the collection for changes before adding an item to the collection

When using ui-select multiple, I am facing an issue where I need to check the collection before ng-model="collection" is updated in order to ensure that the new value is not already present in it. Simply watching the collection does not solve this problem ...

Achieve maximum use of vertical and horizontal space for items in a flexbox

I have a container with elements, each containing an image. I want to arrange these images in the four corners of the container using flexbox. Currently, they are distributing correctly horizontally but not taking up all the available space vertically. He ...

Learning how to utilize localStorage in conjunction with a color picker to modify the --var of :root

After spending an entire afternoon on my JavaScript issue as a beginner, I have a specific goal in mind: allowing the user to select and change the main color of the page. To implement this, I inserted a color picker in my HTML: <input type="color ...

Directing attention to a concealed element

I need help triggering a focus event after an element is recognized as focusable within the DOM. I have successfully shown form elements based on previous input, now I need to set focus for user input. Currently, the function provided is not working unle ...

Adding a style to table data within a table row

I am facing an issue with the following code: <tr> <td class="someclass"> <input /> </td> ... <td> </td> <td> </td> .. </tr> I am looking to override t ...

Find all elements with the same class and identify the first element among them using jQuery

In my dynamic data structure, I need to filter out all elements with the class name "My_data" and select the first one. In the code below, I am looking to retrieve the first element with the class "My_data". <div class="all_data"> <div class="lis ...

What causes the "Import declaration conflicts" error in TypeScript when using require('react')?

Currently, I am exploring the world of Typescript in combination with React and JSX. It's quite a journey, especially when dealing with the complexities of build systems... When compiling my .tsx file (Typescript + JSX) using gulp-typescript, everyth ...

What is a way to adjust the width of fixed columns in a Bootstrap 4.0 responsive table?

I've been struggling to make this work despite following similar questions and answers. I attempted to adjust the column width by directly setting each one, but nothing seems to change! <thead style="white-space: nowrap"> ...

What is causing this div to automatically assume a width of 900 pixels?

There is only one specific instance where I have set a div width to 900 px, and it is located further up the DOM within a div called #Ba. This is where the width is being derived from. My intention is for it to simply take on the width of its containing e ...

Tips on sending dynamic data with jQuery

I am struggling with titling this question properly, so I apologize for any confusion. My issue involves a JSON file containing two arrays. { "list 1": [ { "name": "label1", "imgPath": "images/list1img1.png" ...

The functionality of one button triggering the opening of two dropdown menus simultaneously is being successfully implemented

My issue is that I have created two different dropdowns, but they both open the same dropdown menu. Here is my code; <div class="d-flex"> <button class="btn btn-icon btn-group-nav shadow-sm btn-secondary dropdown-toggle" type="butto ...

Show and hide iframe using jQuery

I am attempting to toggle the visibility of an iframe using a button or anchor element. I found a fiddle created by Sethen posted in this answer, and used it to create my own version. It works perfectly with jQuery 1.8.3, but when I try to use a newer ver ...

Introducing a variety of sub-menu fonts within an elegantly designed jQuery vertical accordion menu

I am struggling to achieve different font sizes, colors, and weights for top and sub-menu level links in my simple jQuery vertical accordion. Despite changing the CSS, I can't seem to get it right. What am I missing? Is there an easy way to accomplish ...

Tips for changing the color of an underline in an <a> tag when it is hovered over

Is it possible to change the color of the underline in an <a> tag when hovering over it? After some investigation, it seems that direct color changes are not feasible. Instead, you can use the border-bottom option. However, my attempt did not yield ...

Next.js server component allows for the modification of search parameters without causing a re-fetch of the data

I have a situation where I need to store form values in the URL to prevent data loss when the page is accidentally refreshed. Here's how I am currently handling it: // Form.tsx "use client" export default function Form(){ const pathname ...

Modifying a boolean attribute in a React state object

I'm facing a challenge while trying to update a boolean attribute within a state object. Here's the approach I'm taking: this.setState( prevState => ({ selectedGrocery: { ...prevState.selectedGrocery, checked: !prevStat ...

Having trouble with displaying nested views in Angular UI-Router?

I am struggling to implement angular ui-router in my project. The views are not displaying as expected. Can anyone provide assistance? Here is a list of the files involved: app.js, index.html, home.html, leftTree.html, topBar.html, ihome.html, footer.html ...