Hover-activated dropdown menu

After reading numerous tutorials and spending hours trying to create a submenu on hover, I still can't seem to get it to work. My goal is to display "Zimmer", "Reservierung", and "Preise" in a vertical menu when hovering over "Hotel," and so on. Here is my code:

a {
    text-decoration: none;}
nav {
height: 50px;
clear: both;}
nav ul {
padding: 5px 0px;
text-align: center;}
nav li {
margin: 0px;
display: inline;
padding: 0px;}
nav li a {
font-size: 18px;
color: #775923;
padding: 0px 20px;
margin: 0px;}
nav li a:hover, nav li a.current {
color: #775923;
    padding: 0px 20px 14px 20px;
-webkit-padding-after: 15px;
box-shadow: 0 4px #b1d130;}
nav ul ul{
display: none;}
nav ul li:hover ul {
    display: block;}
<nav>
    <ul>
            <li><a href="#" class="current">Willkommen</a></li> 
            <li><a href="#">Hotel</a></li>
            <ul>
                    <li><a href="#">Zimmer</a></li>
                    <li><a href="#">Reservierung</a></li>
                    <li><a href="#">Preise</a></li>
                </ul>
            <li><a href="#">Restaurant</a></li>
            <ul>
                    <li><a href="#">Speisekarte</a></li>
                    <li><a href="#">Anlässe</a></li>
                </ul>
            <li><a href="#">Anfahrt</a></li>
            <li><a href="#">Kontakt</a></li>
            <ul>
                    <li><a href="#">Team</a></li>
                    <li><a href="#">Über Uns</a></li>
                </ul>               
       </ul>
</nav>

Link: http://jsfiddle.net/AuJeF/445/

I am hopeful that someone can assist me with this issue. Thank you.

Answer №1

If you are encountering an issue with your markup, make the following changes:

       <li><a href="#">Hotel</a>
            <ul>
                <li><a href="#">Zimmer</a></li>
                <li><a href="#">Reservierung</a></li>
                <li><a href="#">Preise</a></li>
            </ul>
        </li>

(this represents a simple dropdown menu)

 <nav>
        <ul>
            <li><a href="#" class="current">Welcome</a></li> 
            <li><a href="#">Hotel</a>
                <ul>
                    <li><a href="#">Rooms</a></li>
                    <li><a href="#">Reservation</a></li>
                    <li><a href="#">Prices</a></li>
                </ul>
            </li>
            <li><a href="#">Restaurant</a><ul>
                    <li><a href="#">Menu</a></li>
                    <li><a href="#">Events</a></li>
                </ul>
            </li> 
        </ul>
</nav>

CSS

nav
{
    margin-top:15px
}

nav ul
{
    list-style:none;
    position:relative;
    float:left;
    margin:0;
    padding:0
}

nav ul a
{
    display:block;
    color:#333;
    text-decoration:none;
    font-weight:700;
    font-size:12px;
    line-height:32px;
    padding:0 15px;
    font-family:"HelveticaNeue","Helvetica Neue",Helvetica,Arial,sans-serif
}

nav ul li
{
    position:relative;
    float:left;
    margin:0;
    padding:0
}

nav ul li.current-menu-item
{
    background:#ddd
}

nav ul li:hover
{
    background:#f6f6f6
}

nav ul ul
{
    display:none;
    position:absolute;
    top:100%;
    left:0;
    background:#fff;
    padding:0
}

nav ul ul li
{
    float:none;
    width:200px
}

nav ul ul a
{
    line-height:120%;
    padding:10px 15px
}

nav ul ul ul
{
    top:0;
    left:100%
}

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

Check out the jsfiddle here

Answer №2

There is an error in your HTML code. The ul element must be inside the li element, not outside of it.

<li>
    <a href="#">Hotel</a>
                <ul>
                    <li><a href="#">Zimmer</a></li>
                    <li><a href="#">Reservierung</a></li>
                    <li><a href="#">Preise</a></li>
                </ul>
</li>

Your writing style should have the ul element inside the li element for proper display. An ul tag cannot be placed directly inside another ul tag without a parent li element. This will result in a validation error. Remember to place the ul element inside the li element as per HTML standards.

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

Having trouble locating and interacting with the textarea element in Salesforce using Selenium Webdriver

Encountering an issue with the Selenium Webdriver where it throws an error stating that the element is not visible and cannot be interacted with when attempting to access a textarea. 1. The textarea is located within a pop-up window, which can be accessed ...

Customizing active-class in Vuetify

How do I customize the appearance of my v-list-item-group when it is in the active state? <v-list> <v-list-item-group v-model="day" color="green"> <v-list-item v-for="(item, i) in items" :key="i"> <v-list-item-content& ...

Items positioned to the left will not overlap

Having trouble with floating elements? Need to ensure that box 3 aligns under box 1 when resizing the browser window? Use this HTML template: <div class="box"> <p>box 1</p> <p>box 1</p> <p>box 1</p> & ...

The concept of vertical alignment within the CSS Grid system

I am attempting to utilize CSS grids in order to vertically align an unordered list. However, I am encountering the following layout: *A******* *B * *C******* ********* * * ********* ********* * * ********* While the grid template rows a ...

Nested Bootstrap structure with a column containing two sub-columns

I am attempting to create a grid layout using bootstrap. The layout should consist of a full 12 column layout on desktop, with an 8 column grid and a 4 column grid within it. The 8 column grid will contain an image/text, while the 4 column grid will have t ...

The value from the textbox is not being received by the JavaScript and PHP

I'm encountering an issue with my codes where they are not properly passing the value of the verification code from the textbox to JavaScript and then to PHP. I need assistance in resolving this issue. Below is the snippet of code: HTML: /* HTML c ...

Having trouble invoking PHP functions from AngularJS

Hey there, I'm a complete beginner in web development. I have an HTML file with AngularJS code that calls a .php file, both located in my local folder. I've tried looking online, but: Installing a server is not possible as this will be used ...

Display or conceal a div based on the size of the screen using HTML and CSS

Hey there, I recently finished my first React Project and I’m wondering if there’s a way to hide the 'side-menu' section on mobile screens using CSS. Any suggestions? <div className='side-menu'> <SiderComponent /> < ...

Mastering HTML5 Video: A guide to smoothly playing multiple videos in a loop fashion

Although similar questions have been raised in the past, none has successfully addressed this particular issue; they only provide partial solutions. The objective is as follows: We possess a collection of videos referred to as video1, video2, video3, vid ...

The absence of a closing parentheses in the argument is causing an issue when rendering

Apologies for the basic mistake, but I could really use some assistance with this syntax error. I've spent over an hour searching for it and still haven't been able to locate the issue. It seems to be around the success function(data) section. Th ...

Is there a way to prevent mouse wheel scrolling on a React "number" input field?

Currently, I am encountering an issue with MUI type="number" textfields. When I focus on the field and then scroll the mousewheel, the input value changes unexpectedly. I have looked into adding a blur function after scrolling to prevent this, but I would ...

Is there a way to dynamically change select2 styling using code when a form is submitted?

Having trouble highlighting empty select2 selects when a form is submitted? I'm struggling to override my existing CSS styling upon document load. Check out my jQuery attempt: var $requiredUnfilledItems = $(".required:not(.filled)"); if ($requiredUn ...

Creating a search field in Bootstrap 4 that emulates material design, facing challenges with transition animations

I tried to create a material design search field using Bootstrap form input. While I was able to make it work, I noticed a small issue with the focus state. When I click inside the field, the underline animation runs smoothly. However, when I click outside ...

Step-by-step guide on setting up a click counter that securely stores data in a text file, even after the

Can anyone help me make this link actually function as intended? Right now it only runs the JavaScript code, but I would like it to run the code and redirect to a webpage. Additionally, I need the data to be saved to a text file. Please provide assistanc ...

Guide to verifying a value within a JSON object in Ionic 2

Is there a way to check the value of "no_cover" in thumbnail[0] and replace it with asset/sss.jpg in order to display on the listpage? I have attempted to include <img src="{{item.LINKS.thumbnail[0]}}"> in Listpage.html, but it only shows the thumbna ...

Adjusting the gap between TableRows in Material-UI

Is there a way to increase the spacing between TableRow MaterialUI components in my code? <S.MainTable> <TableBody> {rows.map(row => { return ( <S.StyledTableRow key={row.id}> <TableCell component="th" s ...

Manipulating child classes using jQuery

I am struggling to display an X icon next to a tab on my page when the tab is clicked, but I am facing difficulties in toggling its visibility. The issue arises when trying to access the span element within the tabs. $('.tabs .tab-links a').on(& ...

Develop an XML document that includes CSS and DTD seamlessly incorporated

Could someone please provide me with a short code example of an XML file that includes both a DTD and CSS styles all in one file? Just need one element as an example. I'm new to XML and can't seem to find any examples with both XML and CSS comb ...

Overflow of nested item within flex container

I'm trying to make the content under "foo" fully scrollable, but I've been unsuccessful using flexbox. I attempted it with min-height, yet it didn't work out. Check out this JSFiddle for reference $('.ui.dropdown') .dropd ...

Tips for ending a page prematurely

I am currently facing an issue with hosting images on my webpage where the bottom of the images are uneven, creating a messy look at the bottom of the page. Normally I would attempt to align them in a perfect square layout, but since the page needs to be ...