Tips on creating a hover-activated drop-down submenu

I'm having trouble getting the submenu to display correctly when hovering over parent list items. It's displaying in a strange way, is there a standard method for doing this? I can't seem to make any code adjustments work with my current setup.

<div id="navBar">
    <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Artist</a>
            <ul>
                      <li><a href="#">sub menu item 1</a></li>
                      <li><a href="#">sub menu item 2</a></li>
                      <li><a href="#">sub menu item 3</a></li>
                      <li><a href="#">sub menu item 4</a></li>
            </ul>
        </li>
        <li><a href="#">Apprentice</a>
            <ul>
                      <li><a href="#">sub menu item 1</a></li>
                      <li><a href="#">sub menu item 2</a></li>
                      <li><a href="#">sub menu item 3</a></li>
                      <li><a href="#">sub menu item 4</a></li>
            </ul>
        </li>
        <li><a href="#">Contact Us</a></li>
        <li><a href="#">Store</a>
            <ul>
                      <li><a href="#">sub menu item 1</a></li>    
            </ul>
        </li>
    </ul>
</div>

CSS

 /*navigation bar*/
#navBar {
    width: 100%;
    float: left;
    margin: 0;
    padding: 0;
    background-color: #000000;
    border-bottom: 1px solid #ffffff;
}
#navBar ul {
    list-style: none;
    width: 800px;
    margin: 0 auto;
    padding: 0;
} 
#navBar li {
     float: left;
    position:relative;
}
#navBar li a {
    display: block;
    padding: 8px 15px;
    text-decoration: none;
    font-weight: bold;
    font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
    color: #ffffff;
    border-right: 1px solid #ffffff;
    height: 30px;
    font-size: large;
}
 #navBar li:first-child a {
     border-left: 1px solid #ffffff; 
}
#navBar li a:hover {
    color: #000;
    background-color: #9DE4BD;
}
#navBar li ul {
    position:absolute;
    left:-9999;
}
#navBar li:hover > ul  {
 display: block;
  position: relative;
  left: 0px;
  float: none;
  clear: left;
}

Answer №1

Here's a straightforward illustration for you ;) Make sure to specify display:inline-block for your li elements and utilize position:absolute for the submenu (ul)...

CSS:

#a{
    width:96%;
    height:600px;
    position:relative;
    background-color:#0F0;
    background:url(http://8pic.ir/images/lj80krsorpqzchx6xs8b.jpg);
    opacity:0.8;    
    font-family:tahoma;
    font-size:12px;
    padding:2%;

}
#nav ul{
    position:relative;
    height:30px;
    list-style:none;
    background:#000;
    padding:0px;
}
#nav ul li{
    display:inline-block;
    width:100px;
    height:30px;
    text-align:center;
}
#nav li{
    background:#000;
}
#nav li:hover{
    background:#333;
}

#nav ul li ul{
    display:none;
    position:absolute;
    top:30px;
}
#nav ul li ul li{
    display:block;
}
#nav ul li:hover ul{
    display:block;
}
#nav a{
    line-height:25px;
    text-decoration:none;
    width:100px;
    color:#fff;
}

HTML:

<div id="a">
    <div id="nav">
        <ul>
            <li><a href="#">home</a></li>
            <li><a href="#">mobile view</a></li>
            <li><a href="#">categories</a>
                <ul>
                    <li><a href="">Cat 1</a></li>
                    <li><a href="">Cat 2</a></li>
                </ul>
            </li>
            <li><a href="#">contact me</a></li>
        </ul>
    </div>
</div>

Live Demo on JSFiddle

Answer №2

To achieve this effect, simply utilize the CSS properties display:none and display:block;. Initially hide the element and then show it on hover using display:block;.

There are numerous tutorials available online that delve into this topic further. One such example can be found at:

UPDATE: It is recommended to assign classes to your ul elements so that you can easily target and manipulate specific ones by applying display:none; in their CSS properties. Then, on hover of a designated element, use .classname:hover{display:block;} for the desired effect.

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

Is there a way to retrieve the JSON url from the input box

My goal is to retrieve a JSON file from a URL entered in a text box. I have identified the text box as txtr and used jQuery to get the value like this: var txtbval = $("#txtr").val();. I then included this value in the JSON parsing script as follows: url: ...

Is it possible to develop an image that can be zoomed in and out using the mouse

$(document.createElement('img')) .width(imgW) .height(imgH) .addClass('img_full') .attr('src', $(this).attr('data-src')) .draggable() .css({ &a ...

What is the correct way to extract a value from a keyvalue pair?

When dealing with an object that returns boolean "issues", I specify it as a string. If the value is true, I aim to show a checkmark; if false, I want to display a cross. <ul *ngFor="let filtered of reposFiltered | keyvalue"> <li *ngIf=& ...

Require the capability to bypass inline CSS styling

Currently facing an issue with a wordpress website I am constructing using iThemes Builder. The problem arises when integrating the plugin Jquery Megamenu, as the drop-down menu gets truncated due to an overflow:hidden property that cannot be overridden i ...

How to reduce the text input field size in Django Bootstrap for a sleeker design

I have a form with various elements like textfields, radio buttons, and text areas. The textfield size is currently too small and does not look good: https://i.sstatic.net/ghimd.png I'm wondering how I can make the textfields slightly larger and why ...

The if-else statement doesn't seem to be functioning correctly once the else condition is included

I'm currently developing a search function for an address book that iterates through the contact array and displays them in a popup. Initially, it was working correctly by finding and displaying the contacts that were found. However, once I added the ...

The image source is visible in Vue.js, but unfortunately, my picture is not showing up

Below is the code and script that I have: <template> <div class="tasks_container"> <div class="tasks_content"> <h1>Tasks</h1> <ul class="tasks_list"> ...

Tips for Printing a Page Using CSS and Bootstrap Formatting

I'm having trouble getting my website to print with its CSS and bootstrap styles. When I try to print using ctrl + p or the window.print() function, only the div, controls, and buttons appear without their corresponding colors. I've attempted to ...

Error: The symbol $ is not recognized

This is a snippet of code that I've written to automate address filling and extract latitude/longitude/address information. <html> <head> <LINK rel="stylesheet" type="text/css" href="style.css"> <script type="text/javascript" sr ...

Toggling event triggers with the second invocation

At this moment, there exists a specific module/view definition in the code: define(['jquery', 'underscore', 'backbone', 'text!templates/product.html'], function($, _, Backbone, productTemplate) { var ProductView = ...

No matter what I attempt, my presentation refuses to align in the center

My slideshow, which is powered by jQuery/JS and involves absolute positioning for each image, is causing me trouble when trying to horizontally center it on the page. No matter what I do, I can't seem to get it right. The challenge is not only getting ...

Looking to adjust a group of range sliders. Can you help me troubleshoot my code on jsfiddle

I am looking for a way to ensure that my three range sliders all draw from a shared "value pool." For example, if the maximum value is 100 and slider-1 is set to 51, then sliders 2 and 3 should share the remaining value of 49. Currently, when slider 1 is a ...

What is the reason for the absence of margin collapsing in this particular instance?

One thing that confuses me is the absence of vertical margin collapse between each definition list(dl). Looking at the style selector "#sweden dl" below, I have specified a margin of 10px 20px; meaning a margin-top and margin-bottom of 10px for each dl, a ...

A fixed HTML div with a relative div is like having your own

I am in search of two divs with the following styles: <div style="height:20px;" /> <div style="height:100%;" /> These divs result in one being 20px tall and the other spanning 100% of the screen height, causing a vertical scrollbar that is eq ...

What is the best way to increase the size of the left margin?

The h1 text needs to be positioned more towards the middle of the screen, but I'm having trouble with the left margin. Adjusting the margin size in pixels and percentages doesn't seem to have any effect on the page. Changing the background color ...

A lateral sliding motion

Here is the HTML code I am working with: <div class="menuTabs"> <div class="mtabArrowLeft">Left</div> <input class="menutabBTN" name="" type="button" value="a" /> <input class="menutabBTN" name="" type="b ...

Failure to Activate AJAX Function on Android Device

Currently, I am working on an AJAX function that triggers a call either during scrolling or clicking/touching to add new content to my HTML. The functionality works perfectly in a desktop environment for both scroll and click events. However, when testing ...

Enhance Your Images with Fancybox 2.1.5 by Adding Titles Directly to the Photo Window

I need help figuring out how to place the title text inside the photo box on this particular page: Despite my efforts and multiple Google searches, I have not been successful in achieving this. As a newcomer to javascript, I am struggling with implementin ...

Tips on displaying a confirmation box when the page is refreshed or closed

I need to implement a confirmation box for users who try to close the window without saving the content of a textarea within a form. Here is the code I currently have: var myEvent = window.attachEvent || window.addEventListener; var chkevent = window.att ...

Utilize a CSS style or class on a div element

Looking for a way to style alternate div elements within a parent div? Check out this example: <div id="comment"> <div id="m1">...</div> <div id="m2">...</div> </div> I tried to use some jQuery to add a CSS c ...