I'm trying to modify This project of Osvaldas Valutis adding a 3rd and 4th level, but my css knowledge is not so deep and the result, as you can see HERE is very poor:
besides HTML, on CSS side I added
* third level */
#nav li li li{
background-color: #cc470d;
display: none;
position: absolute;
top: 100%;
}
#nav li li:hover li {
display: block;
left: 2px;
top:0px;
right: 0;
}
#nav li:not( :first-child ):hover ul li ul li {
left: 240px;
top:-50px;
}
#nav li ul li ul a {
font-size: 1.25em; /* 20 */
border-top: 1px solid #e15a1f;
padding: 0.75em; /* 15 (20) */
}
#nav li ul li ul li a:hover, #nav li ul li ul:not( :hover ) li.active a {
background-color: #e15a1f;
}
but clearly it is not enough, and then I have to handle also the #media versions.
can please suggest what need to add
You could apply fixed widths to the dropdowns, then adjust the margins to position them correctly.
Change your code for the third level #nav li li li to this:
/* third level */
#nav li li li {
background-color: #cc470d;
display: none;
position: absolute;
top: 100%;
width: 240px; /* will also set width for child dropdown */
margin-top: -1px;
margin-left: 2px;
}
And add this for the fourth level:
/* fourth level */
#nav li li li li {
margin-left: 1px;
}
Related
I have been working on navigation menus for almost 10 days. Here is the link from which I implemented navigation menu. It is working fine. But I couldn't understand the code properly, spacially "position property
"What is the reason for assigning position:relative to the "outer ul" and assigning position:absolute to the "two inner uls(unorder lists)"?
* {
margin: 0px;
padding: 0px;
}
#menu ul {
list-style: none;
}
#menu ul li {
background-color: red;
border: 1px solid white;
width: 120px;
height: 35px;
text-align: center;
line-height: 30px;
float: left;
position: relative;
}
#menu ul li a {
text-decoration: none;
color: white;
display: block;
}
#menu ul li a:hover {
background-color: green;
}
#menu ul ul {
position: absolute;
display: none;
}
#menu ul li:hover>ul {
display: block;
}
#menu ul ul ul {
margin-left: 120px;
top: 0px;
}
<body>
<div id="menu">
<ul>
<li>one</li>
<li>two
<ul>
<li>two.one</li>
<li>two.two</li>
<li>two.three</li>
<li>two.four</li>
<li>two.five</li>
</ul>
</li>
<li>three</li>
<li>four</li>
<li>five</li>
</ul>
</div>
Start by understanding the concepts: CSS Position documentation especially look at the absolute: "It is positioned relative to its closest positioned ancestor," and relative: "The element is positioned according to the normal flow of the document"
So the your outer element (a ul in this case) is positioned relative to it's ancestor (the <div id="menu">) then the child is positioned absolute from that ancestor. The same thing where a ul is inside a ul - it targets the inner ul when you have #menu ul ul in the CSS.
Here is a general breakdown:
/* remove all the padding and margins that the browser sets by default */
* {
margin: 0px;
padding: 0px;
}
/* remove the normal display for a ul as a bullet */
#menu ul {
list-style: none;
}
/* style and position the inner li relative to the parent ul */
#menu ul li {
background-color: red;
border: 1px solid white;
width: 120px;
height: 35px;
text-align: center;
line-height: 30px;
float: left;
position: relative;
}
/* make a link 'a' element show as a block and style it a bit */
#menu ul li a {
text-decoration: none;
color: white;
display: block;
}
#menu ul li a:hover {
background-color: green;
}
/* position the inner ul absolute to the outer ul (but hide it with the 'display: none' */
#menu ul ul {
position: absolute;
display: none;
}
/* when we hover over a li, show the child ul */
#menu ul li:hover>ul {
display: block;
}
/* position the innermost ul from its acestor at the top with a 120px margin */
#menu ul ul ul {
margin-left: 120px;
top: 0px;
}
I'm having some issues with my menu being displayed properly. My 3rd level menu items are hidden behind the others for some reason. I've gone over my template I'm using and things seem to be alright on that end, otherwise the menu option wouldn't even be displayed. Thus I'm thinking something is wrong with my CSS, though I can't see what that would be. Does anyone have any ideas as to what's going on here? You can see an example at http://www.bpwsaskatoon.com and then hovering on the "Membership" option at the top.
The problem is in the css. First you should use classes or id:s on your navigation styles. Example ul li is now targeting every ul li. By using #nav ul li will target only all ul li inside #nav. In html the ul in .main_nav_menu should be div. You have now ul directly under ul.
I would recommend that you try some jquery plugin for dropdown menus. Example Superfish is pretty good, it takes care of many thigs that you should consider in dropdown menus, example touch events.
Here is a quick css that should display the 3rd level menu items.
/*Navigation styles*/
#nav{
display:table;
margin:0 auto 0 auto;
position:relative;
padding:5px;
}
#mnwrpr{
height:48px;
box-shadow: 0 1px 2px rgba(0,0,0,0.5);
clear: both;
display: block;
position: relative;
width: 100%;
z-index:1;
}
#nav ul {
font-family:'Lato', sans-serif;
font-size: 15px;
margin: 0;
padding: 0;
}
#nav ul li {
display: block;
position: relative;
float: left;
}
#nav li ul {
display: none;
}
#nav ul li a {
display: block;
text-decoration: none;
color: #8b8b8b;
padding: 5px 15px 5px 15px;
margin-left: 1px;
white-space: nowrap;
}
#nav li:hover ul {
display: block;
position: absolute;
}
#nav li:hover li {
float: none;
font-size: 14px;
}
#nav li:hover li { background: #ececec; }
#nav li:hover li a:hover {
background: #bcbcbc;
}
#nav ul li ul li ul {
top: 0;
left: 100%;
z-index: 99;
min-width: 12em;
position: absolute;
margin: 0;
padding: 0;
list-style: none;
display: none!important;
}
#nav ul li ul li:hover ul {
display: block!important;
}
#nav ul li ul li ul li {
position: relative;
margin: 0;
padding: 0;
list-style: none;
}
I'm trying to style menu on my website but I'm stuck with coloring and hover effects.
It looks now as below (/user is where cursor was when I took screenshot):
Screenshot
What I need to change is:
when I hover 2nd level item, 1st level becomes "default" (without hover effect)
My CSS for this menu is:
#nav {
text-align: center;
font-size: 1.5em;
font-weight: 700;
letter-spacing: 1px;
}
#nav ul ul {
display: none;
}
#nav ul li:hover > ul {
display: block;
}
#nav ul {
display: inline-table;
list-style: none outside none;
padding: 0 10px;
position: relative;
}
#nav ul:after {
clear: both;
content: "";
display: block;
}
#nav ul li {
float: left;
background: none repeat scroll 0 0 #F5F5F5;
margin-right: 3px;
}
#nav ul li:hover {
background: none repeat scroll 0 0 #E32D40;
}
#nav ul li a {
color: #757575;
display: block;
padding: 10px;
text-decoration: none;
}
#nav ul li a:hover {
color: #FFFFFF;
}
#nav ul ul {
border-radius: 0 0 0 0;
padding: 0;
position: absolute;
top: 100%;
}
#nav ul ul li {
margin-top: 3px;
float: none;
position: relative;
color: #757575;
}
#nav ul ul li a {
color: #757575;
display: block;
padding: 10px;
text-decoration: none;
}
#nav ul ul li a:hover {
}
#nav ul ul ul {
left: 100%;
position: absolute;
top: 0;
}
I'm not sure, if it is possible with only css. As you are displaying the 2nd level with #nav ul li:hover > ul and you are highlighting the 1st with #nav ul li:hover, which are both firing on 2nd level li hover (and there is no parent call in css).
It is quite easy though with jQuery:
$('#nav > ul > li > ul > li').hover(function () {
$(this).parents('li').first().css('background', 'none');
});
Check here: http://jsfiddle.net/balintbako/EruTP/
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Here where my css menu is: My css menu
Notice if you hover over "about us" like below you see the "our clergy" sub menu already out. I don't want that I want it when you hover over "our clergy" for it to show.
This screenshot above is from firefox, while webkit browsers show a 1-2px difference as seen below and I can't figure out why? It sometimes effects how the menu works too.
Here is my css for the menu (I'm using wordpress so that means there is no html):
Feel free to help me out and if you want to clean up the css you can too!
#navbar {
height: 40px;
padding-left: 10px;
margin-left:-10px;
margin-top: -29px;
margin-bottom:0;
background: #F4DE9F;
width:930px;
/*backgroundborder-top: 2px solid #F4DE9F;
border-bottom: 2px solid #F4DE9F;*/
}
#navbar li {
float: left;
list-style: none;
margin-bottom: 30px;
margin-left:-20px;
}
#navbar li a {
font-family: "MuseoSans_500";
color: #3C290B;
font-weight: 500;
text-transform: uppercase;
}
#navbar li:hover {
background:rgba(255, 241, 194, 100); /*#FFF1C2;*/
color: #645548;
text-decoration: none;
}
#navbar li a:hover {
background:rgba(255, 241, 194, 100); /*#FFF1C2;*/
color: #645548;
text-decoration: none;
}
#navbar .parent > a, #navbar .parent > a:hover {
background: #F4DE9F;
background-position: right;
background-repeat: no-repeat;
}
#navbar ul, #navbar ul li {
display: inline;
list-style: none;
margin: 0;
padding: 0;
}
#navbar ul li a {
display: inline-block;
padding: 11px 16.2px 8px;
text-decoration: none;
}
/*
#navbar ul li a:hover {
}
#navbar ul #first a:hover {
}*/
#navbar ul li {
position: relative;
}
#navbar li ul {
display: none;
left: 11px;
position: absolute;
top: 51px;
}
#navbar li ul a {
background: #F4DE9F;
}
#navbar ul ul ul li {
}
#navbar ul li:hover ul {
display: inline-block;
}
#navbar ul li:hover ul, #navbar ul ul li:hover ul, #navbar ul ul ul li:hover ul {
display: block;
margin: -11px 0 0 -11px;
}
/*#navbar ul li:hover ul li a, #navbar ul ul li:hover ul li a, #navbar ul ul ul li:hover ul li a {
display: block;
}*/
#navbar ul li:hover ul ul, #navbar ul ul li:hover ul ul {
margin-top: -50px;
margin-left:129px;
}
#navbar ul li:hover ul li a {
padding: 10px 14px 8px;
width: 112px;
}
#navbar ul li:hover ul ul li a {
padding: 10px 14px 8px;
width: 112px;
}
/*#navbar ul ul ul li:hover ul li a {
padding: 0 16px 0 24px;
width: 140px;
}*/
#navbar .children li a:hover {
color: #000;
}
When you end up with a stylesheet that feels very bloated with several rules and a lot of specificity, such as #navbar ul ul ul li:hover ul it is usually good to take a step back see if you can simplify the rules a bit.
Since you are using Wordpress it comes with a lot of handy classes that makes the job easier. In this case .menu-item and .sub-menu.
Replacing the menu-css with the following styles solves the problems you mentioned in your question, tried in Chrome 23, Safari 6 and Firefox 16.
Example here: http://jsfiddle.net/5qEwH/
.menu-item {
display: inline-block;
position: relative;
font-family: "MuseoSans_500";
font-weight: 500;
text-transform: uppercase;
background: #F4DE9F;
}
.menu-item:hover {
background: #FFF1C2;
}
.menu-item a {
display: inline-block;
height: 20px;
padding: 10px;
text-decoration: none;
color: #645548;
}
/* Hide submenus by default */
.sub-menu {
display: none;
position: absolute;
width: 150px;
top: 40px;
}
.sub-menu .menu-item {
width: 100%;
}
/* The second level sub-menu should be moved to the right */
.sub-menu .menu-item > .sub-menu {
top: 0;
left: 150px;
}
/* Show submenus on hover */
.menu-item:hover > .sub-menu {
display: block;
}
Its a great CSS practice to normalize your styles first so that all the browsers get the same styles for some basic HTML elements.
I add this at the start of a Stylesheet
* { margin: 0px; padding: 0px; border: 0px; text-decoration: none }
There is also a comprehensive stylesheet file for normalization that covers all the browsers and even html5 as well. try to add this before your style.css file.
Here is the link: http://necolas.github.com/normalize.css/
For the hover issue, try this css change
Replace
#navbar ul li:hover ul {
display: inline-block;
}
#navbar ul li:hover ul, #navbar ul ul li:hover ul, #navbar ul ul ul li:hover ul {
display: block;
margin: -11px 0 0 -11px;
}
With
#navbar ul li:hover > ul {
display: inline-block;
}
#navbar ul li:hover > ul, #navbar ul ul li:hover > ul, #navbar ul ul ul li:hover > ul {
display: block;
margin: -11px 0 0 -11px;
}
Hope it helps :)
I'm almost certain this question has been answered in one form or another. Applying the changes I've found here and elsewhere doesn't seem to get me any further.
I'm trying to change the css menu from crisislab.nl from a drop-down menu to a drop-up menu. (As you can see on the site I'm currently working on it.)
Current problem I'm encountering is the fact that menu seems to be working fine, expect for the fact that the menu text is displayed downwards and while the menu is moving up (If this doesn't sound logical, please look at crisislab.nl)
See the code below for my current progress. Anyone willing to assist?
Many thanks in advance!
#navigation {
width: 980px;
height: 38px;
}
#navigation li {
float: left;
position: relative;
top: 220px;
} #navigation li:hover { background: transparent url(gfx/navigation_hover.png) repeat; }
#navigation li a {
text-transform: uppercase;
color: white;
padding: 13px 33px;
line-height: 38px;
font-size: 11px;
}
#navigation li a:hover { text-decoration: none;}
#navigation li ul {
position: absolute;
background: transparent url(gfx/navigation_hover.png) left top repeat;
z-index: 1000;
min-width: 100%;
display:none;
left:-1px;
}
#navigation li:hover ul {
display:block;
}
#navigation li ul li {
background: none;
width: 100%;
}
#navigation li ul li:hover {
background: none;
background-color: #2a51b5;
}
#navigation li ul li a {
text-transform: uppercase;
color: white;
padding-left: 8px 10px;
line-height: 28px;
width: 100%;
display:block;
}
The basic difference from a dropdown to a dropup is the offset of the child ul:
Dropdowns have top:<x>px; and if you want a dropup you just say: bottom:<x>px;
I modified your code to make it work: http://jsfiddle.net/fJSVz/
Basically i changed the following rules:
#navigation li ul {
top: -9999px; /* <- removed */
display:none; /* <- this will trigger the hide/show */
}
#navigation li:hover ul {
bottom:20px; /* <- this is the trick to push the ul up */
display:block; /* <- show the ul on hover */
}