Looking to add a dropdown feature to my current main navigation bar

I've been struggling to add a drop-down menu to my website's main menu. Every time I try, something goes wrong - sometimes the menu appears inline, other times it completely messes up the layout.

Here is the HTML code snippet:

<ul class="mainmenu">
<li><a href="<?=MakeUrl('home')?>">Best Forex Broker</a></li>
<li><a href="<?=MakeUrl('toprated')?>">Top Rated Brokers</a></li>
<li>
    <a href="<?=MakeUrl('coupons')?>"> Forex Bonus</a>
    <ul>
        <li><a href="#">drop1</a></li>
        <li><a href="#">drop2</a></li>
        <li><a href="#">drop3</a></li>
    </ul>
</li>
<li><a href="<?=MakeUrl('articles')?>">Articles & Tutorials</a></li>
<li><a href="<?=MakeUrl('affiliates')?>">Affiliate Programs</a></li>
<li>
    <a href="<?=MakeUrl('feeds')?>"><img src="<?=$site_folder?>/images/rss.png" alt="RSS" /></a>
</li>
<li>&nbsp;</li>
</ul>

And here is the CSS code snippet:

div.mainmenu { background: url('images/body-bg.gif') 0px -50px repeat-x; }
div.mainmenu div.center { background: url('images/body-bg.gif') 0px -50px repeat-x; border-bottom-color: #007399; border-left: none; border-right: none; }
ul.mainmenu { height: 28px; padding: 4px 0px 5px 0px; background: url('images/body-bg.gif') 0px -50px repeat-x; }
ul.mainmenu li { float: left; padding: 5px 10px 5px 12px; margin: 0px;  background: url('images/mainmenu-sep2.gif') left repeat-y; font-size: 15px; }
ul.mainmenu li a { color: #fff;  }
ul.mainmenu li a:hover { color: #e0f0ff; }
ul.mainmenu img { width: 20px; height: 20px; vertical-align: middle; margin: 0px 0px -2px 0px; }

I could really use some help with this. Thank you!

Answer №1

If you want to create a dropdown menu, you can follow the example below. Simply add a class .dropdown to the 2nd level menu and initially hide it. Then, when a user hovers over the li element, display the dropdown menu. Make sure to set positions for both the main menu and the dropdown menu in your CSS. Customize the styling of your dropdown as needed. Check out the code snippet.

ul.mainmenu { position: relative; height: 28px; padding: 4px 0px 5px 0px; }
ul.mainmenu li { list-style-type: none; float: left; padding: 5px 10px 5px 12px; margin: 0px; font-size: 15px; }
ul.mainmenu li a { color: #000;  }
ul.mainmenu li a:hover { color: #e0f0ff; }
ul.dropdown { position: absolute; overflow: hidden; top: 24px; margin: 0; display: none; background: #ccc; padding: 0; max-width: 100px; }
ul.mainmenu li:hover .dropdown, .dropdown:hover { display: block; }
<ul class="mainmenu">
<li><a href="<?=MakeUrl('home')?>">Best Forex Broker</a></li>
<li><a href="<?=MakeUrl('toprated')?>">Top Rated Brokers</a></li>
<li>
    <a href="<?=MakeUrl('coupons')?>"> Forex Bonus</a>
    <ul class="dropdown">
        <li><a href="#">drop1</a></li>
        <li><a href="#">drop2</a></li>
        <li><a href="#">drop3</a></li>
    </ul>
</li>
<li><a href="<?=MakeUrl('articles')?>">Articles & Tutorials</a></li>
</ul>

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

Advantages of placing script src tags at the top of the body versus placing them at the bottom of the body

I've heard that it's best to place the script tags right before the closing body tag. However, when I follow this advice, my angularJS expressions don't seem to compute correctly for some reason. When I place the script tags in that location ...

Troubleshooting V-model errors within VueJS components

Just dipping into VueJS and trying out a chat feature from a tutorial. I noticed the tutorial uses v-model in a component, but when I replicate it, the component doesn't display on the screen and the console throws a "text is not defined" error. Strug ...

What is the best way to transfer $scope to a service in angular.js?

I have two services listed below. When navigating to a specific URL, I need to resolve "OtherService.promise". However, for certain routes, I would prefer to utilize a scope variable within the AppService method instead of the promise. How can I make thi ...

Using the Mongoose $or operator with a nested array in query conditions

Here are the schemas I am using: //ProjectModel const ProjectSchema: Schema = new Schema( owner: { type: Schema.Types.ObjectId, ref: "User" }, users: [{type: Schema.Types.ObjectId, ref: "ProjectUser", unique: true }] ); //Project Use ...

Storing HTML form values for other pages in PHP and MySQLi can be achieved by using session variables or by

I recently set up a database using phpMyAdmin that includes tables for different topics such as Math, Physics, etc. Each topic is assigned a unique topic_id as the primary key in the table tb_topic. Additionally, each topic can have multiple videos associa ...

Using ng-if in AngularJS to compare two objects

I am transferring between controllers with different formations, and I am attempting to use "ng if" to compare the ids, but it is not functioning as expected. Below is the code snippet: var postsApi = 'http://mywebsite.com/wp-json/posts?filter[p ...

Obtaining the row number from a query and transferring it to another Google spreadsheet

I'm currently facing a challenge in extracting both cell data and the corresponding row number from a different sheet in Google Sheets. Although I have successfully formulated a query to retrieve all necessary column values, I'm struggling to inc ...

Develop a reusable block of Vue template when creating a new component

There are times when I find myself needing to repeat certain sections of HTML code in my Template just to keep it DRY. However, creating a new component and passing multiple props and dynamic data seems like too much work. Is there a simpler way to define ...

As the page is scrolled upwards, the custom cursor's location shifts accordingly

In developing a custom hover cursor for my video div, I am encountering an issue where the cursor does not move with my mouse and remains in place when I scroll. Could someone please review my code and identify what I am doing wrong? Additionally, I have ...

TypeScript's type assertions do not result in errors when provided with an incorrect value

We are currently using the msal library and are in the process of converting our javaScript code to TypeScript. In the screenshot below, TypeScript correctly identifies the expected type for cacheLocation, which can be either the string localStorage, the ...

What steps can I take to ensure my CSS component remains unaffected by the global CSS styles?

My navbar component is not displaying the styles correctly as intended. I have a Navbar.module.css file to style it, but after using next-auth for social login, only the buttons remain unstyled while everything else gets styled. The code snippet for impor ...

How to efficiently mock the $window object in Angular unit tests

Attempting to unit test an angular custom service written in Typescript has been a challenge for me. The service is designed to read a global variable defined on the Window object and I have made it promise-based for potential future AJAX calls. Below is a ...

How can I ensure my game or website fits perfectly on the screen without any need

I have recently developed a fun Erase-A-Man game that is optimized for mobile devices. However, I am facing an issue where users need to scroll to view all the letters on their screens. My goal is to make the game fit perfectly on all phone screen sizes so ...

Is it possible for a jQuery selector to retain its object? What can be done to prevent it

One interesting scenario involves a dropdown element within a container. <div class='container' /> <script> var dropdown = "<select class='multi-dropdown'> ... </select>" </script> Every time the value ...

Evaluate One Input Value to Determine Another Input Value

Hey there, I've made an update to the fiddle but let me clarify what I'm trying to achieve. My goal is not to implement form validation as I already have that covered with the HTML5 "required" attribute. What I'm aiming for is to customize t ...

Setting up Mongoose with Admin JS in NestJS: A Step-By-Step Guide

After successfully configuring adminJS in my Nest JS application, it now runs smoothly on localhost:5000/admin. @Module({ imports: [ import('@adminjs/nestjs').then(({ AdminModule }) => AdminModule.createAdminAsync({ ...

Identifying a client's request termination in Jersey

My Jersey application provides a restful web service. I've noticed that clients often abort requests made via ajax, using methods like the one described here On the server side, I'm seeing the following output which is not ideal. Is there a way ...

Comparison of performance between serializing an object to indexedDB and using JSON.stringify

I am curious about the efficiency differences in terms of browser/CPU/memory between using JSON.stringify for serialization versus writing an object to an object store in indexedDB. The context for this question is optimizing the process of writing an obj ...

Start and Pause PHP Execution on Demand

In order to pause the execution of PHP/HTML code and resume it by clicking on a "continue" button, here is an example: <?php echo "Hello"; // I would like the PHP code to halt after printing "Hello" here. // Additionally, I would like to include a "C ...

Tips on updating the content of an HTML element dynamically when it is already being populated using *ngFor

I have created an HTML component that utilizes *ngFor to dynamically generate HTML elements. This results in a total of 3 tags being generated when the code is run. I have included data such as subject in the component file which updates dynamically, how ...