How do I make menu retain hover state when submenu is active? - html

What do I need to include in my CSS to get my menu text to stay black when hovering over a submenu?
I've tried systematically going through my current :hover elements, including > when ever possible, and adapting the relevant parts from other codes into the CSS. I've also combed through similar questions in hopes to find applicable code but have not been successful.
#menu {
width: 100%;
position: fixed;
background: #333;
color: #fff;
}
#menu ul.left {
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul a {
display:block;
color:#fff;
text-decoration:none;
padding:0 15px;
}
#menu ul a:hover {
display:block;
color: #333;
text-decoration:none;
padding:0 15px;
}
#menu ul li {
list-style-type: none;
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul li:hover {
background:#f6f6f6;
color:#333;
}
#menu ul ul {
display:none;
position:absolute;
background: #f6f6f6;
color: #333;
}
#menu ul li:hover > ul {
display:block;
}
<nav id="menu">
<ul class="left">
<li>Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</ul>
</li>
</ul>
</nav>
Expected results: hovering from Menu 1 to Sub Menu x will keep the #333 colored text on Menu 1.
Actual results: hovering from Menu 1 to Sub Menu x will revert to #f6f6f6 colored text on Menu 1.

Added a element to the #menu ul li:hover in css.
#menu {
width: 100%;
position: fixed;
background: #333;
color: #fff;
}
#menu ul.left {
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul a {
display:block;
color:#fff;
text-decoration:none;
padding:0 15px;
}
#menu ul a:hover {
display:block;
color: #333;
text-decoration:none;
padding:0 15px;
}
#menu ul li {
list-style-type: none;
position:relative;
float:left;
margin:0;
padding:0px;
}
#menu ul li:hover a{
background:#f6f6f6;
color:#333;
}
#menu ul ul {
display:none;
position:absolute;
background: #f6f6f6;
color: #333;
}
#menu ul li:hover > ul {
display:block;
}
<nav id="menu">
<ul class="left">
<li>Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</ul>
</li>
</ul>
</nav>

Related

Issue in Painting submenu

In following code on hover of menu1 I want to show submenu but it will not work.I applied below css. Please modified the code where I am wrong.
HTML
<div class="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li>
Menu 1
<div class="sub_divsavedfun" id="Functions_1-5">
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>
Sub Menu 4
<ul>
<li>
Deep Menu 1
<ul>
<li>Sub Deep 1</li>
<li>Sub Deep 2</li>
<li>Sub Deep 3</li>
<li>Sub Deep 4</li>
</ul>
</li>
<li>Deep Menu 2</li>
</ul>
</li>
<li>Sub Menu 5</li>
</ul>
</div>
</li>
</ul>
</div>
CSS
<style>
.primary_nav_wrap
{
margin-top:15px
}
.primary_nav_wrap ul
{
list-style:none;
position:relative;
float:left;
margin:0;
padding:0
}
.primary_nav_wrap 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
}
.primary_nav_wrap ul li
{
position:relative;
float:left;
margin:0;
padding:0
}
.primary_nav_wrap ul li.current-menu-item
{
background:#ddd
}
.primary_nav_wrap ul li:hover
{
background:#f6f6f6
}
.primary_nav_wrap ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#fff;
padding:0
}
.primary_nav_wrap ul ul li
{
float:none;
width:200px
}
.primary_nav_wrap ul ul a
{
line-height:120%;
padding:10px 15px
}
.primary_nav_wrap ul ul ul
{
top:0;
left:100%
}
.primary_nav_wrap ul li:hover > ul
{
display:block
}
</style>
If I remove <div class="sub_divsavedfun" id="Functions_1-5"> then it will work.But as per requirement I have add div tag.
add this css
.primary_nav_wrap ul li:hover .sub_divsavedfun > ul
{
display:block
}
https://jsfiddle.net/mrob21h7/1/
Add this in css and it's working fine
You add div before the sub ul so, in css .primary_nav_wrap ul li:hover > ul not found any ul. because structure of HTML element is now .primary_nav_wrap ul > li > div > ul
.primary_nav_wrap ul li:hover > div > ul
{
display:block
}
Check this Demo
You have div direct child of li not ul. So just replace following code:
.primary_nav_wrap ul li:hover > ul {
display: block
}
to :
.primary_nav_wrap ul li:hover div > ul, .primary_nav_wrap ul li:hover > ul {
display: block
}
Working Snippet :
.primary_nav_wrap {
margin-top: 15px
}
.primary_nav_wrap ul {
list-style: none;
position: relative;
float: left;
margin: 0;
padding: 0
}
.primary_nav_wrap 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
}
.primary_nav_wrap ul li {
position: relative;
float: left;
margin: 0;
padding: 0
}
.primary_nav_wrap ul li.current-menu-item {
background: #ddd
}
.primary_nav_wrap ul li:hover {
background: #f6f6f6
}
.primary_nav_wrap ul ul {
display: none;
position: absolute;
top: 100%;
left: 0;
background: #fff;
padding: 0
}
.primary_nav_wrap ul ul li {
float: none;
width: 200px
}
.primary_nav_wrap ul ul a {
line-height: 120%;
padding: 10px 15px
}
.primary_nav_wrap ul ul ul {
top: 0;
left: 100%
}
/* CODE REPLACED BY BELOW
.primary_nav_wrap ul li:hover > ul {
display: block
}
*/
/*Code added */
.primary_nav_wrap ul li:hover div > ul, .primary_nav_wrap ul li:hover > ul {
display: block
}
<div class="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home
</li>
<li>
Menu 1
<div class="sub_divsavedfun" id="Functions_1-5">
<ul>
<li>Sub Menu 1
</li>
<li>Sub Menu 2
</li>
<li>Sub Menu 3
</li>
<li>
Sub Menu 4
<ul>
<li>
Deep Menu 1
<ul>
<li>Sub Deep 1
</li>
<li>Sub Deep 2
</li>
<li>Sub Deep 3
</li>
<li>Sub Deep 4
</li>
</ul>
</li>
<li>Deep Menu 2
</li>
</ul>
</li>
<li>Sub Menu 5
</li>
</ul>
</div>
</li>
</ul>
</div>

Overlapping Border on Dropdown Menu

I am trying to place a black border below the dropdown menu, then having the "selected" page which is currently the "Home" page being underlined using a orange border. I am trying to overlap the orange border on the black border.
http://jsfiddle.net/46z5su4u/
Edited
Some people seems to be confused, I want to put the orange line on top of the black line.
http://jsfiddle.net/46z5su4u/2/
Any advices?
HTML:
<h1>Drop Down Menu</h1>
<header>
<nav id="primary_nav_wrap">
<ul>
<li class="current-menu-item">Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4
</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 2
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
</ul>
</li>
<li>Menu 3
<ul>
<li class="dir">Sub Menu 1</li>
<li class="dir">Sub Menu 2 THIS IS SO LONG IT MIGHT CAUSE AN ISSEUE BUT MAYBE NOT?
</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 4</li>
<li>Menu 5</li>
<li>Menu 6</li>
<li>Contact Us</li>
</ul>
</nav>
</header>
CSS:
header
{
border-bottom:4px solid #3a3a3a;
}
#primary_nav_wrap
{
margin-top:15px;
margin-bottom:-4px;
position:relative;
}
#primary_nav_wrap ul
{
list-style:none;
float:left;
margin:0;
padding:0;
}
#primary_nav_wrap ul a
{
display:block;
text-decoration:none;
line-height:32px;
padding:0 15px;
font-family:"Open Sans","Lucida Grande",Tahoma,"Trebuchet MS",Verdana,Arial,sans-serif;
font-size:1.1em;
}
#primary_nav_wrap ul li
{
position:relative;
float:left;
margin:0;
padding:0;
}
#primary_nav_wrap ul li.current-menu-item
{
border-bottom:4px solid #ff7500;
font-weight:700;
}
#primary_nav_wrap ul li.current-menu-item a
{
color:#333;
}
#primary_nav_wrap ul li a:hover
{
color: #ff7500;
}
#primary_nav_wrap ul ul
{
display:none;
position:absolute;
top:100%;
left:0;
background:#f8f8f8;
padding: 0 12px;
box-shadow: 0 2px 2px #ccc;
}
#primary_nav_wrap ul ul li
{
float:none;
width:150px;
border-bottom: solid 1px #f2f2f2;
border-top: solid 1px #fff;
}
#primary_nav_wrap ul ul li:after
{
content: "";
}
nav ul ul li:last-child a
{
border-bottom: none;
}
nav ul ul li:first-child a
{
border-top: none;
}
#primary_nav_wrap ul ul a
{
line-height:120%;
padding:10px 6px;
}
#primary_nav_wrap ul li:hover > ul
{
display:block;
}
you may need a lower boarder when you move?
#primary_nav_wrap ul li a:hover
{
color: #ff7500;
border-top: 4px solid #ff7500;/*add*/
}
Try this http://jsfiddle.net/46z5su4u/1/
this is all i have added
#primary_nav_wrap ul li ul li {
border-bottom:1px solid orange;
}
Fixed
http://codepen.io/anon/pen/bfgvo
Added to html
<div class="clearBoth"></div>
<div id="top_blackline"></div>
Added to css
.clearBoth
{
clear:both;
}
#top_blackline
{
margin-top:-4px;
width: 100%;
border-bottom:4px solid #3a3a3a;
}
Just set the dark border on the first-level <li>s instead of <header>.
So the updated css code will look like this:
header {
position: absolute; /* removed border */
}
/* add the following rule */
#primary_nav_wrap > ul > li {
border-bottom:4px solid #3a3a3a;
}
which sets the dark border on first-level <li>s only using the direct child selector >, more on this selector here.
a preview of the code above is here.

Arrows in CSS menu are not lined up properly, when they are floated

I have created this kind of menu.
The CSS classes used are as follows.
#main_nav {
padding: 3px 0px 3px 0px;
z-index: 300 !important;
}
#main_nav ul {
list-style:none;
position:relative;
float:left;
margin:0;
padding:0;
background: #888;
z-index: 300 !important;
min-width: 150px;
white-space: nowrap;
}
#main_nav ul a {
display:block;
text-decoration:none;
font-size:12px;
line-height:32px;
padding:0 15px;
color: #fff;
font-family: "Helvetica Neue", Helvetica, Arial, Verdana, sans-serif;
}
#main_nav ul li {
position:relative;
float:left;
margin:0;
padding:0;
}
#main_nav ul li.current-menu-item {
background:#ddd;
}
#main_nav ul li:hover {
background: #008000;
}
#main_nav ul ul {
display:none;
position:absolute;
top:100%;
left:0;
background:#888;
padding:0;
}
#main_nav ul ul li {
float:none;
white-space: nowrap;
width: 100%;
}
#main_nav ul ul a {
line-height:120%;
padding:10px 15px;
}
#main_nav ul ul ul {
top:0;
left:100%;
}
#main_nav ul li:hover > ul {
display:block;
}
#main_nav > ul > li {
display: inline-block;
float: none;
margin: 0 -3px 0 0;
}
.menu li:last-of-type { border-right: none; }
.menu li > a:after {float: right; margin-left: 5px; content: '\25BA'; }
.menu > li > a:after {float: right; margin-left: 5px; content: '\25BC'; }
.menu li > a.only-child:after { margin-left: 0; content: ''; }
The HTML code :
$(document).ready(function() {
$("a:only-child").addClass("only-child");
});
<div id="main_nav">
<ul class="menu">
<li class="current-menu-item">Home</li>
<li>Menu 1
<ul>
<li>Sub Menu 1</li>
<li>Sub Menu 4
<ul>
<li>Deep Menu 1
<ul>
<li>Category 1</li>
<li>Category 2</li>
<li>Category 3</li>
<li>Category 4</li>
<li>Category 5</li>
</ul>
</li>
<li>Deep Menu 2</li>
</ul>
</li>
<li>Sub Menu 5</li>
</ul>
</li>
<li>Menu 2</li>
<li>Menu 3</li>
</ul>
</div>
This works fine except float: right in the following CSS classes.
.menu li > a:after {float: right; margin-left: 5px; content: '\25BA'; }
.menu > li > a:after {float: right; margin-left: 5px; content: '\25BC'; }
float: right; is meant to float a corresponding arrow to the right of the menu. It however, does not allow the menu text and the arrow to inline properly.
The discrepancy is clearly visible on FireFox.
The picture has been taken from FireFox (31.0). The arrows are not lined up properly with the text. The menu looks good on Google Chrome (36.0.1985.143 m) and Internet Explorer (8).
How can arrows be aligned right and lined up properly?
It looks like instead of setting float:right you needed to use
position: absolute;
right: 0px;
As shown here

dropdown on menu hover

i have a menu that when i hover has a drop down i follow this
and this but unfortunately im getting different result than expected..im getting list but it is displayed as line horizontally and not vertically like the example here is the code i added to make the drop down
css
ul # menu li ul .submenu{
display:none;
}
ul # menu li : hover ul .submenu{
display:block;
}
here is the html
<div id="menu">
<ul id="menu-list">
<li id="">Home</li>
<li id="">
Menu
<ul class="sub-menu">
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
</ul>
</li>
<li id="">Menu</li>
<li id="">Menu</li>
<li id="">Menu</li>
</ul>
</div>
Here you go:
Updated Fiddle
Just the small change. Made UL id = "menu"
<ul id="menu">
<li id="home">Home</li>
<li id="townoffice">
Menu
<ul class="sub-menu">
<li>Sub Menu 1</li>
<li>Sub Menu 2</li>
<li>Sub Menu 3</li>
<li>Sub Menu 4</li>
</ul>
</li>
<li id="busines">Menu</li>
<li id="residents">Menu</li>
<li id="tourists">Menu</li>
</ul>
HTML
<ul id="nav" class="sixteen columns">
<li>Home
</li>
<li>Portfolio
<ul>
<li>Asia
<ul>
<li>Korea</li>
<li>China</li>
<li>Japan</li>
</ul>
</li>
<li>Europe
<ul>
<li>France</li>
<li>Germany</li>
<li>Italy</li>
</ul>
</li>
</ul>
</li>
</ul>
CSS
#nav {
width:800px;
margin:30px 50px;
padding: 0;
float:left;
}
#nav li {
list-style: none;
float: left;
padding:0 10px;
background-color:#367FB3;
color:white;
}
#nav li a:hover {
background-color:#52baff;
color:#fff;
}
//daf adf
/*--temp--*/
#nav ul ul li {
clear:left;
}
#nav ul ul {
position:absolute;
left:14em;
top:0;
}
#nav ul ul li a {
display:block;
padding: 3px 15px;
color: #242424;
text-decoration: none;
font-size:13px;
font-family:"Lato" !important;
}
/*--end temp--*/
#nav li a {
display: block;
padding: 3px 15px;
color: #242424;
text-decoration: none;
font-size:13px;
font-family:"Lato" !important;
}
#nav a:hover {
color:#367FB3;
}
#nav a:active {
color:#367FB3;
}
#nav li ul {
display: none;
width: 14em;
/* Width to help Opera out */
background-color:transparent;
z-index:666;
}
#nav li:hover ul, #nav li.hover ul {
display: block;
position: absolute;
margin:0px -10px;
padding:0px;
}
#nav li:hover ul ul {
display:none;
}
#nav li ul li:hover ul {
display:block
}
#nav li:hover li, #nav li.hover li {
float: none;
line-height:30px;
}
#nav li:hover li a, #nav li.hover li a {
background-color:#367FB3;
color:#fff;
font-size:13px;
font-family:"Lato" !important;
}
#nav li li a:hover {
background-color:#52baff;
color:#fff;
}
FIDDLE

How to get a vertical submenu instead a horizontal one?

I developed a submenu with CSS & jQuery and my intention was that it was vertical submenu but I´m getting a horizontal menu.
I already tried to "play" with display in CSS but its not working, like #menu ul li ul li a {display: inline-block;}.
Anyone there knows the trick to put this submenu vertical?
My jsfiddle with full example:
http://jsfiddle.net/jcak/9EcnL/7/
My html:
<section id="menu-container">
<nav id="menu">
<ul>
<li>Home
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</li>
<li>Procuts
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
</ul>
</li>
<li>About</li>
<li>Contacts</li>
</ul>
</nav>
</section>
My css:
#menu ul li ul li {display: none;}
#menu-container
{
background:green;
width:100%;
height:46px;
float:left;
z-index:7;
}
#menu
{
width:960px;
height:auto;
margin:0 auto 0 auto;
}
#menu ul
{
list-style-type:none;
}
#menu ul li
{
display: inline-block;
float:left;
height:46px;
position: relative;
line-height:46px;
font-weight:300;
}
#menu ul li a
{
text-decoration:none;
color:#ccc;
display:block;
margin-right:5px;
height:46px;
line-height:46px;
padding:0 5px 0 5px;
font-size:20px;
}
#menu ul li a:hover
{
color:#fff;
}
#menu ul li ul {
position: absolute;
left: 0;
-webkit-padding-start: 0;
width: 300px;
}
#menu ul li ul li
{
color:#fff;
}
By default, a ul will be vertical.
The float:left is what is making this whole menu be horizontal instead.
Have a go at removing that and seeing how the menu behaves.
Working Fiddle
#menu ul li
{
display: inline-block;
height:46px;
position: relative;
line-height:46px;
font-weight:300;
}