"Struggling to design a hover drop-down menu using HTML and CSS. Need some assistance

Currently, I am attempting to craft a drop-down menu using HTML and CSS with a hover effect. Below is the HTML code that I have been working on:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
    <style>
        body {
            background-color:#181818;
            margin: 0px;
        }

     ul li {
        display: inline-block;
        font-size: 140%;
        color: orange;
        width: 150px;
        background-color: #505050;
        height: 50px;
        border-radius: 150px;
        position: relative;
        }

        p {
            margin-top: 10px;
            text-align: center;
            margin-bottom: 20px;
        }
        ul li:hover {
            color: #505050;
            background-color: orange;
        }
        
     id2 {
        display: none;
        position: absolute;
        z-index: 999;
        left: 0;
    }
    id1:hover id2 {
        display: block; /* Display the dropdown */
    }


    </style>
<body>

        <ul id="topMenu">
            <li><p>Home</p></li>
            <li><p id="id1">Projects</p></li>
            <ul id="id2">
                <li><p>Project 1</p></li>
                <li><p>Project 2</p></li>
                <li><p>Project 3</p></li>
            </ul>
            <li><p>About Us</p></li>
            <li><p>contact us</p></li>
        </ul>

</body>
</html>

I seem to be encountering some difficulties in my CSS code. Any assistance in identifying and rectifying these issues would be greatly appreciated in ensuring the correct implementation of the hover drop-down menu.

Thank you, Bhavik

Answer №1

you have incorrectly marked up your drop-down menu

instead

<ul id="topMenu">
            <li><p>Home</p></li>
            <li><p id="id1">Projects</p></li>
            <ul id="id2">
                <li><p>Project 1</p></li>
                <li><p>Project 2</p></li>
                <li><p>Project 3</p></li>
            </ul>
            <li><p>About Us</p></li>
            <li><p>contact us</p></li>
        </ul>

to

<ul id="topMenu">
            <li><p>Home</p></li>
            <li><p>Projects</p>
               <ul>
                  <li><p>Project 1</p></li>
                  <li><p>Project 2</p></li>
                  <li><p>Project 3</p></li>
              </ul>
            </li>
            <li><p>About Us</p></li>
            <li><p>contact us</p></li>
        </ul>

Example:

body {
    background-color:#181818;
    margin: 0px;
}
ul li {
    display: inline-block;
    font-size: 140%;
    color: orange;
    width: 150px;
    background-color: #505050;
    height: 50px;
    border-radius: 150px;
    position: relative;
}
p {
    margin-top: 10px;
    text-align: center;
    margin-bottom: 20px;
}
ul li:hover {
    color: #505050;
    background-color: orange;
}
ul li ul{
    display: none;
    padding-left: 0;
}
ul li:hover ul{
    display: block;
    position: absolute; top: 100%;  left: 0;
}
<ul id="topMenu">
    <li>
        <p>Home</p>
    </li>
    <li>
        <p id="id1">Projects</p>
        <ul id="id2">
        <li>
            <p>Project 1</p>
        </li>
        <li>
            <p>Project 2</p>
        </li>
        <li>
            <p>Project 3</p>
        </li>
    </ul>
    </li>
    
    <li>
        <p>About Us</p>
    </li>
    <li>
        <p>contact us</p>
    </li>
</ul>

Answer №2

check this out: http://jsfiddle.net/412cosa7/

HTML Tricks:

If #id1 is not a direct ancestor of #id2, you can remove #id2 from the <p> tag and place it inside a <li> tag, then nest #id2's element within #id1's element.

In reality, the <p> tag in your source is unnecessary.

CSS Tip:

Utilize the parent selector ">". For example: #id1:hover > #id2

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

Error: The specified object does not contain the 'tableRow' method

I am currently working on a contacts book project and I need a table to update as the user inputs data. However, I keep encountering an error related to 'tableRow'. I have tried changing function names and other solutions but haven't been ab ...

Using a CSS-animated DIV as a background with a transparent background DIV overlaid with content on top

In my attempt to overlay bootstrap/html/css on a CSS animated starfield background, I'm encountering some challenges. Online resources suggest using a 1x1 pixel png, but I am looking for a method to implement transparency through coding. Using backg ...

Nav bar breaching the Bootstrap threshold

I am new to using bootstrap and currently in the learning process. I am facing an issue with the navigation bar I created. All the contents are aligning to the right, but the search bar and tabs are breaking onto a new line. I have attempted various soluti ...

"Enhance your shinydashboard with a customizable header dropdown feature that allows you

I am trying to create a vertical grouped dropdown menu in the header panel that contains multiple links. Currently, I can only achieve a flat horizontal layout using tags$li. I want to place linkA and linkB under grouplinkAB, allowing users to open one o ...

Update the database with the new values and display them in an HTML table

I am working with a table that looks like the one below: <script> $("#edit").hide(); // Initially hide the edit table $("#update").click(function() { $("#edit").toggle(); $("#shown").toggle(); ...

Delivering Background Videos with Node.JS

Apologies if my question seems off base or confusing, as I am not very knowledgeable in the world of nodejs. I have been comfortable using just plain PHP and Apache for a while until I discovered ZURB Foundation's stack with Handlebars and SASS, along ...

The HTML footer elegantly hovers just above the bottom of the page, at specific resolutions

Preparing my website for a college interview, but struggling to figure out why the footer is floating above the bottom of the page when the window size is above a certain point. Below are the HTML and CSS snippets used: html,body{ width: 100%; h ...

Currently experiencing difficulty with arrow key navigation in ReactJS

I'm attempting to create a xmb (psp/ps3) menu using react, but I've encountered an issue. The problem is that the items scroll past each other instead of moving in order. For example: Item 1 jumps to Item 3 (skipping Item 2, even though it should ...

What is the process for determining the overall cost of a column in Angular 9?

I am facing a challenge in calculating the total price, total quantity, and total counted(#) of each column in a table. | Item | Price | Quantity | 1 | Mouse | 500 | 1 | 2 | Wire | 100 | 2 | TI:3 | |TP:600 | TQ:3 | < ...

JavaScript: Conditional statement used to determine the target of a touched element

I'm currently working on enhancing the mobile responsiveness of a React project. As part of this process, I am attempting to ensure that certain JavaScript-driven style changes are only applied to elements that are not the target or a child of: <d ...

Is there a way to use jQuery to load a ul from a different webpage into a div?

Progress is being made as I work on making two nearly identical pages function seamlessly together. One page features an alphabet list, and when a letter is selected, the other page filters results (last names from a PHP database query) and displays them o ...

Having issues with the CSS dropdown menu not activating when hovering

I am facing an issue with a simple menu that includes a dropdown feature upon hovering. The code works perfectly fine in Fiddle, however, it fails to work when running the entire page locally on IE. Can someone please assist me? (here's my code at fi ...

Populate JQuery fields dynamically based on changing name elements

I am working on a section that displays products dynamically fetched from a database using an ajax call. The results vary, some are single-row while others have multiple rows. Initially, I have a placeholder row of HTML on the main page for data entry whic ...

Incorporating JSON data into data-groups within an Ionic application

I am currently working on developing an Ionic app using the newest version (Ionic 3xx and Angular 5) and I am attempting to incorporate the shuffleJS feature for filtering. Everything was working fine when I used static data in my template with the 'd ...

Update a div in PHP using the data received from an AJAX response

I recently developed a PHP application and encountered an issue with updating the value of a date picker. The user is prompted for confirmation when changing the date, and upon confirmation, the date in the "expiry" field (with id expiry) should update alo ...

Can JQuery be used to identify the CSS styles applied to selected elements?

Highlight the text by selecting it and clicking a button. If the text is already highlighted, clicking the button should make the selected text return to normal. Can this be achieved using jQuery and a single button? Is it possible to identify the CSS st ...

Can using display: none impact the SEO of a website's navigation menu?

Is there a difference in SEO impact between using display:none with CSS and using display:none with jQuery for menus? Appreciate the insights! ...

What steps should I take to ensure my button remains intact and undamaged?

I'm encountering an issue with a CSS-styled button that appears distorted and displayed oddly across multiple lines when it is set as a link. However, if I change the button to an input element, it looks flawless. How can I ensure my link button displ ...

What is the best way to incorporate a counter into my JavaScript game?

I've created this JavaScript game: window.onload = function() { canv = document.getElementById("gc"); ctx = canv.getContext("2d"); document.addEventListener("keydown", keyPush); setInterval(game, 1000 / 7); } px = py = 10; gs = tc ...

Is it possible for you to pinpoint the mistake in the code below?

I've been trying to locate an error in the function but have been unable to do so. As a beginner in this area, I would appreciate your patience. <html> <head><title>print names</title> <script langua ...