Issues with the Dropdown Menu

I'm currently working on a dropdown menu and I've encountered two issues.

  1. When I hover over the links in the main navigation bar (such as 'about' and 'connect'), only the actual words are clickable, not the entire area that the text occupies like with 'works' and 'achievements.'

  2. In the dropdown menu itself, when I hover over the links, the entire section of text is not being highlighted.

I've attempted to resolve this by adjusting padding and margins but it hasn't completely fixed the problem.

This is the CSS code I'm using:

/*CSS CODE HERE*/ <br/>

And here's my HTML markup:

/*HTML CODE HERE*/ 

You can view my code preview via this link: https://jsfiddle.net/zinctan/83jh28o4/1/.

Answer №1

Here are the main causes of the issues:

  1. The padding should be added to the link itself, not the li element.
  2. Similarly to the previous issue, you also need to include width:100% in the links for them to occupy the full width.

Additionally, there is another problem: You forgot to insert <a> elements for the li's that contain dropdowns. These links should be included like the others.

Check out the DEMO

Answer №2

Make sure to add padding to the links:

#nav ul li a {
  display: block;
  padding: 7px 10px;
}

Additionally, the <li> elements on the same level must have an <a> tag for it to function properly, like so:

<a href="#" class="navText">WORKS</a>

In this section of the code, adjust the width to 140px (subtracting 10px of padding on each side from the total 160 pixels):

#nav ul li ul li a:hover {
    width: 140px;
}

Visit this link for more information.

Answer №3

body {
background-color: #FFFFF5;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#nav {
width: 480px;
float: right;
height: 30px;
border-top: thin solid #000000;
line-height: 0px;
text-align: right;
padding-right: 20px;
}
.navText {
color: #000000;
text-decoration: none;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 17px;
}
#nav ul {
text-align: left;
display: inline;
list-style: none;
padding-top: 0px;
padding-right: 0px;
padding-bottom: 0px;
padding-left: 0px;
}
#nav ul li {
font: 17px/17px sans-serif;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
display: inline-block;
position: relative;
cursor: pointer;
padding-top: 7px;
padding-bottom: 7px;
padding-left: 10px;
padding-right: 10px;
}
#nav ul li a:hover {
background: #555;
color: #fffff5;
display: inline-block;
position: relative;
cursor: pointer;
}
#nav ul li:hover {
background: #555;
color: #fffff5;
}
#nav ul li ul {
position: absolute;
top: 30px;
left: 0px;
opacity: 0;
visibility: hidden;
-webkit-transiton: opacity 0.2s;
-moz-transition: opacity 0.2s;
-ms-transition: opacity 0.2s;
-o-transition: opacity 0.2s;
-transition: opacity 0.2s;
width: 160px;
}
#nav ul li:hover ul {
display: block;
opacity: 1;
visibility: visible;
}
#nav ul li ul li {
background: #555;
display: block; 
color: #fffff5;
font: 13px sans-serif;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
#nav ul li ul li a {
text-decoration: none;
background: #555; 
display: block; 
color: #fffff5;
font: 13px sans-serif;
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
}
 #nav ul li ul li:hover {
background: #666;
}
#nav ul li ul li:hover a {
    background: #666;
}
</style>
     <div id="nav">
      <ul>
        <a href="about.html" class="navText"><li>ABOUT</li></a>
        <li>WORKS
        <ul>
      <li><a href="written.html">Written</a></li>
      <li><a href="photos.html">Photography</a></li>
         <li><a href="film.html">Film</a></li>
         <li><a href="other.html">Other</a></li>
  </ul>
    </li>
        <li>ACHIEVEMENTS
        <ul>
      <li><a href="skills.html">Skills</a></li>
         <li><a href="acad.html">Academic</a></li>
         <li><a href="cca.html">Co-Curricular</a></li>
  </ul>
        </li>
        <a href="connect.html" class="navText"><li>CONNECT</li></a>
      </ul>
    </div>

1: Modify the location of anchor tags with list items:

<a href="about.html" class="navText"><li>ABOUT</li></a>
…
<a href="connect.html" class="navText"><li>CONNECT</li></a>

2: Update the CSS rule for hovering on nested unordered list items:

CSS

#nav ul li ul li:hover {
  background: #666;
}

Also, append the following style rule:

#nav ul li ul li:hover a {
  background: #666;
}

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

Experiencing problems with CSS compatibility while transitioning from vanilla JavaScript to React

Currently, I am working on revamping the frontend of an app that was initially built with vanilla javascript and transitioning it to a React framework. However, I'm encountering some challenges with styling the HTML elements. Specifically, the textare ...

Adjust the color of the label when the checkbox is marked, and make it red when the checkbox is left

Looking to create a customized checkbox due to challenges in styling the default HTML checkbox, I have attempted the following code. The checkbox functions properly when clicking on the label, but I am unable to change the border color of the label based o ...

Employing jQuery to add an element as a sibling rather than a child node

I'm having trouble finding the specific functionality I need. My goal is to add sibling DOM elements to a disconnected node. From what I gather, it should be possible with either .after() or .add(), but for some reason both methods are not working as ...

Sending JSON Data over URL

I am facing a challenge with passing data between two HTML files. My initial solution involved sending the data through the URL using the hash and then parsing the link using JSON.parse(window.location.hash.slice(1));. This method worked for a few attempts ...

CSS - image does not adhere to max-height when placed in a fluid container - link to jsFiddle

For instance: http://jsfiddle.net/vjqjv/7/ I am facing an issue with an image inside a container (refer to #slider-section in the jsFiddle) that has a fluid height and a set max-height. I have defined max-height and max-width for my image as 100% (check ...

eliminate the script and HTML comment from a designated division

Here is the default code that I need to modify using JavaScript to remove scripts, comments, and some text specifically from this div: I came across this script that removes all scripts, but I only want to remove specific ones from this div: $('scri ...

How can I dynamically generate multiple Reactive Forms from an array of names using ngFor in Angular?

I am in the process of developing an ID lookup form using Angular. My goal is to generate multiple formGroups within the same HTML file based on an array of values I have, all while keeping my code DRY (Don't Repeat Yourself). Each formGroup will be l ...

What are the reasons for the decrease in canvas text quality when using requestAnimationFrame?

I have created a unique canvas effect where text changes color using requestAnimationFrame: var text = 'Sample text'; ctx.fillText(text,canvas_width/2,100); requestAnimationFrame(animate); function animate(time){ ctx.fillText(text,-offset,100 ...

Saving HTML elements within an XML file

I am dealing with a String that has an html format containing various html tags. My goal is to enclose this string within xml tags while preserving the html tags, for example: public class XMLfunctions { public final static Document XMLfromString(Str ...

Make sure to employ Bootstrap's justify-content-between and stack-to-horizontal col correctly

I am currently working on a responsive form that contains 1 label, 1 input field, and 5 buttons. Both the buttons and the input field are located under col-8. The positioning I aim for the buttons is as follows: They should start below and align with ...

I encountered an issue while attempting to link my project to a database. An error popped up stating that the name 'textbox' does not exist in the current context

I encountered an error when trying to connect my project to a live database. The error message stated: the name ‘textbox’ does not exist in the current context. How can I resolve this issue? Below is the aspx.cs code that I used for the connection, wh ...

Can IE(7?) cause distortion of backgrounds from sprites?

This task is giving me a headache. We're almost finished with revamping our website. The last step involves consolidating all the glyphs and icons into a sprite. These images are transparent .png files, so we made sure the sprite maintains transparen ...

Turn off Chrome 69's autofill functionality

I've recently encountered an issue with Chrome's password autofill feature that has been troubling me for a few days now. This problem began when I was using Chrome version 69. After much trial and error, I found a solution by removing the id an ...

Accordion checkbox with dynamic features

Currently, I am dynamically populating data into a jQuery accordion. My goal is to include a checkbox just before the <h2> text. <div id="checkbox"> <h2> <span> <input type="checkbox" class="mycheck" value="apple" / ...

Navigating through a multistep form in AngularJS using UI Router and arrow keys for seamless movement

Is there a way to navigate to the next or previous form step using arrow keys in AngularJS UI Router? The code provided below is currently allowing navigation with previous and next buttons. .config(function($stateProvider, $urlRouterProvider) { $stat ...

The inclusion of a content editable feature within a carousel is leading to unexpected event propagation

I am dynamically creating an editable div using the code snippet below. <div class='reflection-field' contenteditable="true" data-number="${index}"></div> Expected Outcome: When I click on the generated div, I anticipate that the c ...

Modifying the Header Background Color

Looking for help on my forum at The header background on my site needs fixing. I am trying to change the entire header background to match the color of the logo background, but I'm unsure what codes need to be adjusted. Can anyone provide guidance? ...

Is it possible to accomplish this using a list or a table in HTML/CSS?

I am currently working on creating a visual representation of a color catalog using hyperlinked images. My goal is to have a layout that displays the colors in a grid format, similar to this: However, the number of images per row may vary depending on the ...

Adjust the stroke and fill colors of an SVG element when hovering over it

I am facing a challenge with an SVG image that I have: https://i.stack.imgur.com/r4XaX.png When hovered over or clicked, it should change to https://i.stack.imgur.com/EHRG2.png Current Icon <svg width="24" height="24" viewBox="0 0 24 24" fill="non ...

Displaying the numeric value from a MySQL database in a textbox

In my code, I utilize the following segment to populate a database table where each row contains a button with the label "Load". while($row=mysqli_fetch_array($result)) { echo" <tr id='rowNum'> <td >".$row['No.'] ...