Display of items in a submenu through a drop-down menu when hovering over it

As I work on creating a Navbar with a dropdown menu, I'm encountering an issue where hovering over the main list element displays all the submenu contents at once. No matter what I try in my CSS, including adjusting the positioning of li and ul elements, I can't seem to resolve this problem.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  color: white;
  font-family: Arial, Helvetica, san-serif;
}

body {
  background-color: pink;
}

h1 {
  text-align: center;
  margin: auto;
  padding-top: .4em;
  background-color: coral;
  height: 2em;
  width: 100%;
}

nav {
  background: red;
  margin: auto;
  padding: 10px;
  line-height: 1.6em;
  width: 100%;
}

nav>ul>li {
  position: relative;
  text-align: center;
  color: yellow;
  background-color: purple;
  width: 31%;
  height: auto;
}

nav>ul>li>ul {
  background-color: blue;
  list-style: none;
  position: absolute;
  /*display: none;*/
  width: 100%;
}

nav>ul>li:hover ul li {
  display: block;
}

nav>ul>li>ul>li>ul {
  list-style: none;
  background-color: black;
  position: absolute;
  left: 100%;
  top: 0px;
  width: 40%;
  display: none;
}

nav>ul>li>ul>li:hover ul {
  display: block;
}
<!DOCTYPE html>
<html>

<head>
  <title>Web Page</title>
  <link rel="stylesheet" type="text/css" href="C:\Users\geek\Desktop\positions\styles.css">
</head>
<header>
  <H1>My Web Page</H1>
</header>

<body>
  <div class="wrapper">
    <nav>
      <ul>
        <li>Music
          <ul>
            <li>Song 1
              <ul>
                <li>Thing 1
                </li>
                <li>Thing 2
                </li>
                <li>Thing 3
                </li>
                <li>Thing 4
                </li>
                <li>Thing 5
                </li>
                <li>Thing 6
                </li>

              </ul>
            </li>
            <li>Song 2</li>
            <li>Song 3</li>
            <li>Song 4</li>
          </ul>
        </li>
      </ul>
    </nav>
  </div>

</body>

</html>

The alignment issue arises when trying to center the first dropdown items with the "Music" section in the Navbar. Each subsequent sub-menu should appear positioned at top 0px and left 100% of their parent list item (e.g., "Song 1"). The expected results are outlined in the CSS for reference.

Answer №1

* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: white;
font-family: Arial, Helvetica, san-serif;
}

body {
background-color: pink;
}

h1 {
text-align: center;
margin: auto;
padding-top: .4em;
background-color: coral;
height:2em;
width: 100%;
} 

nav {
background: red;
margin: auto;
padding:10px;
line-height: 1.6em;
width: 100%;
}

nav > ul >li {
position: relative;
text-align: center;
color: yellow;
background-color: purple;
width: 31%;
height: auto;
}  

/* Additional styling */

nav ul li ul li ul {
  display:none;
}

nav ul li ul li:hover ul {
  display:block;
}
<!DOCTYPE html>
<html>
<head>
<title>Web Page</title>
<link rel="stylesheet" type="text/css" 
href="C:\Users\geek\Desktop\positions\styles.css">

</head>
<header>
<H1>My Web Page</H1>
</header>
<body>
<div class="wrapper">
    <nav>
        <ul>
            <li>Music
                <ul>
                    <li>Song 1
                        <ul>
                            <li>Thing 
1</li>
                            <li>Thing 
2</li>
                            <li>Thing 
3</li>
                            <li>Thing 
4</li>
                            <li>Thing 
5</li>
                            <li>Thing 
6</li>

                        </ul>
                    </li>
                    <li>Song 2</li>
                    <li>Song 3</li>
                    <li>Song 4</li>
                </ul>
            </li>
        </ul>
    </nav>
</div>

</body>
</html>

Answer №2

If you're looking to achieve a similar effect, check out this example that demonstrates it on click. You can adapt the idea for use with hover as well.

https://example.com/codepen

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

Tips for reducing text in a card design

As a newcomer to codeigniter 4, I recently created an event card with a lengthy description. However, when I attempted to display the event data from the database on the card, it ended up becoming elongated due to the length of the description: https://i. ...

Deactivate the second subradio button when the first option is selected and vice versa

Need some assistance, hoping for your help. I have 2 choices with subcategories. 1 - Option A ---- variant 1 ---- variant 2 ---- variant 3 2 - Option B ---- variant 4 ---- variant 5 ---- variant 6 Check out my code here Users must choose betwe ...

Guide on creating a hand-drawn pencil circle using only CSS!

Is anyone familiar with how to create a hand-drawn pencil circle hover effect using pure CSS, without relying on SVG like the one featured on CodeMyUi? I've seen examples accomplished with SVG, but I'm specifically interested in achieving it usin ...

Gradient Border on CSS Button

I'm trying to create a button with a silver-ish border and gradient like the one in the picture. I've managed everything except for the border, which is giving me some trouble. Here's the current CSS code I'm using for the button. .b ...

Discover Vuejs: Display Less, Reveal More

In order to achieve a specific task, I need to display 12 items in 4 columns with 4 items each. Initially, only the first four items are visible and the rest are hidden until the user clicks the "Show More" button. Upon clicking the button, the next row ...

Concentrate on user input in PHP programming

My goal is to create a PHP validation form where errors are displayed correctly every time. However, I am encountering an issue with setting focus on the input field with an error if one occurs. I have variables like $rut_error, $first_name_error, $last_na ...

transferring data from the interface to the processor

I'm encountering an issue with sending dropdown list values to a new page for display. When I try to load the page after selecting a building from the dropdown menu, I receive an error message. I'm unsure about what mistake I might be making. Be ...

Tips for creating a responsive floating page div

Hey there, I'm still fairly new to web development and have a question regarding a registration page that is displayed as a floating div above the main page. How can I ensure that this div is centered in a responsive way? The pages are separated and ...

Is placing JavaScript on the lowest layer the best approach?

I'm facing a unique situation that I haven't encountered before and am unsure of how to address it. My website has a fixed top header and footer. On the left side, there is a Google Adsense ad in JavaScript. When scrolling down, the top header s ...

Move your cursor over a specific element to change the div located before it, rather than the one after

Is it possible to have <div> B affect <div> A on hover, instead of the other way around? I've only seen code for the reverse using (+) in CSS. #b:hover + #a { background: #ccc } <div id="a">Div A</div> <div id="b"> ...

Converting Scalable Vector Graphics to Hypertext Markup Language and

So I've got this SVG file called 7.svg. What's the best way to show this image using HTML or CSS on my index.html page? ...

Organize group data by month in a visually appealing table using HTML/PHP with

I have successfully implemented a code that prints HTML table rows from a database table and sorts them by date. The challenge lies in the fact that the date is stored as VARCHAR (due to a pre-existing project with an active database used in a PHP web app ...

Insert fresh user information into the div

Learning JavaScript is a challenge I'm tackling. I have a question that may seem trivial, but any assistance would be greatly appreciated. I currently have this code: Javascript <script type="text/javascript"> function fn(){ var Name = ...

Using JS regular expressions to only select anchor (a) tags with specific attributes

When trying to select a link tag (a) with a specific data-attr, I encountered an issue. I currently use the following regex pattern: /<a.*?data-extra-url=".*?<\/a>/g. However, I noticed that this selection goes wrong when there are no line br ...

Having trouble displaying image using absolute path in Express

Currently, I am developing a NodeJS application and have encountered an issue with a div element that has a background-image set in an external CSS file. Surprisingly, the CSS file functions perfectly when tested independently, and the background image dis ...

What is the best way to add an image element as a child of a list item only when its sibling checkbox is checked, upon clicking another button?

As a beginner in jQuery, I am working on developing a game where users can select and check 3 images from a list of ten. Following this selection, I aim to have the checked images added to a separate unordered list that I have already set up. <ul id="i ...

I am interested in creating a design where two DIVs are layered on top of each other with one having transparency, allowing visibility through to the background DIV. This can be achieved using a combination of

Looking to enhance the visual appeal of my website, I decided to incorporate a background image within a div. To add more depth and creativity, I am now striving to overlay a semi-transparent black box on top of the image. This overlay will not only allo ...

Switching Tabs When a Button is Clicked

I am currently using a guide from the link provided to learn how to create tabs: http://www.w3schools.com/howto/howto_js_tabs.asp. function openTab(evt, tabName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClass ...

Tips for inserting an HTML table into a div element using JQuery/JavaScript

I currently have a navigation bar with only two items. While I may potentially add more items in the future, for now I am content with just these two. Upon opening the page, my goal is to display the content of the first item in the navigation bar. Each it ...

Aligning angular bootstrap modal window in the center vertically

Is there a way to vertically center a modal window on the screen, regardless of the size of the content being displayed? I'm currently using an image as a modal window in my page, and while I was able to align it horizontally, I'm struggling to g ...