nav menu won't expand - html

I have a vertical navigation menu and I want to show different levels of the menu upon hovering of certain elements. The problem is that the method I used is not working and I do not understand why. When I hover over "Product", I expect to see a sub-menu expand, but nothing happens. Why?
HTML:
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Product</li>
<ul>
<li>Blueberries</li>
<li>Rasberries</li>
<li>Strawberries</li>
</ul>
<li>Contact</li>
</ul>
</nav>
CSS:
nav {
border:1px solid red;
}
nav ul ul {
display:none;
}
nav ul li:hover > ul {
display:block;
}

Your code:
nav ul li:hover > ul {
display:block;
}
Means "Make any ul within a hovered li display:block". Your submenu is not within the LI, it's after it. Here's a working version of what you were trying to do.
Working HTML:
<li>Product
<ul>
<li>Blueberries</li>
<li>Rasberries</li>
<li>Strawberries</li>
</ul>
</li>
Working CSS:
nav ul li ul {
display:none;
}
nav ul li:hover ul {
display:block;
}

Also
nav ul ul {
display:none;
}
should be
nav ul li ul {
display:none;
}

Try this for your html:
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Product
<ul>
<li>Blueberries</li>
<li>Rasberries</li>
<li>Strawberries</li>
</ul>
</li>
<li>Contact</li>
</ul>
</nav>

You have two ways of changing this; you can either update the HTML or you can update the CSS.
There are pros and cons to changing code and in a vacuum I can't recommend one approach over the other.
Without changing your HTML you can make the CSS work like this:
nav ul li:hover + ul {
display: block;
}
Note that rather than using the descendant selector this uses the adjacent selector and applies the style to the element that immediately follows the hovered LI.
Alternatively, the HTML change mentioned above does work equally well.
This link http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ provides a fantastic resource.

Related

How to display list on button hover?

I am trying to display the list on button hover, but what happens is, whenever I hover near the button area, text gets displayed.
.header_nav {
width: 800px;
height: 400px;
}
.header_nav ul {
list-style: none;
}
.header_nav ul ul {
display: none;
}
.header_nav ul ul #nav_button:hover>ul {
display: block;
}
.header_nav ul ul li >ul {
display: none;
}
.header_nav ul li:hover >ul {
display: block;
}
<nav class="header_nav">
<ul>
<li>
<input type="button" value="Button 1" name="nav_button" id="nav_button">
<ul>
<li>Locations</li>
<li>
Mumbai
<ul>
<li>Txt 1</li>
<li>Txt 2s</li>
<li>Txt 3</li>
</ul>
</li>
<li>Delhi</li>
<li>Banglore</li>
<li>Nagpur</li>
</ul>
</li>
</ul>
</nav>
JS FIDDLE : https://jsfiddle.net/fhv7drst/
It is because your li element was block element.
I changed it to inline and it started working as per your requirements
HTML:
<li class="parentElement">
<input type="button" value="Button 1" name="nav_button" id="nav_button">
CSS:
li.parentElement{
display: inline;
}
here is the working fiddle
https://jsfiddle.net/m73p8pea/
The reason is that your li element is a block element, which means that it will automatically try to span the entire width available. In your case, this is the 800px provided by the topmost element.
You have two solutions readily available - one is to make the list element an inline-block element (or simply an inline element, though I'd prefer inline-block here, as block is how it started) to prevent it spaning the whole width:
.header_nav ul li {
display: inline-block;
}
You could also trigger the display change on the unordered list when hovering over the button directly, not when hovering over it's parent list item:
.header_nav #nav_button:hover + ul {
display: block;
}
This is likely the better solution, as it doesn't mess with the display types more than you need to, and you more accurately describing what you want to happen - show the list when the button is hovered.
As pointed by #Rahul Arora indeed it is because of li as block element.
But if for some reason you still want to keep it as block element, you can keep it by making it as inline-block. I also recommend removing margin (see your given example by inspect element, it is to the write of ul) and padding which is assigned by browser as default.
Here is the code:
.header_nav
{
width:800px;
height:400px;
}
.header_nav ul
{
list-style:none;
//displaying ul & all its child as inline block until overriden by other rules
display:inline-block;
//removing default margin and padding
margin:0;
padding:0;
}
.header_nav ul ul
{
display:none;
}
.header_nav ul ul #nav_button:hover>ul
{
display:block;
}
.header_nav ul ul li >ul
{
display:none;
}
.header_nav ul li:hover >ul
{
display:block;
}
<nav class="header_nav">
<ul>
<li>
<input type="button" value="Button 1" name="nav_button" id="nav_button">
<ul>
<li>Locations</li>
<li>
Mumbai
<ul>
<li>Txt 1</li>
<li>Txt 2s</li>
<li>Txt 3</li>
</ul>
</li>
<li>Delhi</li>
<li>Banglore</li>
<li>Nagpur</li>
</ul>
</li>
</ul>
</nav>

How to change text color on OnMouseHover in a Dropdown Menu

I have a dropdown menu.I want the background and text color to get changed as soon as mouse hover is performed .At present background color is getting changed correctly whereas i am not able to change the text color ..
Here is the fiddle ...
Fiddle
and Here is the HTML...
<nav>
<ul>
<li>ABOUT US
<ul>
<li>Photoshop </li>
<li>Illustrator </li>
<li>DreamViewer </li>
<li>Web Design
<ul>
<li>HTML</li>
<li>CSS</li>
</ul>
</li>
</ul>
</li>
</ul>
Please help me to solve this..
Just add this css color: #000; in class nav ul ul li a:hover .
nav ul ul li a:hover{
color: #000;(added css)
background: #e6e6e6;
}
Hope it'll help you.
nav ul ul li a:hover {
background: #e6e6e6;
color:#INSERT HEX;
}
you just need to add below code.
nav ul li ul li:hover > a {
color: #f00;
}
DEMO HERE

using css set hover in ul list

this is my html file:-
<div id="load">
<ul>
<li>Activities
<ul>
<li>Physical1
<ul>
<li>Cricket
<ul>
<li>One Day</li>
</ul>
</li>
</ul>
</li>
<li>Test1
<ul>
<li>Test At Abc</li>
</ul>
</li>
<li>Test2
<ul>
<li>Test At Xyz</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
i want to set in to css hover.
i dont konw about css hover much so give me suggestion for this.
this is my output i needed.
hover in Acvivities display
Physical1
Test1
Test2
hover in Physcial1 display Cricket...
thanks...
ul > li > ul {
display: none; // hide all submenus
}
li:hover > ul {
display: block; // show sub menu when parent li is hovered
}
DEMO
ul li ul
{
display:none;
}
ul > li:hover > ul
{
display:block;
}
http://jsfiddle.net/AyLYe/
I won't provide the whole code, but with this base you can adapt the code to display the rest of what you need.
Before we start, see this tutorial I found with a google search: http://line25.com/tutorials/how-to-create-a-pure-css-dropdown-menu I scanned it at a glance and it looks pretty good, it will at least guide you through the concepts and the process.
Here's a JSFiddle:http://jsfiddle.net/Kq7vD/
Heres the CSS that makes it work:
#load ul li ul {
display: none;
}
#load ul li:hover ul {
display: block;
}
Note that by removing #load you can cause this to work across every list in your menu. The downside to this is that the css rules then apply to every list on your site, even if it shouldn't be a menu. It is recommended that you keep your rules relatively specific for this reason.
EDIT to address your comment:
If your HTML structure includes a DIV before each UL, even the nested UL's then your css rules will need to adapt to that new structure. In particular, it's also important to note that you will not set the UL to display: none/block; anymore but the DIVs.
Assuming a structure like:
<div id="load">
<div>
<ul>
<li>
<div>
<ul>
<li></li>
</ul>
</div>
</li>
</ul>
</div>
</div>
Your code would then look like...
#load div ul li div {
display: none;
}
#load div ul li:hover div {
display: block;
}

Create second level dropdown using css

I am trying to create a second level dropdown. I successfully created first level dropdown but bit stuck in making it level 2. Please assist me to complete it..
and also please explain me what mistake I am doing that I cant get the second level dropdown even the css part is good (I think so)
EDIT: I know there are many tutorials on dropdown css. But I want to know why this is not working.
Here is the link to jsbin
HTML
<ul id="nav">
<li>Home</li>
<li>Details
<ul id="subNav">
<li>x details<li>
<li>y details</li>
</ul></li>
<li>About Us
<ul id="xSubNav">
<li>About company
<ul>
<li>full information</li>
<li>summary</li>
</ul></li>
<li>About Author</li>
</ul></li>
</ul>
CSS
*{font-family:consolas;}
li{line-height:20px;}
ul#nav>li{float:left;width:100px;list-style:none;
cursor:hand;cursor:pointer;}
ul#nav li li
{display:none;width:150px;}
ul#nav li ul
{padding:0;margin:0;}
ul#nav>li:hover>ul>li
{display:block;}
ul#nav>li:hover{color:grey;}
ul li li{color:black;}
ul li li:hover
{color:cornflowerblue;}
ul li li:hover li /* level 2 dropdown part */
{display:block;margin-left:150px;width:300px;}
Here is solution with your code
Just add the below css:
ul ul li { position:relative;}
ul ul li ul { position:absolute; display:none; left:0px; top:0px;}
ul ul li:hover ul { display:block;}
ul#nav li li li {display:block;}
Check this working fiddle
The problem is the specificity of CSS rules. Just add #nav to the last three rules, to not get overridden by the first ones.
ul#nav li li{color:black;}
ul#nav li li:hover
{color:cornflowerblue;}
ul#nav li li:hover li
{display:block;margin-left:150px;width:300px;}
And I think some other tuning is needed, but that's the idea.

CSS Dropdown Menu

I'm looking for advice on creating a drop down menu using Unordered Lists. I have managed to get a horizontal unordered list by using the following CSS and HTML. I'd like to make it so on some of the menus a drop down menu appears.
I've tried multiple different methods and haven't managed to get it right.
CSS
#nav li {
display: inline;
padding-right: 22px;
vertical-align: middle;
text-align: center;
}
#nav li a {
color: #f9f7ee;
background-image:url(images/bullet.gif);
background-repeat:no-repeat;
padding-left: 16px;
text-decoration: none;
}
#nav li a:hover {
background-image:url(images/bulletsolid.gif);
background-repeat:no-repeat;
padding-left: 16px;
color: #f9f7ee;
}
HTML
<ul id="nav">
<li>About</li>
<li>Teaching</li>
<li>Performing</li>
<li>Media</li>
<li>Blog</li>
<li>Links</li>
<li>Contact</li>
</ul>
This isn't too hard with css involving nesting your unordered lists and hiding the nested ones until the parent li is hovered. Here's a quick example if you wanted drop down below About. your html for it would look like so.
<ul id="nav">
<li>About
<ul>
<li>drop down below about 1</li>
<li>drop down below about 2</li>
</ul>
</li>
</ul>
#nav ul {
display: none;
}
#nav li:hover > ul {
display: block;
}
This will hide the child ul until the parent ul's li is hovered basically.
If your interested in building accesibile CSS check out a list apart
http://www.alistapart.com/articles/horizdropdowns/
There one of my favourites sites for good standards and proper CSS foundation