I have a pure CSS menu (no javascript desired for this), with the HTML:
<ul id="nav">
<li>
<div>First Menu
</div>
<ul>
<li>First Option</li>
<li>Second Option</li>
<li id="subnav">Sub Menu —>>
<ul>
<li>First Sub Option</li>
<li>Second Sub Option</li>
<li>Third Sub Option</li>
<li>Fourth Sub Option</li>
</ul>
</li>
<li>Third Option
</li>
<li id="subnav">Second Sub Menu —>>
<ul>
<li>Second Sub Menu, First Sub Option</li>
<li>Second Sub Menu, Second Sub Option</li>
</ul>
<li>Fourth Option</li>
</li>
</ul>
</li>
<li>
<div>Second Menu
</div>
<ul>
<li>Next First Option</li>
<li>Next Second Option</li>
</ul>
</li>
</ul>
And the CSS:
#nav, #nav a {
text-decoration: none;
text-transform: uppercase;
color: #000;
padding: 0px 1px 0px 1px;
margin: 0px;
border: black thin solid;
text-decoration: none;
font-weight: bold;
font-style: italic;
font-size: 115%;
list-style-type: none;
font-family: sans-serif;
margin-left: none;
background-color: #EFAA0D;
z-index:100;
}
#nav>li {
float: left;
}
#nav li ul {
list-style-type: none;
display: none;
width:20em;
position: absolute;
left: 4px;
font-weight: normal;
}
#nav li>ul {
top: auto;
left: auto;
}
#nav li:hover>ul {
display: block;
}
#nav li a:hover {
background-color: #fdca2e;
}
#nav li a:link, #nav li a:visited {
padding: 0em .25em 0em 0.25em;
border: black thin solid;
text-decoration: none;
display: block;
left:2px;
}
#nav li>a {
font-weight: normal;
}
#subnav li a:link, #subnav li a:visited {
color: #EFAA0D;
padding: 0em .25em 0em 0.25em;
border: green thin solid;
display: block;
left:2px;
background-color: black;
}
#subnav li a:hover {
background-color: #555;
position: relative;
left: 1px;
}
Please see the fiddle: http://jsfiddle.net/sablefoste/wRK9v/7/
My concern is viewing the "Third Option" and beyond. When trying to access these options, it is covered by the sub-menu of the "Second Option". The only way to access is to skate the mouse over the padding.
Is there a way to fix this?
You have left the default padding for unordered list elements, which is what is indenting your menu. Padding is considered part of the element regarding the :hover state. You first want to zero out the padding for your UL elements, then add a margin [-left] of the amount you wish for it to indent. In this case, you would add (or I should say, merge):
#nav li ul {
padding: 0;
margin-left: 40px;
}
Demo: http://jsfiddle.net/wRK9v/8/
You have not overridden the default padding on the ULs, so in your Fiddle you can see how the menu is moved over to the left, but when you mouseover the area "outside" of the UL you are still technically moused over the third UL and it will not disappear.
If you override the default padding that space on the left will go away, but now all of your UL elements will be lined up and you still won't be able to view the rest of the menu.
#nav li > ul {
top: auto;
left: auto;
padding-left: 0;
}
If you then set the left positioning, you will be able to display the sub menus with as much or as little spacing as you want. You can apply this to all of the menus:
#nav li > ul {
top: auto;
left: 100px;
padding-left: 0;
}
Or specifically add it to the 3rd level menu:
#nav li #subnav > ul {
padding-left: 0px;
left: 150px;
}
Here's the Fiddle
I tend to want to solve everything in code, but a quick change is to increase the left-hand margin:
#nav li ul li ul
{
margin-left: 17.5em;
margin-top: -1.5em;
}
I'd assume you actually want the submenu to be just on the right of the second menu, right? I'd rework your indents to use hard pixels instead of ems. You are already specifying your width as 20em, so you can work with that and have your submenus show up to the right of the main menu.
The fiddle: http://jsfiddle.net/wRK9v/12/
Related
I have a problem with the navigation bar. When I hover over About or Text on the nav bar it shows a spacing on the left side of the button, I want it the hover colour to contain the full width of the button.
https://jsfiddle.net/jdd3h0sf/3/
HTML:
<div id="nav">
<ul>
<li class="home">Home</li>
<li>About</li>
<li>Text ⌄
<ul class="submenu">
<li>One</li>
<li>Two</li>
<li>Three</li></li>
</ul>
<li>Work</li>
<li>Contact ⌄
<ul class="submenutwo">
<li>One</li>
<li>Two</li>
<li>Three</li></li>
</ul>
</ul>
CSS:
#nav {
background-color: #333;
height: 52px;
text-align: center;
margin: 0 auto;
letter-spacing: 1px;
text-transform: uppercase;
}
#nav ul {
list-style-type: none;
padding: 0;
margin: 0;
}
#nav li {
border-right: 1.8px solid #191919;
height: auto;
width: 156.5px;
padding: 0;
margin: 0;
}
.home {
border-left: 1.8px solid #191919;
}
#nav ul li {
display: inline-block;
}
#nav ul li:hover {
background-color: #444;
}
#nav ul li a, visted {
color: #ccc;
display: block;
padding: 15px;
margin: 0;
text-decoration: none;
}
#nav ul li a:hover {
color: #ccc;
text-decoration: none;
}
#nav ul li:hover ul {
display: block;
}
#nav ul ul {
display: none;
position: absolute;
background-color: #444;
border: 1px solid #333;
border-top: 0;
max-width: 169px;
}
#nav ul ul li {
display: block;
}
#nav ul ul li a:visited {
color: #ccc;
}
#nav ul ul li a:hover {
color: #2980B9;
}
This is a part of display:inline-block;. If you want to keep them displayed inline-block, there are several different solutions (Read a css-Tricks article about it):
1 - Change your HTML format:
Change your <li>'s html like this:
<ul>
<li>
one</li><li>
two</li><li>
three</li>
</ul>
Or this:
<ul>
<li>one</li
><li>two</li
><li>three</li>
</ul>
Or even with comments, like this:
<ul>
<li>one</li><!--
--><li>two</li><!--
--><li>three</li>
</ul>
Or, just place all the li's on a single line:
<ul><li>one</li><li>two</li><li>three</li></li>
It is messy, yet effective.
2 - Negative margins:
Pretty straightforward:
li{
display: inline-block;
margin-right: -4px;
}
3 - Skip the closing tag:
This is actually perfectly fine in HTML5, li's do not have to have a closing tag.
<ul>
<li>one
<li>two
<li>three
</ul>
4 - Set the <ul>'s font size to 0:
ul {
font-size: 0;
}
ul li {
font-size: 16px;
}
5 - Or, just float the <li>'s:
Whatever floats your boat.
You are experiencing the dreaded inline-block spacing issue. In your fiddle, if you condense all of your li elements to be on the same line, the hover works as expected. The linked article outlines a few other options.
You can also just float the elements and that would resolve the issue.
#nav ul li {
float: left;
}
I am having some problem getting my navigation bar to work. Here is the desired output for my navigation bar: example.
I am trying to make it so that when the user hovers the top level of the navigation bar, a drop down list is shown.
However, the second level of my navigation bar is just floating around. How can I style it?
This is my HTML:
<div id="menubar">
<ul id="menu">
<li class="selected">Home</li>
<li>Volunteers
<ul>
<li>Add</li>
<li>View</li>
<li>Update</li>
</ul>
</li>
<li>Packaging Session
<ul>
<li>Add</li>
<li>View</li>
<li>Update</li>
</ul>
</li>
</ul>
</div>
And this is my CSS:
#menubar
{ width: 900px;
height: 72px;
padding: 0;
background: #1293EE;}
ul#menu, ul#menu li
{ float: left;
margin: 0;
padding: 0;}
ul#menu li
{ list-style: none;}
ul#menu li a
{ letter-spacing: 0.1em;
font: normal 100% arial, sans-serif;
display: block;
float: left;
height: 37px;
padding: 29px 26px 6px 26px;
text-align: center;
color: #FFF;
text-transform: uppercase;
text-decoration: none;
background: transparent;}
ul#menu li a:hover, ul#menu li.selected a, ul#menu li.selected a:hover
{ color: #FFF;
background: #0D66A5;}
ul#menu li ul li a
{
display: none;
height: auto;
margin: 0;
padding: 0;
position:absolute;
}
In google chrome:
In Internet Explorer:
Instead of hiding every a tag in the dropdown, rather hide the entire ul, and use that as the position element, and style the li's and a's as any other element.
Example: http://jsfiddle.net/gd2SX/
Look for the area, that says "Added styles".
HTMLDog made a very interesting and comprehensive article about floating menus: Sons of Suckerfish.
You will be particularly interested in the part about dropdowns, and particularly the sample they provide.
It will help you correct much more than just your floating problem.
For your particular problem, I suggest that you disable the hiding of the menus, style everything to look perfect as if all the submenus were open, and then re-activate the hiding of submenus:
/* Lists directly inside list-items. */
li>ul {
display: none;
}
/* Lists directly inside hovered list-items. */
li:hover>ul,
li.selected>ul {
display: block;
}
Then you will find it much easier to fix.
my sub navigation menu items are being displayed on top of each other
here is the code:
/* NAVIGATION */ .nav-bar {width: 100%; height: 80px; background-image:url(../images/bg-menu80.jpg);}
.nav-hold {overflow: hidden;}
.nav-list {float: right;}
.nav-list li {float: left; width: auto; position: relative;}
.nav-list li a {text-decoration: none; display:block; padding: 30px 7px 20px 7px; color: #f9f9f9; font-size: .9em; font-weight: bold;}
.nav-list li ul {display: none;}
.nav-list li a:hover {text-decoration: none; display: block; padding: 30px 7px 20px 7px; color: #000; font-size: .9em; font-weight: bold; background-color: #e7e4e4;}
.nav-list li a:hover li{display: block; position: absolute; margin: 0; padding: 0;}
.nav-list li a:hover li{float: left;}
.nav-list li:hover li a{ background-color: #333; border-bottom: 1px solid #fff; color: #FFF;}
<ul class="nav-list" id="navigation"><!--Menu list-->
<li>Home</li>
<li>About Us</li>
<li> <ul>Members
<li>Board of Directors</li>
<li>Committee</li>
</ul></li> <li>Join Us</li> <li>Events</li>
<li>Rules & Guidelines</li> <li>Archive</li> <li>Contact Us</li> <li>Login</li> </ul><!--ENDS Menu list-->
Your code doesn't really do much when you copy it directly into a jsfiddle (a website used to 'mock up' and test your HTML, CSS and JS), and there's not much description about what you are expecting/wanting to happen, so there's going to be some guess work here...
I think there are 2 problems here.
The HTML
First, the <li> that has the subnavigation <ul> inside it has some incorrect html.
The Members needs to be before the <ul>, not inside it.
So it should look like this:
<li>
Members
<ul>
<li>Board of Directors
</li>
<li>Committee
</li>
</ul>
</li>
The CSS
Second, you are trying to display the subnavigation <ul> (by default this is display: none) when you hover on the <a> tag, but you need to do it when you hover the <li>, because it's that element that contains the subnavigation <ul>.
So it should look like this:
.nav-list li ul {
display: none;
list-style: none;
margin: 0;
padding: 0;
position: absolute;
}
.nav-list li:hover ul {
display: block;
}
Here's a fiddle with my tweaks, hopefully that'll get you back on track.
http://jsfiddle.net/davidpauljunior/ve9Ub/
I have a drop down menu list made in css and plain HTMl. It works fine but it rolls under my image slider , and I can see on a part of the menu when i hover on any of my menu. I think z-index property is missing somewhere. But I used in my ul li tag but no use.
html
<ul id="menu">
<li>Home</li>
<li>About Us
<ul>
<li>The Team</li>
<li>History</li>
<li>Vision</li>
</ul>
</li>
<li>Products
<ul>
<li>Cozy Couch</li>
<li>Great Table</li>
<li>Small Chair</li>
<li>Shiny Shelf</li>
<li>Invisible Nothing</li>
</ul>
</li>
<li>Contact
<ul>
<li>Online</li>
<li>Right Here</li>
<li>Somewhere Else</li>
</ul>
</li>
</ul>
css
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: #2C5463;
margin-left: 1px;
white-space: nowrap;
}
ul li a:hover { background: #617F8A; }
li:hover ul {
display: block;
position: absolute;
}
li:hover li {
float: none;
font-size: 11px;
}
li:hover a { background: #617F8A; }
li:hover li a:hover { background: #95A9B1; }
image slider has z-index property from java-script. so it will take high priority.
You need to give z-index to your navigation also.
Is your image slider having jQuery?
Than you have to put z-index in li ul li{z-index:999px;}.
If you add z-index to the following class.
li:hover ul {
display: block;
position: absolute;
z-index:1000;
}
Also make sure that the z-index for the menu is having higher property.
I have been trying different things and still have been able to get exactly what I wanted. I want to create a menu. I want the menu to be about 20px wide and the dropdown to be 80-100px.
<div id="menulevel2grouping2">
<ul>
<li>T
<ul>
<li>Type 1</li>
<li>Type 2</li>
<li>Type 3</li>
<li>Type 4</li>
</ul>
</li>
<li>S
<ul>
<li>Small</li>
<li>Medium</li>
<li>Large</li>
</ul>
</li>
</ul>
</div>
#menulevel2grouping2{
margin: 0px;
padding: 0px;
}
#menulevel2grouping2 ul{
margin: 0px;
padding: 0px;
line-height: 30px;
}
#menulevel2grouping2 li{
margin: 0px;
padding: 0px;
list-style: none;
float: left;
position: relative;
}
#menulevel2grouping2 ul li a{
text-align: center;
font-family: Helvetica, Verdana Arial, sans-serif;
font-size: 12px;
background-color: blue;
width: 60px;
text-decoration: none;
display: block;
color: #000000;
}
#menulevel2grouping2 ul ul{
position: absolute;
display: none;
top: 30px
}
#menulevel2grouping2 ul li:hover ul{
display: block;
}
Any ideas on how I could get it done?
Thanks!
Drjay
Is this the effect you want? See fiddle here: http://jsfiddle.net/scTgZ/
I have revised your css. See the fiddle.
First of all, whenever possible you should provide a class or id to your elements to avoid complications. That's the purpose of CSS, to facilitate things, not to complicate them.
Anyway, something like this should work:
#menulevel2grouping2 ul li{width:20px; position:relative; z-index:10}
#menulevel2grouping2 ul li ul li{width:200px; position:relative; z-index:10}
you may not need the position and z-index (I'm not testing it) and you may need some float for the upper li, but this should be more than enough to give you an idea