CSS dropdown activated by hovering over the incorrect element

Having a CSS dropdown menu works great, except for one pesky problem. The dropdown triggers when hovering over the container itself, rather than just the link. Check out this jsfiddle with the code: http://jsfiddle.net/9BRac/

<div class="nav-con">
  <nav role='navigation' id="nav">
    <ul>
      <li id="linked"><a href="#">Home</a></li>
      <li><a href="#">About</a>
          <ul class="dropdown">
            <li>Link 1.</li>
            <li>Link 2.</li>
            <li>Link 3</li>
          </ul>  
      </li>
      <li><a href="#">Clients</a>
          <ul class="dropdown">
            <li>Link 1.</li>
            <li>Link 2.</li>
            <li>Link 3</li>
          </ul>  
      </li>
      <li><a href="#">Contact Us</a>
          <ul class="dropdown">
            <li>Link 1.</li>
            <li>Link 2.</li>
            <li>Link 3</li>
          </ul>  
      </li>
    </ul>
    </nav>


.clearfix:before,  
.clearfix:after {  
    content: " ";  
    display: table;  
}  
.clearfix:after {  
    clear: both;  
}  
.clearfix {  
    *zoom: 1;  
}  

nav {
  width: 100%;
  height: 60px;
  background-color:black;
  border-bottom: #ffd600 solid 10px; 
  margin: 0 auto;
}

nav ul {
  position:inheirt;
}

nav ul li {
    transition: all .6s ease-in-out;
  -moz-transition: all .6s ease-in-out;
  -o-transition: all .6s ease-in-out;
  -webkit-transition: all .6s ease-in-out;
  width: 80px;
  height:60px;
  margin-left:10px;
  display:;
  float: left;
  line-height: 60px;
  text-align:center;
  list-style:none;
  position:inherit;
  color: white;
  text-decoration:none;
}

 nav ul li:hover {
  width: 80px;
  -moz-transition: all .6s ease-in-out;
  -o-transition: all .6s ease-in-out;
  -webkit-transition: all .6s ease-in-out;
  height:60px;
  margin-left:10px;
  background-color:#ffd600;
  float: left;
  line-height: 60px;
  text-align:center;
  list-style:none;
  position:inherit;
  color: white;
  text-decoration:none;
}

nav ul li a {
  color: white;
  text-decoration:none;
}

html {
  background-color: #f2f2f2;
}

#dropdown {
    width:100%;
  height:200px;
  opacity: 0.8;
  background-color:black;
    display:none;
}


.dropdown {
  margin-top:10px;

}
.dropdown li {
  width: 300px;
  background-color: #ffd600;
}

nav ul ul {
    opacity: 0;
  transition: all .6s ease-in-out;
  -moz-transition: all .6s ease-in-out;
  -o-transition: all .6s ease-in-out;
  -webkit-transition: all .6s ease-in
}

nav ul li:hover > ul {
        opacity: 0.8;
    transition: all .6s ease-in-out;
    -moz-transition: all .6s ease-in-out;
    -o-transition: all .6s ease-in-out;
    -webkit-transition: all .6s ease-in-out;
    }

nav ul ul li {
  opacity: 1.0 !important;
}

nav ul ul li:hover {
  width: 350px !important;
}

Answer №1

Make sure to utilize the following code snippet

/* Customize your dropdown */
nav ul ul {
    opacity: 0;
    visibility:hidden;
    transition: opacity .6s ease-in-out;
    -moz-transition: opacity .6s ease-in-out;
    -o-transition: opacity .6s ease-in-out;
    -webkit-transition: opacity .6s ease-in
}

/* Implementation for displaying your dropdown */
nav ul li:hover > ul {
    opacity: 0.8;
    visibility:visible;
}

If you need an interactive example, check this out http://jsfiddle.net/DanielApt/9BRac/12/

Description: Your .dropdown remains present, however with an opacity of 0.

Whenever you hover over this "hidden" .dropdown, it activates the hover style for its parent li, causing the .dropdown to reappear.

The solution is to specify a visibility of hidden for your .dropdown, and switch the visibility to visible only when hovering over the parent li

In addition, exclusively apply the transition effect on the opacity attribute, so that visibility changes immediately while opacity transitions smoothly.

Note: The reason for avoiding display:none is due to the impossibility of transitioning display properties (reference for using visibiliy instead)

Answer №2

Consider utilizing visibility:hidden instead of modifying just the opacity on hover, as demonstrated in this example:

http://jsfiddle.net/3YFTL/22/

Adjust the transition timing to enhance the smoothness of the effect.

Moreover, it may be beneficial to apply the transition property solely to opacity rather than all properties.

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 with Flowbite dropdown functionality in Next.js 12.* with Tailwind CSS

Hello, I am a newcomer to Next.js and currently working on a project using Tailwind/Flowbite. I copied and pasted the Flowbite search input with dropdown code from , but unfortunately, the dropdown is not functioning as expected. I have followed the docum ...

What is the method for displaying the props value in console.log?

Recently, I started learning React and was tackling a simple project. Below is the code snippet that I have been working on. While working on it, I came across a situation where I needed to print the value of listItem={people} which is passed as props in ...

In this scenario, why is the `position: fixed` property anchored to the document rather than the viewport?

I have a gallery set up with a custom lightbox feature that I designed. The lightbox is styled with position:fixed and top:0, so theoretically it should stay in the same position regardless of scrolling. However, I am experiencing issues where this is not ...

Is there a way to set a value for an attribute in a form post submit button click?

I need to update the value of a form when the user selects "null" <form name="myForm" action="formProcess.php" method='post' onSubmit="return validateForm()"> <label for="venue">Venue:</label> <select name="venue"> ...

Designing Your WordPress Website's Header Structure

When working on my non-WP sites, I usually handle columns using Bootstrap. However, this is my first time delving into CSS positioning without it. I am trying to align my site header elements similar to the image shown, where the text elements are flush ri ...

Developing a timetable feature using PHP

I am working on creating a schedule page that involves an SQL table named 'appointments' with columns for id, name, date, and time. My goal is to organize the data from this table into an HTML/CSS based table where all records with the same date ...

Is it possible to reroute using an ESP8266 captive portal?

I'm having trouble creating an html form in an esp8266 captive portal. It seems like the escape characters are causing issues. I've exhausted all possible options I could find. I've been using the backslash (\) to escape and even attem ...

Image that floats or pops over the content

I'm currently working on designing a card similar to the one displayed in the image. However, I'm facing difficulty in creating the floating image effect on the card. Any tips or suggestions on how to achieve this using angular, jquery, css, or a ...

Enhanced browsing experience with personalized search suggestions

As a developer, I am aware that you have the ability to customize the browser's suggestions for various fields. For example, specifying that a field is for email and only suggesting values for email fields in the future. Is this functionality really ...

Modify a CSS style with JavaScript or jQuery

Can CSS rules be overwritten using jQuery or JavaScript? Imagine a table with rows categorized by different classes - "names" for persons and "company" for companies. How can we efficiently hide or show specific rows based on these classes? If we have a ...

Tips for overflowing an image within an Ionic toolbar

I am attempting to adjust the image overflow within the toolbar using the following code: ion-header { background: var(--ion-color-primary); ion-toolbar { --padding-end: 30px; --padding-start: 30px; --min-height: 100px; ion-avatar ...

Why does my jQuery map code work in version 2.0 but not in version 3.0?

I've encountered an error with a jQuery map snippet that I'm trying to troubleshoot. It was working fine under jQuery 2, but after upgrading to version 3, it doesn't work anymore and I can't figure out why. Feeling stuck! var menuIte ...

Retrieve a document from a specified URL using JQuery's Ajax functionality

I have an HTML page with an input type text field. I want to be able to paste a URL, for example , into this text field and then click a button labeled "download" in order to download the linked file onto my computer. I am looking to implement this funct ...

Is there a way to prevent the Material UI Chip component from appearing when there is no content present?

I have developed an application that pulls content from an API and displays it on the screen. Within this app, I have implemented a Material UI Card component to showcase the retrieved information, along with a MUI Chip component nested within the card. &l ...

Adapting the colors of the MUI Switch component

I am currently utilizing the switch component from MUI and I am looking to change the styling between two different colors. <Switch color={dataType === "expenses" ? "error" : "secondary"} onChange={handleOnSwitch} che ...

I keep encountering an issue when trying to load the website <a href="linktext.com">linktext.com</a> using SQL and PHP

I recently encountered an issue while trying to retrieve data from an SQL database on a webpage using PHP. The process was interrupted whenever there was a hyperlink present in the SQL text data, such as linktext.com. I attempted to use HTML special chara ...

Ways to position variable width blocks so they appear to float

I'm facing an issue with floating multiple DIVs and allowing them to break out of a containing div. Below is an example of my code: <!DOCTYPE HTML> <html lang="en-GB"> <head> <meta charset="utf-8" /> <st ...

Is there a way to transform my QML WebEngine application into HTML5 code?

This is a basic application that simply loads webviews for various applications, designed in qml using qt.webengine. However, I am encountering errors due to an outdated web browser. As I do not plan to switch to Qt 6 just yet, I am exploring the possibili ...

Utilize CSS to ensure that images are equal in height to their container element

This is the markup div.shadow | div#content |img.shadow | Is there a way to ensure that the shadow images always maintain the same height as the content area? The challenge lies in the fact that the content area can adjust its size based on different fac ...

The image overlay in the div remains fixed in a large screen size but does not stack when the screen size

Sorry for the not-so-great title, I'm still learning the terminology. I have a situation where a div is covering part of a background image when the screen is wide. However, on smaller screens, I want that div to move below the background image inste ...