CSS and HTML mouseover effects for hover interactions

My current code presents an issue where hovering over "About Us" opens all the dropdown menus below. How can I modify the CSS so that each menu only appears when individually moused over? For example, when hovering over "Team," only the menus under it should be visible.

Additionally, I would like to adjust the width of the menu to shift it more towards the left.

Another problem arises when a dropdown menu is lengthy, causing it to hide beneath other content on the page. I want the dropdown menu to appear above the rest of the page's content instead of being hidden.

Thank you in advance for your help!

<style>
ul {
  font-family: Arial, Verdana;
font-size: 14px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul { display: none; }
ul li a {
  display: block;
  text-decoration: none;
  color: #ffffff;
  border-top: 1px solid #ffffff;
  padding: 5px 15px 5px 15px;
  background: #000061;
  margin-left: 1px;
  white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover ul {
  display: block;
  position: absolute;
}
ul li:hover li {
  float: none;
  font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
</style>

<body>
<ul id="menu">
  <li><a href=""><b>Home</b></a></li>
  <li><a href=""><b>About Us</b></a>
    <ul>
    <li><a href="">Team</a>
    <ul>
        <li><a href="">Profile</a></li>
        <li><a href="">Board</a></li>
    </ul>
    </li>
    </ul>
  </li>
<ul>
</body>

JSFiddle: http://jsfiddle.net/LWEry/

Answer №1

Just like this:

ul {
  font-family: Arial, Verdana;
font-size: 14px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul { display: none; }
ul li a {
  display: block;
  text-decoration: none;
  color: #ffffff;
  border-top: 1px solid #ffffff;
  padding: 5px 15px 5px 15px;
  background: #000061;
  margin-left: 1px;
  white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover > ul {
  display: block;
  position: absolute;
  width: 100%;
}
ul li:hover li {
  float: none;
  font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }

.sub-menu
 {
    position: absolute;
    top: 0;
    left: 100%;
 }

http://jsfiddle.net/3xWcu/2/

I made a slight modification to one of the selectors.

FROM

li:hover ul

TO

li:hover > ul

Check out my updated fiddle. I included a sub-menu class in the ul element that contains the Profile and Board li tags:

 <ul class="sub-menu">
    <li><a href="">Profile</a></li>
    <li><a href="">Board</a></li>
 </ul>

and also added some CSS above.

Answer №2

Is this what you're looking for? http://jsfiddle.net/3xWcu/

<style>
ul {
  font-family: Arial, Verdana;
font-size: 14px;
  margin: 0;
  padding: 0;
  list-style: none;
}
ul li {
  display: block;
  position: relative;
  float: left;
}
li ul { display: none; }
ul li a {
  display: block;
  text-decoration: none;
  color: #ffffff;
  border-top: 1px solid #ffffff;
  padding: 5px 15px 5px 15px;
  background: #000061;
  margin-left: 1px;
  white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover ul {
  display: block;
  position: absolute;
  width: 100%;
}
ul li:hover li {
  float: none;
  font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
</style>

<body>
<ul id="menu">
  <li><a href=""><b>Home</b></a></li>
  <li><a href=""><b>About Us</b></a>
    <ul>
    <li><a href="">Team</a>
    <ul>
        <li><a href="">Profile</a></li>
        <li><a href="">Board</a></li>
    </ul>
    </li>
    </ul>
  </li>
<ul>
</body>

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

Unable to vertically align an image within a navigation bar item that is not the brand

Having a unique issue for which I cannot find a solution. In the navbar of Bootstrap 4, I am attempting to include an image next to one of the items, not the navbar-brand as commonly asked about. When Bootstrap 4 is loaded with a custom alpha dark theme, ...

What methods can be used to prevent the page body from resizing when additional elements are added?

I am having an issue with setting up the layout of my webpage. I want the body to take up 100% of the screen size, and within the body there is a mainContainer div that should be 90% of the body's dimensions. The problem arises when I try to add a di ...

Retrieving the first and last names of users by joining the friends table

After successfully retrieving the friend's ID for user 75, I'm facing a challenge in selecting users.firstname and users.lastname from the users table for these friend IDs. Removing the IF statement, as suggested by a friend, is not an option as ...

:after functionality not operating properly in Internet Explorer

The titles on this page have arrows displayed before them using a special styling. However, there seems to be an issue with how it displays on Internet Explorer. // edit : I made a mistake, my styling is actually on paragraph tags 'p'. Maybe tha ...

Is there a way to efficiently access checkboxes by their IDs and toggle classes without the need for redundant classes and functions?

In my current project, I have set up functionality to toggle classes on a table for hiding specific columns. This is determined by checkboxes selected by the user above the table, each checkbox having its own unique ID like "product_1", "product_2", and so ...

Add information to MySQL using PHP with expiration date

Having an issue here - I am trying to incorporate an expiration date into a specific row of my database table using HTML and PHP, but something seems to be wrong. The row is not getting deleted when the expiration time passes. Also, I was wondering if it ...

Tips for consistently showing the addition symbol in my search bar

I created a code snippet that showcases a search box with CSS animations and jQuery toggling functionality. One issue I encountered is ensuring that the plus icon remains visible at all times, whether the search box is expanded or contracted. How can I ac ...

Simple HTML and CSS exercise for beginners to grasp the concept

I'm looking to modify the layout of a specific webpage at based on the following instructions: First, insert this link within the <head> <link rel="stylesheet" href="https://trafficbalance.io/static/css/sdk/sdk.css"> -next, add th ...

Spinning a CSS object around an axis

Trying to add a unique touch to my image rotation with CSS. While familiar with rotating around x, y, and z axis, I want to achieve a diagonal rotation this time. Wondering if there's a way to tweak the y axis for a more slanted effect on my image? ...

Sharing information from child to parent controller in AngularJS through data push

I encountered an issue while attempting to push a cities object to an array in the parent controller. The error message displayed was "Cannot read property 'push' of undefined". Is there a solution to this problem? The ChildCtrl is embedded with ...

Guide on how to pre-load custom fonts using webpack and rails 6

How can I efficiently preload a custom font in my Rails 6 application using webpack? Here is the font setup in config/_fonts.scss : @font-face { font-family: 'customfont'; src: font-url('customfont.eot?81236734'); src: font-url(& ...

Execute a zoom out action by pressing the (Ctrl) and (-) keys simultaneously in Javascript

I'm trying to figure out how to simulate a Ctrl - zoom out using Javascript. I've noticed that using the style zoom property or the transform property gives different results with white space in the corners, rather than the smooth zoom out effect ...

Breaking text with overflow-wrap and <br> tag

Hello, I have successfully implemented a content editable div. However, I am looking to automatically insert a <br> tag whenever the text overflows the width of the content-editable area. Essentially creating a line break whenever there is an overf ...

What is the best way to invoke an HTML file using a jQuery function in a JavaScript file?

When I call an HTML file from a jQuery function using require(), I am encountering an issue where I am unable to link the CSS with the HTML file. After running the file, only plain HTML is displayed without any styling. The structure of my HTML file is as ...

How to retrieve the background color of text on a webpage

My website features both primary and secondary search options. When a search term is entered, the website highlights the first term in blue and the second term (or "search within" term) in purple. This highlighting is typically done using java script on t ...

Simple Timer App with Vanilla JavaScript

Hey there! I am a newcomer to JavaScript and recently joined the stackoverflow community. Currently, I am working on a project called Pomodoro Timer with the goal of creating something similar to this example: http://codepen.io/GeoffStorbeck/full/RPbGxZ/ ...

Is it possible to transfer a 2D array between pages using JavaScript?

As I venture into the world of JavaScript, I am curious about ways to transfer specific data from the main page to the product page when a user clicks on an item. I prefer not to use PHP or any other programming language besides JavaScript. Although I tri ...

Make the child element's width equal to the parent element's height using only CSS

Is it possible to make the width of a child-div match the parent-div's height using only CSS? Check out this JsFiddle Demo Typically, I would use jQuery like this: $("#child-div").width($("#child-div").parent().height()); But I really need to ac ...

Why does one of the two similar javascript functions work while the other one fails to execute?

As a new Javascript learner, I am struggling to make some basic code work. I managed to successfully test a snippet that changes text color from blue to red to ensure that Javascript is functioning on the page. However, my second code attempt aims to togg ...

How can we assign dynamic unique IDs to HTML elements within a for..of loop in TypeScript?

Within my array, I am using a for..of TypeScript loop to dynamically add identical HTML elements. Each element needs to have a unique ID. Is it feasible to achieve this directly within the for..of Typescript loop? The code snippet is as follows: for (le ...