Click here for more navigation options

I'm looking to enhance my navigation bar by adding an additional section that expands underneath when a specific button is clicked. Think of it like the functionality on the Apple Store website where clicking on a product reveals another bar.

https://i.sstatic.net/4eI3B.png

I have my CSS style sheet ready for reference, and I believe JavaScript might be needed for this, but I'd prefer a solution using pure CSS if possible!

Any assistance with this would be greatly appreciated!

Below is the HTML code for my navigation:

<header>
    <div class="title">
        <img src="img/logo2.png"/>
    </div>
    <div class="navbar">
        <ul>
            <li style="float: left"><a href="#home">Home</a></li>
            <li><a href="#contact">Contracts</a></li>
            <li><a href="#about">About Us</a></li>
            <li><a href="#other">Other</a></li>
            <li> <a href="#release">Release Notes</a></li>
            <li> <a target="_blank" href="http://www.phpartnership.com">Pinnacle Health Partnership</a></li>
        </ul>
    </div>
</header>

This will create the following structure:

https://i.sstatic.net/9sTak.png

And here is my complete CSS stylesheet:

body {
    background-color: #fff;
    margin: 0;
    font-family: Arial;
    color: #333;
}

<!-- CSS styles continue -->

I also attempted to use some JavaScript to toggle the visibility of the additional section upon clicking the corresponding button:

<script>
$(index.php).ready(function(){
    ``$("#contract").click(function(){
            $("<div class="contracts">").toggle();
        });
});
</script>

Answer №1

If you're looking for a certain functionality, try something like this: jsfiddle

To start, make sure to add jQuery to your local setup by including the following script in the head section of your HTML:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

You can find guidance on installing jQuery here.

Include the provided HTML inside the .navbar:


<ul class="additional">
     <li>test1</li>
      <li>test2</li>
      <li>test3</li>
      <li>test4</li>
</ul>

Add the following CSS:


.additional {
position:absolute;
top:100%;
width:100%;
background:#000;
display:none;
}
.additional li {
color:#fff;
}

Then include the JavaScript:


$('.navbar ul li:nth-child(2) a').click(function() {
          $(".additional").slideToggle();
});

For a more responsive approach, check out this version with targets: jsfiddle with target

Use data-target on the li a like so:


<li><a href="#contact" data-target="contracts">Contracts</a></li>
<li><a href="#about" data-target="aboutus">About Us</a></li>

Include the additional HTML:


 <ul class="additional contracts">
     <li>test1</li>
     <li>test2</li>
     <li>test3</li>
     <li>test4</li>
 </ul>
 <ul class="additional aboutus">
      <li>about</li>
      <li>about</li>
      <li>about</li>
      <li>about</li>
 </ul>

And add the corresponding jQuery:


$('.navbar ul li a').click(function() {
  $(".additional").slideUp();
  var target = '.' + $(this).data('target');
  $(target).slideDown();
})

Alternatively, you can target the href attribute of the li a by adding IDs as shown below:


<li><a href="#contract">Contracts</a></li>
<li><a href="#about">About Us</a></li>
------
<ul class="additional" id ="contract">
....
<ul class="additional" id ="about">
....

With the appropriate JavaScript:


$('.navbar ul li a').click(function() {
$(".additional").slideUp();
var target = $(this).attr('href');
$(target).slideDown();

})

For further reference and implementation, visit this jsfiddle link

Give one of these solutions a try and let me know how it works for you.

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

Website Responsiveness - inquiries for all needs

My first responsive website project has hit a roadblock, as I struggle to understand how to implement media queries effectively. Here's a snippet of my code... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/x ...

Is there a way to integrate a Python script or code directly into an HTML document?

I am currently facing a challenge while working on an assignment. The task at hand requires me to establish a connection with an Oracle database using Python in order to retrieve information about a specific table. Subsequently, I need to display this data ...

Searching for data across multiple tables using the Laravel framework combined with Vue.js and API integration

I am currently implementing a multi-search feature for a single table The search functionality is functioning correctly but only for one column Here is the snippet from my controller: public function index() { return Matter::when(request('sear ...

Convert vue.js output into a text input box in HTML

Currently, I am implementing a feature to scan QR codes using a web camera. If you are interested, you can find the library instascan. In my project, I have an index.html file where I call the function app.js(instascan/docs/..). However, I am facing an is ...

Tips for creating an inline style with "border: 1px solid #72777d"

For the boxes I applied borders using "border: 1px solid #f2f2f2" as depicted in the image. The issue is that with 1px solid borders, they overlap each other. Is there a way to use inline borders with CSS instead? Thank you in advance ^_^. border: 1px s ...

Executing a callback function within two nested functions

I am having trouble with the callback function that is being called inside a function which in turn calls another function. exports.user = function(userName, pwd, callback) { db.User.findOne({'userName': userName}, function(error, obj) { ...

Is there a way to manipulate a website's HTML on my local machine using code?

I am currently working on a project to create a program that can scan through a website and censor inappropriate language. While I have been able to manually edit the text using Chrome Dev tools, I am unsure of how to automate this process with code. I ha ...

When utilizing Md-select in Protractor, how can the dropdown list be accessed?

I am having trouble locating the option from the dropdown menu that I need to select. Despite trying various methods, I have not been successful. <md-select ng-model="card.type" name="type" aria-label="Select card type" ng-change="$ctrl.onCardSelecti ...

Numerous text boxes sharing identical IDs

Here is the code snippet that I am working with: <%for (int index = 1; index < 7; ++index) {%> <tr> <td> <div class="indicacao_gdp"> ...

What is the best way to pass a context to the function constructor?

After reading about the dangers of using the eval method, I decided to utilize a function constructor to prevent any malicious code injection. Here is the approach I took: function evalContext(context: Record<string, unknown>) { const injectCon ...

Struggling to decide on the perfect CSS selector for my puppeteer script

I am trying to use puppeteer page.type with a CSS selector. <div class="preloader"> <div class="cssload-speeding-wheel"></div> </div> <section id="wrapper" class="login-register"> <div class="login-box"> <d ...

Strange gray gradient background suddenly showing up on mobile devices

Encountering an issue with a login form displayed on a sliding modal. The design looks correct on desktop and even in responsive mode with dev tools, but when accessed through mobile Chrome or Safari on an iPhone, an unusual gray rounded box or shadow appe ...

Transferring an organized array to processing

My goal is to transfer an array of integers from a php file called load.php to a JS script, which will then forward it to a Processing file written in JavaScript. In load.php, I define the array and send it using JSON (the array contains a minimum of 40 i ...

Issues with table formatting and malfunctioning functions

I created a basic tic-tac-toe game, but I'm having an issue with the table display. It appears squished when the game loads. Is there a way to prevent this and keep the table empty? Additionally, I am using NotePad++ and encountering problems running ...

Tips for adjusting the timezone offset for upcoming dates affected by Daylight Saving Time

I have an upcoming event scheduled for 02-May-2021 at 1:30 PM in Central Daylight Time. However, I am scheduling it today on 25-FEB-2021 in Central Standard Time and noticing that the time shifts to 12:30 PM. Is there a solution to prevent this shift? My ...

I am looking to deactivate Sundays on the datepicker and also set a specific DateFormat

On my website, I have implemented a datepicker. I am trying to disable Sundays and set a specific date format. When I attempt to do this separately, it works without any issues. However, when I try to combine the two functionalities, it fails. I suspect th ...

The validation process using jQuery may encounter errors when attempting to validate inputs before an 'require_from_group_exact' input

Utilizing the jQuery Validation Plugin, I am able to validate various inputs on an HTML page. It is necessary for the first name to always be provided, and either an email OR a mobile number but not both. This validation is achieved by using the require_fr ...

Is it possible to convert the information from <input type="file"/> into an image on the client-side?

As mentioned, I currently have this code: <input type="file" accept="image/png, image/jpeg" onChange={(e) => onHandleProfileImageChange(e)} ></input> I receive the file's data and send it to the server. On the server side, ...

When importing a component that is passed as a prop in React, it is causing a blank page to render and is displaying an error in the

Visit this link for the code Everything was running smoothly with the app until I decided to include the Icon component in SidebarOption.js. Now, upon doing so, a blank page appears and an error is displayed in the console: An error occurred stating that ...

Using w3-include-html with a relative URL path

Is there a way for me to use the same header HTML on multiple pages within subdirectories without having to duplicate the file itself? As shown in the code snippet below, the header file is located in both the current directory and parent directory. While ...