How to prevent menu from closing when losing mouse focus? - html

I built a nice drop down menu using examples I had found on the web but my manager has run into the issue that the menu can be hard to use at times because if the mouse falls off of the menu it closes. I've read a few examples on how to get around it here, but my issue is that I can't get these solutions to work with my code.
Can someone give me a few pointers? My CSS code and a html example is included below:
#cssmenu ul,
#cssmenu li,
#cssmenu span,
#cssmenu a {
padding: 0;
position: relative;
margin-left:auto;
margin-right:auto;
text-align:center;
font-family: "charles modern"
}
#cssmenu {
line-height: 1;
background: #ffffff;
margin-left:auto;
margin-right:auto;
text-align:center;
}
#cssmenu .padding{
display: block;
position: absolute;
z-index: 78;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
background: none !important;
}
/* Padding amount for first-level dropdown */
#cssmenu > li > .submenu > .padding{
width: 400%;
left: -30%;
height: 160%;
}
#cssmenu .buffer{
display: block;
position: absolute;
bottom: 0px;
right: 100%;
width: 100%;
height: 100%;
background: none !important;
}
#cssmenu .shadow{
display: none !important;
}
#cssmenu:after,
#cssmenu ul:after {
content: '';
display: block;
clear: both;
}
#cssmenu a {
background: #ffffff;
color: #00A0DF;
display: block;
font-family: "charles modern", "charles modern light", Calibri, sans-serif;
font-weight: bold;
padding: 19px 20px;
text-decoration: none;
margin-left:auto;
margin-right:auto;
text-align:center;
z-index:99999;
}
#cssmenu ul {
list-style: none;
}
#cssmenu > ul > li {
display: inline-block;
float: center;
margin:0 auto;
}
#cssmenu.align-center {
text-align: center;
}
#cssmenu.align-center > ul > li {
float: none;
}
#cssmenu.align-center ul ul {
text-align: left;
}
#cssmenu.align-right > ul {
float: right;
}
#cssmenu.align-right ul ul {
text-align: right;
}
#cssmenu > ul > li > a {
color: #ffffff;
font-size: 12px;
}
#cssmenu > ul > li:hover:after {
content: '';
display: block;
width: 0;
height: 0;
position: absolute;
left: 50%;
bottom: 0;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-bottom: 10px solid #0fa1e0;
margin-left: -10px;
}
#cssmenu > ul > li:first-child > a {
border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
}
#cssmenu.align-right > ul > li:first-child > a,
#cssmenu.align-center > ul > li:first-child > a {
border-radius: 0;
-moz-border-radius: 0;
-webkit-border-radius: 0;
}
#cssmenu.align-right > ul > li:last-child > a {
border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
}
#cssmenu > ul > li.active > a {
color: #047aea;
text-align: center;
padding: 14px 14px;
font-size:150%;
font-style: bold;
background: #ffffff;
}
#cssmenu > ul > li:hover > a {
background-color: #00A0DF;
color: white;
}
#cssmenu .has-sub {
z-index: 1;
}
#cssmenu .has-sub:hover > ul {
display: block;
}
#cssmenu .has-sub ul {
display: none;
position: absolute;
width: 250px;
top: 100%;
left: 0;
font-size: 100%;
}
#cssmenu.align-right .has-sub ul {
left: auto;
right: 0;
}
#cssmenu .has-sub ul li {
*margin-bottom: -1px;
}
#cssmenu .has-sub ul li a {
background: #f2f2f2;
display: block;
line-height: 120%;
padding: 6px 6px;
color: black;
font-syle: bold;
font-size: 105%;
}
#cssmenu .has-sub ul li {
background: #f2f2f2;
}
#cssmenu .has-sub ul li:hover a {
background: #00A0DF;
}
#cssmenu ul ul li:hover > a {
color: #ffffff;
}
#cssmenu .has-sub .has-sub:hover > ul {
display: block;
}
#cssmenu .has-sub .has-sub ul {
display: none;
position: absolute;
left: 100%;
top: 0;
height: 250px;
overflow: auto;
}
#cssmenu.align-right .has-sub .has-sub ul,
#cssmenu.align-right ul ul ul {
left: auto;
right: 100%;
}
#cssmenu .has-sub .has-sub ul li a {
background: #f2f2f2;
}
#cssmenu .has-sub .has-sub ul li {
background: #f2f2f2;
}
#cssmenu .has-sub .has-sub ul li a:hover {
background: #00A0DF;
}
#cssmenu ul ul li.last > a,
#cssmenu ul ul li:last-child > a,
#cssmenu ul ul ul li.last > a,
#cssmenu ul ul ul li:last-child > a,
#cssmenu .has-sub ul li:last-child > a,
#cssmenu .has-sub ul li.last > a {
border-bottom: 0;
}
<div id='cssmenu'>
<ul>
<li class='active has-sub'><center>Image 1</center></a>
<ul>
<li class='has-sub'><a href='#'><span>Add New Item</span></a></li>
<li class='has-sub'><a href='#'><span>View Item</span></a></li>
<li class='has-sub last'><a href='#'><span>Item With Sub-Items:</span></a>
<ul>
<li><a href='#'><span>User 1</span></a></li>
<li><a href='#'><span>User 2</span></a></li>
</ul>
</li>
</ul>
<li class='active has-sub'><center> Image 2</center></a>
<ul>
<li class='has-sub'><a href='#'><span>Add New Item</span></a></li>
<li class='has-sub'><a href='#'><span>View Item</span></a></li>
</ul>
</li>
</ul>
</div>

EDIT: Now using a different (simpler) method involving borders and container divs. Just change the color of the border property of .level-2-menu, .level-3-menu from red to transparent in your actual code. The only issue is the level 3 menu blocking the bottom right corner of the previous level 2 item.
new jsFiddle
It sounds like what you need is some area around each menu where you can be off the visible menu but still keep the menu open. Here is some code to demonstrate the concept. You should be able to adapt the concept into your code and style it however you like.
jsFiddle
The background-color: red is just to make the extra hover area visible for demonstration purposes.
HTML:
<div class="cssmenu">
<ul class="level-1-menu">
<li class="level-1-item">
Level 1 Item 1
<ul class="level-2-menu">
<li class="level-2-item">Level 2 Item 1</li>
<li class="level-2-item">Level 2 Item 2</li>
<li class="level-2-item">
Level 2 Item 3 with Sub-Items
<ul class="level-3-menu">
<li class="level-3-item">Level 3 Item 1</li>
<li class="level-3-item">Level 3 Item 2 longer</li>
</ul>
</li>
</ul>
</li>
<li class="level-1-item">
Level 1 Item 2
<ul class="level-2-menu">
<li class="level-2-item">Level 2 Item 1</li>
<li class="level-2-item">Level 2 Item 2 longer</li>
</ul>
</li>
</ul>
</div>
CSS:
.cssmenu {
background-color: white;
}
.cssmenu ul {
background-color: white;
list-style: none;
padding: 0;
}
.cssmenu li {
padding: 20px 0;
position: relative;
}
.cssmenu li:hover {
background-color: lightblue;
}
.cssmenu li a {
padding: 20px;
}
.level-1-menu {
text-align: center;
}
.level-1-item {
display: inline-block;
text-align: left;
}
.level-2-menu,
.level-3-menu {
display: none;
position: absolute;
white-space: nowrap;
}
.level-1-item:hover .level-2-menu,
.level-2-item:hover .level-3-menu {
display: block;
}
.level-2-menu {
left: 0;
top: 100%;
}
.level-3-menu {
left: 100%;
top: 0;
}
.level-2-item {
position: relative;
}
.level-2-item::after,
.level-3-item::after {
background-color: red;
bottom: 0;
content: '';
left: -30px;
position: absolute;
right: -30px;
top: 0;
z-index: -1;
}
.level-2-item:first-child::after,
.level-3-item:first-child::after {
top: -30px;
}
.level-2-item:last-child::after,
.level-3-item:last-child::after {
bottom: -30px;
}

Related

Equal horizontal space between links in responsive navigation?

I am trying to make horizontal menu navigation. I have several links in navigation, and I would like to have equal horizontal space between them.
How to make links in horizontal menu with equal space between them?
HTML:
<div id="header">
<div class="secondary-navigation">
<div itemscope itemtype="http://schema.org/SiteNavigationElement">
<nav id="navigation">
<ul id="menu-main" class="menu">
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
<li class="menu-item">Link</li>
</ul>
Menu
</nav>
</div>
</div>
</div>
CSS:
#header {
position: relative;
float: left;
padding: 0 0 0 0;
clear: both;
}
/*-----------------------------------------------
/* Header navigation
/*---------------------------------------------*/
.secondary-navigation {
display: block;
width: 100%;
float: left;
}
.secondary-navigation a {
vertical-align: top;
color: #F1F1F1;
font-weight: bold;
margin-top: 8px;
margin-bottom: 4px;
line-height:18px;
font-size: 15px;
border-bottom: 2px solid #333888;
}
.secondary-navigation a:hover, .secondary-navigation .sfHover {
color: #F1F1F1;
border-bottom: 2px solid #F1F1F1;
}
.secondary-navigation li li a { line-height: 1 }
.secondary-navigation a .sub {
font-size: 12px;
font-weight: normal;
color: #CFCFCF;
text-transform: none;
}
.menu-item-has-children > a:after {
content: "▼";
font-size: 10px;
color: #F1F1F1;
position: absolute;
right: 12px;
top: 22px;
}
.footer-navigation .menu-item-has-children > a:after { display: none }
.sub-menu .menu-item-has-children>a:after {
right: 0;
top: 17px;
}
.menu .current-menu-item > a { background: #fff }
.menu .current-menu-item > a:after {
content: "";
position: absolute;
width: 100%;
height: 1px;
background: #fff;
bottom: 0px;
left: 0;
z-index: 1;
}
#navigation {
margin: 0 auto;
font-size: 13px;
width: 100%;
float: left;
}
#navigation ul {
margin: 0 auto;
list-style: none; /*Added*/}
#navigation .menu { float: left; }
#navigation ul li {
float: left;
position: relative;
margin-left: 0;
}
#navigation > ul li:first-child a { }
#navigation > ul li:last-child a { border-right: 0 }
#navigation ul .header-search { float: right }
#navigation > ul > li:last-child { border-right: none }
#navigation ul li a, #navigation ul li a:link, #navigation ul li a:visited { display: block }
#navigation > ul > .current a {
background: transparent;
color: #555 !important;
}
#navigation li:hover ul, #navigation li li:hover ul, #navigation li li li:hover ul, #navigation li li li li:hover ul {
opacity: 1;
left: -228px;
top: 0;
}
#navigation ul ul {
position: absolute;
width: 226px;
z-index: 400;
font-size: 12px;
color: #333888;
border: 1px solid #F1F1F1;
background: #FFFFFF;
padding: 0;
}
#navigation ul ul li {
margin-left: 0;
padding: 0 10%;
width: 80%;
color: #333;
}
#navigation ul ul li:hover { background: #F1F1F1 }
#navigation ul ul a, #navigation ul ul a:link, #navigation ul ul a:visited {
padding: 12px 0;
position: relative;
border-left: 0;
background: transparent;
border-right: 0;
text-transform: none;
line-height: 1.4;
margin-right: 0;
min-height: 100%;
}
#navigation ul ul li:last-child a { border-bottom: none }
#navigation ul ul {
opacity: 0;
left: -999em;
}
#navigation ul li:hover ul {
left: -1px;
opacity: 1;
top: 81px;
}
#navigation ul ul li:hover ul {
top: -1px;
left: -228px;
padding-top: 0;
}
#navigation ul ul ul:after { border-color: transparent }
I tried something like this, but it does not work for me.
You can use justify-content: space-between or justify-content: space-around flexbox property
ul {
display: flex;
justify-content: space-between;
border: 1px solid black;
list-style-type: none;
padding: 10px;
margin: 0;
}
<ul>
<li>Random Link</li>
<li>Random Link</li>
<li>Random Link</li>
<li>Random Link</li>
</ul>

float sub navigation left

I am trying to create a drop down navigation that you can see the sub menu within the drop down a little like this![enter image description here
I have the essential layout i want though i am failing to actually create the drop down float left.
HTML
<div id='cssmenu'>
<ul>
<li><a href='#'><span>Home</span></a></li>
<li><a href='#'><span>Services</span></a>
<div class="sub">
<div class="subinner">
<ul>
<li><a href='#'><span>Consultancy</span></a></li>
<li><a href='#'><span>Project Managment & Execution</span></a></li>
<li><a href='#'><span>Commissioning</span></a></li>
</ul>
<ul>
<li>Training</li>
<li>Plant Performance Enchancement</li>
</ul>
</div> <!-- End of Sub -->
</div> <!-- End of Subinner -->
</li>
<li class='sub'><a href='#'><span>About</span></a>
<div class="sub">
<div class="subinner">
<ul>
<li><a href='#'><span>Company</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
</div>
</li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
Below is the CSS controls the drop down no JS is used.
#cssmenu ul {
margin: 0;
padding: 7px 6px 0;
background: #493B88;
line-height: 100%;
width: auto;
}
#cssmenu li {
margin: 0 5px;
padding: 0 0 8px;
float: left;
position: relative;
list-style: none;
}
#cssmenu a,
#cssmenu a:link {
font-weight: bold;
font-size: 13px;
color: #e7e5e5;
text-decoration: none;
display: block;
padding: 8px 20px;
margin: 0;
}
#cssmenu a:hover {
background: #000;
color: #000;
}
#cssmenu .active a,
#cssmenu li:hover > a {
background: #ccc;
color: #000;
}
#cssmenu ul ul li:hover a,
#cssmenu li:hover li a {
background: none;
border: none;
color: #000;
width: 30%;
}
#cssmenu ul ul a:hover {
background: #7d7d7d;
color: #000 !important;
width: 400px;
}
#cssmenu li:hover > .sub .subinner ul {
display: block;
position: absolute;
left: 0;
}
#cssmenu ul ul {
z-index: 1000;
display: none;
margin: 0;
padding: 0;
width: 300px;
position: absolute;
top: 40px;
left: 0;
background: #ffffff;
border: solid 1px #b4b4b4;
}
#cssmenu ul ul li {
float: none;
margin: 0;
padding: 3px;
}
#cssmenu ul ul a,
#cssmenu ul ul a:link {
font-weight: normal;
font-size: 12px;
}
#cssmenu ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
* html #cssmenu ul {
height: 1%;
}
Here is a link to view my drop down menu Live
https://jsfiddle.net/pj8kL8dL/
I have spent 2-3 hours to get this result, i hope it is what you need.
By the way, I would avoid using position:something so often.
It seems a responsive one, did you like the result?
body{
margin: 0;
}
#cssmenu ul {
margin: 0;
}
#cssmenu ul > li {
margin: 0 5px;
padding: 0 0 8px;
float: left;
list-style: none;
}
#cssmenu ul > ul li {
margin: 0 5px;
padding: 0 0 8px;
float: none;
list-style: none;
}
#cssmenu a,
#cssmenu a:link {
font-weight: bold;
font-size: 13px;
color: #e7e5e5;
text-decoration: none;
display: block;
padding: 8px 20px;
margin: 0;
}
#cssmenu a:hover {
background: #000;
color: #000;
}
#cssmenu .active a,
#cssmenu li:hover > a {
background: #ccc;
color: #000;
}
#cssmenu ul ul li:hover a,
#cssmenu li:hover li a {
background: none;
border: none;
color: #000;
}
#cssmenu ul ul a:hover {
background: #7d7d7d;
color: #000 !important;
}
#cssmenu li:hover > .sub .subinner ul {
display: block;
}
#cssmenu ul ul {
z-index: 1000;
display: none;
margin: 0;
padding: 0;
background: #ffffff;
border-bottom: solid 1px #b4b4b4;
border-top: solid 1px #b4b4b4;
}
#cssmenu ul ul li {
float: left;
margin: 0;
padding: 3px;
}
#cssmenu li:nth-child(2) ul ul li {
float: none;
margin: 0;
padding: 3px;
}
#cssmenu li:nth-child(2) ul ul li a{
border-bottom: 1px solid #B4B4B4;
}
#cssmenu ul ul a,
#cssmenu ul ul a:link {
font-weight: normal;
font-size: 12px;
}
#cssmenu ul:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
* html #cssmenu ul {
height: 1%;
}
#cssmenu ul li:nth-child(2) .subinner{
top: 47px;
position: absolute;
left: 0px;
width: 100%;
}
#cssmenu ul li:nth-child(3) .subinner{
top: 47px;
position: absolute;
left: 0px;
width: 100%;
}
#cssmenu{
margin: 0px;
padding: 7px 6px 0px;
background: #493B88 none repeat scroll 0% 0%;
line-height: 100%;
width: auto;
height: 40px;
}
#cssmenu li:nth-child(2):hover > .sub .subinner ul {
width: 100%;
}
#cssmenu li:hover > .sub .subinner ul {
width: 100%;
}
#cssmenu .subinner ul ul{
float: left;
width: 33% !important;
border: none;
}
<div id='cssmenu'>
<ul>
<li><a href='#'><span>Home</span></a></li>
<li><a href='#'><span>Services</span></a>
<div class="sub">
<div class="subinner">
<ul>
<ul>
<li><a href='#'><span>Consultancy</span></a></li>
<li><a href='#'><span>Project Managment & Execution</span></a></li>
</ul>
<ul>
<li><a href='#'><span>Commissioning</span></a></li>
<li><span>Training</span></li>
<li><span>Plant Performance Enchancement</span></li>
</ul>
<ul>
<li><a href='#'><span>Something</span></a></li>
<li><a href='#'><span>Something</span></a></li>
</ul>
</ul>
</div> <!-- End of Sub -->
</div> <!-- End of Subinner -->
</li>
<li class='sub'><a href='#'><span>About</span></a>
<div class="sub">
<div class="subinner">
<ul>
<li><a href='#'><span>Company</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
</div>
</li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
Remove the position: relative; in #cssmenu li so the sub menu will be relative to any outer div or body.
Also increase width of sub menu to 100% in #cssmenu ul ul

Changing background Color of root element for clicking or selecting Child Element

Consider the following HTML
HTML
<div id='cssmenu'>
<ul>
<li class='has-sub'><a href='#'><span>Skills</span></a>
<ul>
<li class='has-sub'><a href='#'><span>Analysis</span></a>
<ul>
<li><a href='#'><span>Category and Skills</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Employee Skills </span></a>
</li>
<li class='has-sub'><a href='#'><span>Setup </span></a>
<ul>
<li><a href='#'><span>Skills</span></a></li>
<li class='last'><a href='#'><span>Skills Categories</span></a></li>
<li class='last'><a href='#'><span>Skill Level</span></a></li>
</ul>
</li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Proposals</span></a>
<ul>
<li><a href='#'><span>New Propsals</span></a></li>
<li class='last'><a href='#'><span>List of Proposals</span></a></li>
</ul>
</li>
<li class="has-sub"><a href='#'><span>Procurement</span></a>
<ul>
<li><a href='#'><span>Indents</span></a></li>
<li><a href='#'><span>Status</span></a></li>
</ul>
</li>
</ul>
</div>
CSS
#import url(http://fonts.googleapis.com/css?family=Oxygen+Mono);
/* Please Keep this font import at the very top of any CSS file */
#charset "UTF-8";
/* Starter CSS for Flyout Menu */
#cssmenu {
padding: 0;
margin: 0;
border: 0;
line-height: 1;
}
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul ul {
list-style: none;
margin: 0;
padding: 0;
}
#cssmenu ul {
position: relative;
z-index: 597;
float: left;
}
#cssmenu ul li {
float: left;
min-height: 1px;
line-height: 1em;
vertical-align: middle;
position: relative;
}
#cssmenu ul li.hover,
#cssmenu ul li:hover {
position: relative;
z-index: 599;
cursor: default;
}
#cssmenu ul ul {
visibility: hidden;
position: absolute;
top: 100%;
left: 0px;
z-index: 598;
width: 100%;
}
#cssmenu ul ul li {
float: none;
}
#cssmenu ul ul ul {
top: -2px;
right: 0;
}
#cssmenu ul li:hover > ul {
visibility: visible;
}
#cssmenu ul ul {
top: 1px;
left: 99%;
}
#cssmenu ul li {
float: none;
}
#cssmenu ul ul {
margin-top: 1px;
}
#cssmenu ul ul li {
font-weight: normal;
}
/* Custom CSS Styles */
#cssmenu {
width: 200px;
background: #333333;
font-family: 'Oxygen Mono', Tahoma, Arial, sans-serif;
zoom: 1;
font-size: 12px;
}
#cssmenu:before {
content: '';
display: block;
}
#cssmenu:after {
content: '';
display: table;
clear: both;
}
#cssmenu a {
display: block;
padding: 15px 20px;
color: #ffffff;
text-decoration: none;
text-transform: uppercase;
}
#cssmenu > ul {
width: 200px;
}
#cssmenu ul ul {
width: 200px;
}
#cssmenu > ul > li > a {
border-right: 4px solid #1b9bff;
color: #ffffff;
}
#cssmenu > ul > li > a:hover {
color: #ffffff;
}
#cssmenu > ul > li.active a {
background: #1b9bff;
}
#cssmenu > ul > li a:hover,
#cssmenu > ul > li:hover a {
background: #1b9bff;
}
#cssmenu li {
position: relative;
}
#cssmenu ul li.has-sub > a:after {
content: '+';
position: absolute;
top: 50%;
right: 15px;
margin-top: -6px;
}
#cssmenu ul ul li.first {
-webkit-border-radius: 0 3px 0 0;
-moz-border-radius: 0 3px 0 0;
border-radius: 0 3px 0 0;
}
#cssmenu ul ul li.last {
-webkit-border-radius: 0 0 3px 0;
-moz-border-radius: 0 0 3px 0;
border-radius: 0 0 3px 0;
border-bottom: 0;
}
#cssmenu ul ul {
-webkit-border-radius: 0 3px 3px 0;
-moz-border-radius: 0 3px 3px 0;
border-radius: 0 3px 3px 0;
}
#cssmenu ul ul {
border: 1px solid #0082e7;
}
#cssmenu ul ul a {
font-size: 12px;
color: #ffffff;
}
#cssmenu ul ul a:hover {
color: #ffffff;
}
#cssmenu ul ul li {
border-bottom: 1px solid #0082e7;
}
#cssmenu ul ul li:hover > a {
background: #4eb1ff;
color: #ffffff;
}
#cssmenu.align-right > ul > li > a {
border-left: 4px solid #1b9bff;
border-right: none;
}
#cssmenu.align-right {
float: right;
}
#cssmenu.align-right li {
text-align: right;
}
#cssmenu.align-right ul li.has-sub > a:before {
content: '+';
position: absolute;
top: 50%;
left: 15px;
margin-top: -6px;
}
#cssmenu.align-right ul li.has-sub > a:after {
content: none;
}
#cssmenu.align-right ul ul {
visibility: hidden;
position: absolute;
top: 0;
left: -100%;
z-index: 598;
width: 100%;
}
#cssmenu.align-right ul ul li.first {
-webkit-border-radius: 3px 0 0 0;
-moz-border-radius: 3px 0 0 0;
border-radius: 3px 0 0 0;
}
#cssmenu.align-right ul ul li.last {
-webkit-border-radius: 0 0 0 3px;
-moz-border-radius: 0 0 0 3px;
border-radius: 0 0 0 3px;
}
#cssmenu.align-right ul ul {
-webkit-border-radius: 3px 0 0 3px;
-moz-border-radius: 3px 0 0 3px;
border-radius: 3px 0 0 3px;
}
jsfiddle
Here I have made a multilevel menu items.What I want is that on clicking any of the child element the root element will have it's background color changed(sort of selected items style) and child element panel disappear. How can I do that?
MY TRY
$(document).ready(function () {
$( "a" ).click(function( event ) {
$( ".has-sub" ).css("background-color","blue");
});
I have tried above code to try two things but did not work.
would use jQuery.
-- not sure exactly which elements you want the background changed on but this is the basic idea.
$(function() {
$('.last').click(function() {
$(".has-sub").css("background", "yellow");
})
});
Please let me know if this is what you are looking for?
<html>
<head>
<style type="text/css">
#import url(http://fonts.googleapis.com/css?family=Oxygen+Mono);
/* Please Keep this font import at the very top of any CSS file */
#charset "UTF-8";
/* Starter CSS for Flyout Menu */
#cssmenu {
padding: 0;
margin: 0;
border: 0;
line-height: 1;
}
#cssmenu ul,
#cssmenu ul li,
#cssmenu ul ul {
list-style: none;
margin: 0;
padding: 0;
}
#cssmenu ul {
position: relative;
z-index: 597;
float: left;
}
#cssmenu ul li {
float: left;
min-height: 1px;
line-height: 1em;
vertical-align: middle;
position: relative;
}
#cssmenu ul li.hover,
#cssmenu ul li:hover {
position: relative;
z-index: 599;
cursor: default;
}
#cssmenu ul ul {
visibility: hidden;
position: absolute;
top: 100%;
left: 0px;
z-index: 598;
width: 100%;
}
#cssmenu ul ul li {
float: none;
}
#cssmenu ul ul ul {
top: -2px;
right: 0;
}
#cssmenu ul li:hover > ul {
visibility: visible;
}
#cssmenu ul ul {
top: 1px;
left: 99%;
}
#cssmenu ul li {
float: none;
}
#cssmenu ul ul {
margin-top: 1px;
}
#cssmenu ul ul li {
font-weight: normal;
}
/* Custom CSS Styles */
#cssmenu {
width: 200px;
background: #333333;
font-family: 'Oxygen Mono', Tahoma, Arial, sans-serif;
zoom: 1;
font-size: 12px;
}
#cssmenu:before {
content: '';
display: block;
}
#cssmenu:after {
content: '';
display: table;
clear: both;
}
#cssmenu a {
display: block;
padding: 15px 20px;
color: #ffffff;
text-decoration: none;
text-transform: uppercase;
}
#cssmenu > ul {
width: 200px;
}
#cssmenu ul ul {
width: 200px;
}
#cssmenu > ul > li > a {
border-right: 4px solid #1b9bff;
color: #ffffff;
}
#cssmenu > ul > li > a:hover {
color: #ffffff;
}
#cssmenu > ul > li.active a {
background: #1b9bff;
}
#cssmenu > ul > li a:hover
{
background: #1b9bff;
}
#cssmenu ul ul li
{
background-color:#1b9bff;
}
#cssmenu li {
position: relative;
}
#cssmenu ul li.has-sub > a:after {
content: '+';
position: absolute;
top: 50%;
right: 15px;
margin-top: -6px;
}
#cssmenu ul ul li.first {
-webkit-border-radius: 0 3px 0 0;
-moz-border-radius: 0 3px 0 0;
border-radius: 0 3px 0 0;
}
#cssmenu ul ul li.last {
-webkit-border-radius: 0 0 3px 0;
-moz-border-radius: 0 0 3px 0;
border-radius: 0 0 3px 0;
border-bottom: 0;
}
#cssmenu ul ul {
-webkit-border-radius: 0 3px 3px 0;
-moz-border-radius: 0 3px 3px 0;
border-radius: 0 3px 3px 0;
}
#cssmenu ul ul {
border: 1px solid #0082e7;
}
#cssmenu ul ul a {
font-size: 12px;
color: #ffffff;
}
#cssmenu ul ul a:hover {
color: #ffffff;
}
#cssmenu ul ul li {
border-bottom: 1px solid #0082e7;
}
#cssmenu ul ul li:hover > a {
background: #4eb1ff;
color: #ffffff;
}
#cssmenu.align-right > ul > li > a {
border-left: 4px solid #1b9bff;
border-right: none;
}
#cssmenu.align-right {
float: right;
}
#cssmenu.align-right li {
text-align: right;
}
#cssmenu.align-right ul li.has-sub > a:before {
content: '+';
position: absolute;
top: 50%;
left: 15px;
margin-top: -6px;
}
#cssmenu.align-right ul li.has-sub > a:after {
content: none;
}
#cssmenu.align-right ul ul {
visibility: hidden;
position: absolute;
top: 0;
left: -100%;
z-index: 598;
width: 100%;
}
#cssmenu.align-right ul ul li.first {
-webkit-border-radius: 3px 0 0 0;
-moz-border-radius: 3px 0 0 0;
border-radius: 3px 0 0 0;
}
#cssmenu.align-right ul ul li.last {
-webkit-border-radius: 0 0 0 3px;
-moz-border-radius: 0 0 0 3px;
border-radius: 0 0 0 3px;
}
#cssmenu.align-right ul ul {
-webkit-border-radius: 3px 0 0 3px;
-moz-border-radius: 3px 0 0 3px;
border-radius: 3px 0 0 3px;
}
</style>
</head>
<body>
<div id='cssmenu'>
<ul>
<li class='has-sub'><a href='#'><span>Skills</span></a>
<ul>
<li class='has-sub'><a href='#'><span>Analysis</span></a>
<ul>
<li><a href='#'><span>Category and Skills</span></a></li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Employee Skills </span></a>
</li>
<li class='has-sub'><a href='#'><span>Setup </span></a>
<ul>
<li><a href='#'><span>Skills</span></a></li>
<li class='last'><a href='#'><span>Skills Categories</span></a></li>
<li class='last'><a href='#'><span>Skill Level</span></a></li>
</ul>
</li>
</ul>
</li>
<li class='has-sub'><a href='#'><span>Proposals</span></a>
<ul>
<li><a href='#'><span>New Propsals</span></a></li>
<li class='last'><a href='#'><span>List of Proposals</span></a></li>
</ul>
</li>
<li class="has-sub"><a href='#'><span>Procurement</span></a>
<ul>
<li><a href='#'><span>Indents</span></a></li>
<li><a href='#'><span>Status</span></a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
CSS
#cssmenu ul li a.select {
background: #4eb1ff;
color:#fff;
}
HTML
<ul>
<li class='has-sub'>
<span>Skills</span>
</li>
</ul>
Try this. Its working fine for me.

Responsive Navigation Menu

I am trying to develop a menu which is responsive. The menu should look like this when not responsive:
And like the below when screen is reduced for tablets/mobiles:
You can find a fiddle here: http://jsfiddle.net/yxMhj/
HTML Code:
<div id='cssmenu'>
<div class="logo">
<img src="images/logo.png" />
</div>
<ul>
<li class='active'><a href='index.html'>Home</a></li>
<li class='has-sub '><a href='#'>Products</a>
<ul>
<li class='has-sub '><a href='#'>Product 1</a>
<ul>
<li><a href='#'>Sub Item</a></li>
<li><a href='#'>Sub Item</a></li>
</ul>
</li>
<li class='has-sub '><a href='#'>Product 2</a>
<ul>
<li><a href='#'>Sub Item</a></li>
<li><a href='#'>Sub Item</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>About</a></li>
<li><a href='#'>Contact</a></li>
</ul>
</div>
CSS Code:
#import url(http://fonts.googleapis.com/css?family=Oxygen+Mono);
#cssmenu {padding: 0; margin: 0; border: 0;}
#cssmenu ul, #cssmenu li {list-style: none; margin: 0; padding: 0;}
#cssmenu ul {position: relative; z-index: 597; }
#cssmenu ul li { float: left; min-height: 1px; vertical-align: middle;}
#cssmenu ul li.hover,
#cssmenu ul li:hover {position: relative; z-index: 599; cursor: default;}
#cssmenu ul ul {visibility: hidden; position: absolute; top: 100%; left: 0; z-index: 598;}
#cssmenu ul ul li {float: none;}
#cssmenu ul ul ul {top: 0; left: auto; right: -99.5%; }
#cssmenu ul li:hover > ul { visibility: visible;}
#cssmenu ul ul {bottom: 0; left: 0;}
#cssmenu ul ul {margin-top: 0; }
#cssmenu ul ul li {font-weight: normal;}
#cssmenu a { display: block; line-height: 1em; text-decoration: none; }
#cssmenu {
background: #333;
border-bottom: 4px solid #1b9bff;
font-family: 'Oxygen Mono', Tahoma, Arial, sans-serif;
font-size: 16px;
}
#cssmenu > ul { *display: inline-block; }
#cssmenu:after, #cssmenu ul:after {
content: '';
display: block;
clear: both;
}
#cssmenu a {
background: #333;
color: #CBCBCB;
padding: 0 20px;
}
#cssmenu ul { text-transform: uppercase; }
#cssmenu ul ul {
border-top: 4px solid #1b9bff;
text-transform: none;
min-width: 190px;
}
#cssmenu ul ul a {
background: #1b9bff;
color: #FFF;
border: 1px solid #0082e7;
border-top: 0 none;
line-height: 150%;
padding: 16px 20px;
}
#cssmenu ul ul ul { border-top: 0 none; }
#cssmenu ul ul li { position: relative }
#cssmenu > ul > li > a { line-height: 80px; }
#cssmenu ul ul li:first-child > a { border-top: 1px solid #0082e7; }
#cssmenu ul ul li:hover > a { background: #35a6ff; }
#cssmenu ul ul li:last-child > a {
border-radius: 0 0 3px 3px;
box-shadow: 0 1px 0 #1b9bff;
}
#cssmenu ul ul li:last-child:hover > a { border-radius: 0 0 0 3px; }
#cssmenu ul ul li.has-sub > a:after {
content: '+';
position: absolute;
top: 50%;
right: 15px;
margin-top: -8px;
}
#cssmenu ul li:hover > a, #cssmenu ul li.active > a {
background: #1b9bff;
color: #FFF;
}
#cssmenu ul li.has-sub > a:after {
content: '+';
margin-left: 5px;
}
#cssmenu ul li.last ul {
left: auto;
right: 0;
}
#cssmenu ul li.last ul ul {
left: auto;
right: 99.5%;
}
.logo{
margin-top:0px;
float:left;
margin-left:20%;
padding-right:10px;
}
I really appreciate your help guys!
Try this
<style>
.menu {
border: 0;
margin: 0 0 10px 0;
padding: 0;
width: 100%;
}
.menu ul {
display: table;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
.menu li {
display: table-cell;
background-color: #eee;
}
.menu li:hover {
background-color: #ccc;
}
.menu a {
display: block;
padding: 10px 0;
width: 100%;
}
.menu li, .menu a {
text-align: center;
}
#Media screen and (max-width:480px) {
div.menu li {
display: block !important;
width: 100% !important;
}
}
</style>
<div class="menu">
<ul>
<li>Your logo here</li>
<li>Home</li>
<li>Products</li>
<li>Company</li>
<li>Blog</li>
</ul>
</div>

How to make a horizontal expandable menu fluid

I have the following HTML code which displays a horizontal expandable menu inside a DIV:
<div id='cssmenu'>
<ul>
<li class='active'><a href='about_us.aspx'>ABOUT US</a></li>
<li>MISSION</li>
<li class='has-sub'><a href='#'>LEADERSHIP</a>
<ul>
<li><a href='#'>President</a></li>
<li><a href='#'>Medical Director</a></li>
<li><a href='#'>Board of Directors</a></li>
<li><a href='#'>Key Administrators</a></li>
</ul>
</li>
<li><a href='#'>WESTMED HISTORY</a></li>
<li><a href='#'>COMMUNITY SUPPORT</a></li>
</ul>
</div>
Here is my CSS for the menu:
#import url(http://fonts.googleapis.com/css?family=Oxygen+Mono);
#cssmenu {
padding: 0;
margin: 0;
border: 0;
width: 100%;
}
#cssmenu ul, #cssmenu li {
list-style: none;
margin: 0;
padding: 0;
}
#cssmenu ul {
position: relative;
z-index: 19;
}
#cssmenu ul li {
float: left;
min-height: 1px;
vertical-align: middle;
border-right: 1px solid #535353;
}
#cssmenu ul li:last-child {
border-right: none;
}
#cssmenu ul li.hover, #cssmenu ul li:hover {
position: relative;
z-index: 20;
cursor: default;
}
#cssmenu ul ul {
visibility: hidden;
position: absolute;
top: 100%;
left: 0;
z-index: 21;
width: 100%;
}
#cssmenu ul ul li {
float: none;
}
#cssmenu ul ul ul {
top: 0;
left: auto;
right: -99.5%;
}
#cssmenu ul li:hover > ul {
visibility: visible;
}
#cssmenu ul ul {
bottom: 0;
left: 0;
}
#cssmenu ul ul {
margin-top: 0;
}
#cssmenu ul ul li {
font-weight: normal;
}
#cssmenu a {
display: block;
line-height: 1em;
text-decoration: none;
}
#cssmenu {
background: #333;
border-bottom: 4px solid #1b9bff;
font-family: Tahoma, Arial, sans-serif;
font-size: 12px;
}
#cssmenu > ul {
*display: inline-block;
}
#cssmenu:after, #cssmenu ul:after {
content: '';
display: block;
clear: both;
}
#cssmenu a {
background: #333;
color: #CBCBCB;
padding: 0 12px;
}
#cssmenu ul {
text-transform: uppercase;
}
#cssmenu ul ul {
border-top: 4px solid #1b9bff;
text-transform: none;
min-width: 190px;
}
#cssmenu ul ul a {
background: #1b9bff;
color: #FFF;
border: 1px solid #0082e7;
border-top: 0 none;
line-height: 150%;
padding: 16px 20px;
}
#cssmenu ul ul ul {
border-top: 0 none;
}
#cssmenu ul ul li {
position: relative;
}
#cssmenu > ul > li > a {
line-height: 48px;
}
#cssmenu ul ul li:first-child > a {
border-top: 1px solid #0082e7;
}
#cssmenu ul ul li:hover > a {
background: #35a6ff;
}
#cssmenu ul ul li:last-child > a {
border-radius: 0 0 3px 3px;
box-shadow: 0 1px 0 #1b9bff;
}
#cssmenu ul ul li:last-child:hover > a {
border-radius: 0 0 0 3px;
}
#cssmenu ul ul li.has-sub > a:after {
content: '+';
position: absolute;
top: 50%;
right: 15px;
margin-top: -8px;
}
#cssmenu ul li:hover > a, #cssmenu ul li.active > a {
background: #1b9bff;
color: #FFF;
}
#cssmenu ul li.has-sub > a:after {
content: '+';
margin-left: 5px;
}
#cssmenu ul li.last ul {
left: auto;
right: 0;
}
#cssmenu ul li.last ul ul {
left: auto;
right: 99.5%;
}
For desktop and large screen it works great but whenever I load it inside a tablet the image goes over to the next line:
How can I make the menu more fluid, so it resizes based on the amount of space that's available to the cssmenu DIV?
Check out flexbox, this might be an option for you.
This demo is pretty handy, you would choose space-around (if I understood your question correctly): http://codepen.io/chriscoyier/pen/FAbpm