Transforming a subnav bar into a dropdown on smaller screens

Hello everyone, I'm new here and seeking some guidance. Please bear with me if I seem a bit lost or naive. I am attempting to replicate the navigation bar from this website: WOHA. While I don't need it to be an exact replica, I have managed to create a similar hover effect on the secondary navbar by using jQuery onclick function toggle. However, my main challenge now is making the secondary navbar accessible as a dropdown option from the main navbar when the screen size decreases (similar to the example on the linked website). Could someone kindly provide me with instructions on how to achieve this?

$( ".subnav-trigger-about" ).click(function() {
  $( "#subnav-about" ).toggle();
});
    
$( ".subnav-trigger-services" ).click(function() {
  $( "#subnav-services" ).toggle();
});
#subnav-about {
  

  display: none;
}

#subnav-services {
  display: none;
}

.navbar-brand {
  color: #FFD700;
  opacity: 0.75;
  font-size: 2rem;
}

.navbar-brand:hover {
  color: #FFD700;
  opacity: 1;
}

.nav-item a:hover {
  color: #FFD700;
}

@media (min-width: 988px) {
  #subnav-about {
    position: absolute;
    right: 0;
    left: 0;
    
  }

  #subnav-services {
    position: absolute;
    right: 0;
    left: 0;
  }
}
  

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b7974746f686f697a6b5b2e35293529">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3c31312a2d2a2c3f2e1e6b706c706c">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/jquery-3.6.1.js" integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI=" crossorigin="anonymous"></script>

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
      <a class="navbar-brand" href="index.html">TEST</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mx-auto mb-2 mb-lg-0 test">
          <li class="nav-item">
            <a href="#" class="subnav-trigger-about nav-link">ABOUT</a>
          </li>
          <li class="nav-item">
            <a href="#" class="nav-link subnav-trigger-services">SERVICES</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">PROJECTS</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">CONTACT US</a>
          </li>
        </ul>

      </div>
    </div>
  </nav>

  <!-- About Us Secondary Nav -->

  <nav id="subnav-about" class="navbar navbar-expand-lg bg-light">
    <div class="container-fluid">
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav mx-auto">
          <li class="nav-item">
            <a class="nav-link" href="#">TEST</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">EXPERTISE</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">PEOPLE</a>
          </li>

        </ul>
      </div>
    </div>
  </nav>

  <!-- Services Secondary Nav -->

  <nav id="subnav-services" class="navbar navbar-expand-lg bg-light">
    <div class="container-fluid">
      <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav mx-auto">
          <li class="nav-item">
            <a class="nav-link" href="#">CONSULTATION</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">MANAGEMENT</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">TRAINING COURSES</a>
          </li>

        </ul>
      </div>
    </div>
  </nav>

I believe I may need to utilize @media queries in conjunction with the code, but I'm unsure of where to go from there

Edit: I have made some additions to my navbar content since my initial post, although nothing substantial that should have caused any major changes. I'm puzzled as to why the code snippet shared below seems to work for others but not for me. Any assistance in resolving this would be highly appreciated.

Answer №1

Here is a solution for you to try:

$(".subnav-trigger").click(function() {
  $("#subnav").toggle();
});
#subnav {
  display: none;
}
@media (min-width: 992px){
#subnav{position: absolute; right:0; left:0;}}
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6c61617a7d7aae7f454a4148427445408c4842">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0c6e6363787f787e6d7c4c39202e272e">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-IDwe1+LCz02ROU9k972gdyvl+AESN10+x7tBKgc9I5HFtuNz0wWnPclzo6p9vxnk" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/jquery-3.6.1.js" integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI=" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="index.html">TEST</a>

    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mx-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <a href="#" class="subnav-trigger nav-link">ABOUT</a>
 <nav id="subnav" class="navbar navbar-expand-lg bg-light">
  <div class="container-fluid">
    <div class="navbar-collapse" id="navbarNav">
      <ul class="navbar-nav mx-auto">
        <li class="nav-item">
          <a class="nav-link" href="#">TEST</a>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="#">Expertise</a>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="#">People</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

        </li>

        <li class="nav-item">
          <a href="#" class="nav-link">SERVICES</a>
        </li>

        <li class="nav-item">
          <a class="nav-link" href="#">Link</a>
        </li>
      </ul>
    </div>
  </div>
</nav>

<!-- About Us Secondary Nav -->

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

What is the best way to access nested JSON array data that includes varying key names and content?

In my current project, I am trying to extract data from a JSON array. Here is my approach: Below is the jQuery/JavaScript code I used to generate and retrieve data, particularly focusing on the nodes object which determines different layouts: var getPost ...

Update the second mouse click functionality using jQuery

I currently have a jQuery function integrated into my WordPress portal that manages the functionality of the menu and the display of subcategories to ensure proper mobile optimization. This code, which was created by the theme editor, handles these aspects ...

Merge data from api into visual charts using Google Chart Library

I received an API response with the following data structure: { "status": 200, "message": "OK", "data": [ { "_id": { "report_type": "robbery" }, "report_type": "robbery", "Counts": 11 }, { "_id": { "repo ...

Showing information retrieved from a database using PHP in a sequential manner

I'm currently working on developing a webpage that features 6 rows of images, with each row containing 4 images accompanied by captions right below them. The structure I have in mind is as follows: IMAGE IMAGE IMAGE IMAGE TEXT TEXT TEXT TEXT IMAGE ...

div is consistently correct across all web browsers

After creating the following CSS code for a div to always be positioned on the right: #button > .right { background-position: -268px 1415px; height: 180px; position: fixed; right: -90px; top: 40%; width: 263px; -webkit-transform ...

How can I disable a select element in Laravel 5?

Hey everyone! Currently using Laravel 5 and trying to style the "select" class as "selectpicker". I'm facing an issue where I want to disable or hide the selected option when clicked, as I'm creating a div with the option's content right b ...

Managing the hierarchy of controllers in CodeIgniter

I've been looking everywhere to find an example in Codeigniter on how to build a tree structure like the one below. If anyone can provide me with some assistance, it would be greatly appreciated. Specifically, when I click on child node 2, I want to ...

What is the process for creating a Sass mixin that declares a selector at the base level, rather than nested within another

Apologies for the unconventional terminology in the query, but I am struggling to find better words to describe it. Hopefully, this example will clarify my intention: scss-syntax .my-smug-selector { @include my-smug-mixin(30px); } desired css-output ...

Using the OR selector in a complex selector with jQuery

I am currently working on modifying a selector that locates every 6th product_tile element, then drills down to the child div with the tile_back class, and finally targets the select box with the name crust. However, I now need this selector to also includ ...

"Steady layout of grid for the navigation bar and

Currently, I am in the process of developing a control panel with the use of HTML and CSS. To structure the page, I opted for a grid layout. However, I encountered an issue where the navbar and sidebar do not stay fixed on the screen despite trying various ...

Interactive hexagon shape with dynamic image content on click using a combination of CSS, HTML,

As a beginner in CSS, HTML, and JavaScript, I came across a code snippet to create a pattern of hexagons with images (refer to the first code below). My goal is to change the image displayed on a clicked hexagon to another picture (see second code). First ...

Focus on targeting each individual DIV specifically

Is there a way to toggle a specific div when its title is clicked? I have tried the following code snippet: $( '.industrial-product' ).find('.show-features').click(function() { $('.industrial-product').find('.ip-feat ...

An effective approach to automatically close an Expansion Panel in an Angular Mat when another one is opened

I am attempting to implement functionality where one expansion panel closes when another is opened. By default, the multi attribute is set to "false" and it works perfectly when using multiple expansion panels within an accordion. However, in this case, I ...

Async functions within async functions

I am trying to obtain the geolocation data from a client and then load locations using Ajax, followed by displaying them as a list. Within my code, I have three functions: getGeolocation, loadLocation, and createList. Both getGeolocation and loadLocation ...

"Position centrally on the inside, using flexbox to fill the entire

Seeking assistance with aligning the text in the center of the flexboxes so that the h4 and p elements in the large box are centered within their respective boxes, as well as ensuring the text in the two smaller boxes is also centered. Any help would be gr ...

Angular Recursive Bootstrap Breadcrumb Guide

I am looking to implement the bootstrap breadcrumb component (https://getbootstrap.com/docs/4.0/components/breadcrumb/) in my project. My goal is to use the breadcrumb to show the entire path to the current directory within a component that resembles a di ...

Is it possible for JavaScript code, within an AJAX-returned page, to invoke a function that originally resided in a JavaScript file on the initial page using jQuery?

I have come across this code snippet: <!doctype html> <html> <head> <meta charset="UTF-8> <title>JQuery Test</title> <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script> ...

Causes for the display of the text within the alt attribute

Recently, I attempted to view an HTML page in text browsers and noticed that the alt attribute value of the img tag was visible. I decided to omit the alt attribute and instead utilized CSS: .headerImg:after, .headerImg::after { conte ...

Invoke a web service with jQuery through Ajax requests

I am attempting to implement an Autocomplete feature for a search field but I am facing issues with the code provided below. The problem is that the web method is not triggering when the Autocomplete function runs. What could be causing this issue? Below ...

Utilizing CSS3 webkit-transitions with multiple properties on a div element along with multiple webkit-transition-delays

Is there a way to make a div slide 200px to the right and then reduce its scale by half separately? I want it to slide first and then scale, but currently both transformations happen simultaneously. I tried using webkit-transition-delay to set different ...