CSS menu with 3 nested lists - align left problems - html

I have a navigation menu with 3 sub levels. I need the 3rd level list to align left but I can't get it to behave. I can get levels 2 and 3 to align relatively with the main menu but I want the 2nd level to align relatively and the 3rd level aligning completely to the left. I'd appreciate any help/advice going, here is what I have:
JSFIDDLE HERE
HTML:
<ul class="nav">
<li>LINK1</li>
<li>LINK2
<ul class="#">
<li>LINK2a
<ul class="#">
<li>LINK2b</li>
<li>LINK2c</li>
<li>LINK2d</li>
<li>LINK2e</li>
<li>LINK2f</li>
</ul>
</li>
</ul>
</li>
<li>LINK3</li>
<li>LINK4</li>
<li>LINK5</li>
</ul>
CSS:
a, img { border:none; }
* { margin:0; padding:0; }
.nav{
display:block;
list-style:none;
text-align: left;
position: relative;
height: 30px;
}
.nav li {
display: block;
position: relative;
float: left;
border: 1px solid #FFF;
}
.nav li a {
width: 125px;
height: 30px;
display: table-cell;
vertical-align: middle;
text-decoration: none;
font-size: 12px;
font-family: Arial, Verdana, Helvetica, sans-serif;
text-align: center;
color: #000;
z-index: 2000;
background-color: #999;
}
li.active {
background-color: #999 ;
color: #000;
}
.nav li a:hover {
top: 30px;
}
.nav li:hover > a {
color:#fff;
background-color:#333;
}
.nav li ul {
width: 125px;
height: 25px;
position: absolute;
left: -9999px;
border: 1px solid #FFF;
}
.nav li:hover ul {
left: -2px;
}
.nav li li:hover > a {
color:#000;
background-color:#999;
}
.nav li li{
position: relative;
border: 1px solid #FFF;
height: 25px;
}
.nav li li a {
height: 25px;
top: 25px;
text-decoration: none;
font-size: 9px;
font-family: Arial, Verdana, Helvetica, sans-serif;
text-align: center;
display: table-cell;
vertical-align: middle;
color: #000;
z-index: 2000;
background-color: #999;
}
li li.active {
background-color: #a9d9d9 ;
color: #007f7b;
}
.nav li li:hover ul {
left: -2px;
top: 25px;
}
ul.nav li > ul {
width: 700px;
height: 25px;
background-color:blue;
}
ul.nav li > ul > li > ul {
background-color:red;
height: 25px;
}
ul.nav li > ul > li { display: none; }
ul.nav li:hover li { display: block; }

Well, you are missing some simple changes CSS to make it work properly. I'll post what changes I made here to make it work:
.nav li ul became .nav li > ul
.nav li:hover ul became .nav > li:hover > ul
.nav li li:hover ul became .nav li > li:hover ul
ul.nav li > ul > li { display: none; } became ul.nav li ul ul { display: none; }
ul.nav li:hover li { display: block; } became ul.nav li:hover > ul { display: block; }
Added:
ul.nav li ul ul {
position: absolute;
top:25px;
left: -129px;
}
ul.nav li li:hover > a {
color: white;
background-color: #333
}

Related

How to align the drop up menu

I've basically copy/pasted the code from Drop-down menu that opens up/upward with pure css
body {
padding: 3em;
}
#menu>li:hover ul {
display: block;
}
#menu * {
padding: 0;
margin: 0;
font: 12px georgia;
list-style-type: none;
}
#menu {
float: center;
text-align: center;
line-height: 10px;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background: #B0BD97;
}
#menu ul {}
#menu ul li ul li a:hover {
background: #ECF1E7;
padding-left: 9px;
border-left: solid 1px #000;
}
#menu ul li ul li {
width: 140px;
border: none;
color: #B0BD97;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 3px;
padding-right: 3px;
background: #B0BD97;
z-index: 1;
}
#menu ul li ul li a {
font: 11px arial;
font-weight: normal;
font-variant: small-caps;
padding-top: 3px;
padding-bottom: 3px;
}
#menu ul li {
float: left;
width: 146px;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background: #979E71;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
}
#menu li {
position: relative;
float: left;
}
#menu ul li ul,
#menu:hover ul li ul,
#menu:hover ul li:hover ul li ul {
display: none;
list-style-type: none;
width: 140px;
bottom: 0;
}
#menu:hover ul,
#menu:hover ul li:hover ul,
#menu:hover ul li:hover ul li:hover ul {
display: block;
}
#menu:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 145px;
margin-top: -22px;
font: 10px;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom: 100%;
border-bottom: 1px solid transparent
}
<iframe style="width:100%">padding</iframe>
<div id="menu" style="text-align:center; float: center; width: 100%">
<ul>
<li>
<center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
No matter what I do, I cannot align the menu to sit in the center of the web page.
Your help would be highly appreciated.
Thanks in advance.
Like this? I added the following css:
#menu {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
body {
padding: 3em;
}
#menu {
position: absolute;
left: 50%;
transform:translate(-50%, 0);
}
#menu>li:hover ul {
display: block;
}
#menu * {
padding: 0;
margin: 0;
font: 12px georgia;
list-style-type: none;
}
#menu a {
display: block;
text-decoration: none;
color: #3B5330;
}
#menu a:hover {
background: #B0BD97;
}
#menu ul {}
#menu ul li ul li a:hover {
background: #ECF1E7;
padding-left: 9px;
border-left: solid 1px #000;
}
#menu ul li ul li {
width: 140px;
border: none;
color: #B0BD97;
padding-top: 3px;
padding-bottom: 3px;
padding-left: 3px;
padding-right: 3px;
background: #B0BD97;
z-index: 1;
}
#menu ul li ul li a {
font: 11px arial;
font-weight: normal;
font-variant: small-caps;
padding-top: 3px;
padding-bottom: 3px;
}
#menu ul li {
float: left;
width: 146px;
font-weight: bold;
border-top: solid 1px #283923;
border-bottom: solid 1px #283923;
background: #979E71;
}
#menu ul li a {
font-weight: bold;
padding: 15px 10px;
}
#menu li {
position: relative;
float: left;
}
#menu ul li ul,
#menu:hover ul li ul,
#menu:hover ul li:hover ul li ul {
display: none;
list-style-type: none;
width: 140px;
bottom: 0;
}
#menu:hover ul,
#menu:hover ul li:hover ul,
#menu:hover ul li:hover ul li:hover ul {
display: block;
}
#menu:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 145px;
margin-top: -22px;
font: 10px;
}
#menu:hover ul li:hover ul {
position: absolute;
margin-top: 1px;
font: 10px;
}
#menu>ul>li:hover>ul {
bottom: 100%;
border-bottom: 1px solid transparent
}
<iframe style="width:100%">padding</iframe>
<div id="menu">
<ul>
<li>
<center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
You can horizontally center #menu by following CSS rules:
define #menu's display as inline-block
Add a container div outside of #menu and make its text-align as center.
I've made a fiddle for your code, please check.
Update your HTML code as below.
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<link rel = "stylesheet" type = "text/css" href = "menu.css"/>
<title>Frames Test</title>
</head>
<body>
<iframe style="width:100%">padding</iframe>
<div class="container" style="text-align:center">
<div id="menu" style="text-align:center; float: center;display:inline-block">
<ul>
<li><center>item1</center>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</li>
</ul>
</div>
</div>
</body>
</html>

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>

Center navigation Menu in wordpress

I'm working on old Wordpress page created with myMag theme, and I need to centralize menu bar, I'm pretty new to CSS and looking for various solutions I cant find any solutions for my case.
the CSS file looks like this:
#navcontainer #navbar {
float: left;
height: 51px;
line-height: 50px;
margin-left: -15px;
margin-top: 1px;
}
margin:0;
padding:0;
width:900px;
list-style-type:none;
display:table;
position:relative;
line-height:50px;
z-index:5;
ul {
text-align: justify
}
li {
display: inline-block
}
#navcontainer #navbar a {
height: 40px;
display: block;
padding: 0 21px;
text-decoration: none;
text-align: center;
line-height: 28px;
outline: none;
float: left;
z-index: 35;
position: relative;
color: #FFF;
text-shadow: 1px 1px 0 #515151;
font-weight: bold;
}
#navcontainer #navbar a:hover {
color: #CCC;
}
#navcontainer #navbar ul a {
line-height: 30px;
}
#navcontainer #navbar li {
float: left;
position: relative;
z-index: 20;
margin-top: 10px;
}
#navcontainer #navbar li li {
border-left: none;
margin-top: 0;
}
#navcontainer #navbar ul {
position: absolute;
display: none;
width: 172px;
top: 38px;
left: -1px;
background: #f5f5f5;
}
#navcontainer #navbar li ul a {
width: 130px;
height: auto;
float: left;
text-align: left;
padding: 0 21px;
color: #606060;
text-shadow: none;
}
#navcontainer #navbar ul ul {
top: auto;
}
#navcontainer #navbar li ul ul {
left: 172px;
top: 0px;
}
#navcontainer #navbar li ul ul a {
border-left: 1px solid #CCC;
}
#navcontainer #navbar ul {
border-top: none;
}
#navcontainer #navbar ul a,
#navcontainer #navbar ul li {
background-color: #e7e7e7;
}
#navcontainer #navbar ul a:hover,
#navcontainer #navbar ul a:focus {
color: #18649a;
}
#navcontainer #navbar li:hover ul ul,
#navcontainer #navbar li:hover ul ul ul,
#navcontainer #navbar li:hover ul ul ul ul {
display: none;
}
#navcontainer #navbar li:hover ul,
#navcontainer #navbar li li:hover ul,
#navcontainer #navbar li li li:hover ul,
#navcontainer #navbar li li li li:hover ul {
display: block;
}
php that creates menu looks like this:
<div id="navcontainer">
<div class="navigation">
<div class="wrapper">
<ul id="navbar">
<?php $exclude = $myOptions['pages_excl'];?>
<?php if(!$myOptions['pages_limit']) {
$limit = '10'; } else {
$limit = $myOptions['pages_limit'];
}
if($myOptions['pages_limit'] == '0') {
$limit = 0;
}
?>
<li>Home</li>
<?php wp_list_pages("exclude=$exclude&title_li=&depth=3&number=$limit"); ?>
</ul>
</div>
</div>
</div>
You need to give the #navbar a fixed width and change the margin as following:
#navcontainer #navbar {
width: 600px; // change this to a width that suits your menu
height: 51px;
line-height:50px;
margin-left: auto;
margin-right: auto;
margin-top: 1px;
}
margin-left:auto; and margin-right: auto; center the navigation, this can only be done if the navigation has a fixed width. So you will need to adjust the fixed width when you change the number of items in the navigation.

css menu hover text colour

i have this CSS:
.nav > li:hover {
background: #4D2177;
color:#FFFFFF;
text-decoration:none;
}
.active {
background: #4D2177;
color:#FFFFFF;
text-decoration:none;
}
nav, ul, li, a {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
.container {
width: 100%;
margin: 10px auto;
}
.toggleMenu {
display: none;
padding: 10px 15px;
color: #4D2177;
width:100%;
text-align:center;
}
.nav {
list-style: none;
*zoom: 1;
text-align: right;
}
.nav:before,.nav:after {
content: " ";
display: table;
}
.nav:after {
clear: both;
}
.nav ul {
list-style: none;
width: 12em;
}
.nav a {
padding: 10px 15px;
color:#4D2177;
}
.nav li {
position: relative;
text-align: left;
}
.nav > li {
display: inline-block;
}
.nav > li > .parent {
background-image: url("/images/downArrow.png");
background-repeat: no-repeat;
background-position: right;
}
.nav > li > a {
display: block;
}
.nav li ul {
position: absolute;
left: -9999px;
}
.nav > li.hover > ul {
left: 0;
color:#FFFFFF;
}
.nav li li.hover ul {
left: 100%;
top: 0;
}
.nav li li a {
display: block;
background: #4D2177;
color:#FFFFFF;
position: relative;
z-index:100;
border-top: 1px solid #ffffff;
}
.nav li li li a {
background:#f36f25;
color:#ffffff;
z-index:200;
border-top: 1px solid #ffffff;
}
#media screen and (max-width: 600px) {
.active {
display: block;
}
.nav {
border-top: none;
}
.nav > li {
display: block;
border-top: 1px solid #ffffff;
}
.nav > li > .parent {
background-position: 95% 50%;
}
.nav li li .parent {
background-image: url("/images/downArrow.png");
background-repeat: no-repeat;
background-position: 95% 50%;
}
.nav ul {
display: block;
width: 100%;
}
.nav > li.hover > ul , .nav li li.hover ul {
position: static;
}
}
and this HTML:
<a class="toggleMenu" href="#">Menu</a>
<ul class="nav">
<li><span>Homepage</span></li>
<li><span>News</span></li>
<li><span>Sections</span>
<ul>
<li><span>Beavers</span></li>
<li><span>Cubs</span></li>
<li><span>Scouts</span></li>
<li><span>Explorers</span></li>
</ul>
</li>
<li><span>Gallery</span></li>
<li><span>Events</span></li>
<li><span>Contact Us</span></li>
</ul>
for a horizontal menu but the hover colour for the text isn't changing to white (#FFFFFF)
how can i get this working?
You are targetting li but a doesn't inherit the color by default unless you specify to inherit by declaring color: inherit; so use the below selector instead
.nav > li:hover a {
color: #fff;
}
The above selector will target a element on hover of li
Where on the other hand you can also use the selector below
.nav > li a:hover {
color: #fff;
}
This will change the color of a element on :hover of a
If you want to specify the inheritance.. you can do it like
.nav > li a {
color: inherit;
}
And than you can use the selector you were using, and it should work...
.nav > li > a:hover {
background: #4D2177;
color:#FFFFFF;
text-decoration:none;
}
add " > a:hover to the first line of the CSS.
You were overwriting it further down. Here is the fiddle.
http://jsfiddle.net/g6QMB/
Use the :hover selector.
The CSS:
.nav li a:hover {
color:#ffffff;
}
The fiddle.
add this class:
ul.nav li:hover a{
color:#fff;
}
jsFiddle

Sublevels in Drop Down Menu Stack on top of One Another Instead of Displaying Verticly

Recently a few months ago I had to add sublevel functionality into a drop down menu on one of our sites. The tactic I took before worked well for the one column in the navigation, but I was asked to add a sublevel to the column before it which didn't work because I was using relative positioning (see the example below):
<style type="text/css">
#div#mycontent { overflow: visible; }
#nav ul { font-family: Arial, Verdana; font-size: 10px; margin: 0; padding: 0; list-style: none; font-weight: bold; }
#nav ul li { display: block; float: left; margin: 0;}
#nav li ul { display: none; }
#nav ul li a { display: block; text-decoration: none; color: #3c1c4e; border-top: 1px solid #ffffff; padding: 5px 15px 5px 15px; background: #f0e8d8; margin-left: 1px; white-space: nowrap; }
#nav ul li a:hover { background: #f0e8d8; }
#nav li:hover ul { display: block; position: absolute; }
#nav li:hover li { float: none; font-size: 11px; }
#nav li:hover a { background: #f0e8d8; }
#nav li:hover li a:hover { background: #fff7e7; }
/* This is for sublevels in the drop down */
#nav li:hover ul li ul {display: none}
#nav li ul li:hover ul { display: block; }
#nav li ul li ul li { position: relative; left: 188px; bottom:25px ;padding-left:1px }
So I modified the sublevels in the drop down menu to use relative positioning used an overlap approach (due to the way to previous coder originally designed the drop down). The new code looks like the one below:
#nav li ul li ul li { position: absolute; left: 125px; bottom: 0px; border-style: solid; border-width: 1px; border-color:purple; z-index: 1; }
However as the title indicates the LI under the unordered list are now stacking on top of one another. Instead of displaying vertically one after the other. I believe it requires me to clear the float, but it looks like it was done up above. So I'm unsure if I need to redefine the float then clear it in order to make sure the links in the sub list will display vertically.
Edit:
A good thought to add the HTML to show how I'm trying to execute this:
<html>
<head>
<style type="text/css">
#div#mycontent { overflow: visible; }
#nav ul { font-family: Arial, Verdana; font-size: 10px; margin: 0; padding: 0; list-style: none; font-weight: bold; }
#nav ul li { display: block; float: left; margin: 0;}
#nav li ul { display: none; }
#nav ul li a { display: block; text-decoration: none; color: #3c1c4e; border-top: 1px solid #ffffff; padding: 5px 15px 5px 15px; background: #f0e8d8; margin-left: 1px; white-space: nowrap; }
#nav ul li a:hover { background: #f0e8d8; }
#nav li:hover ul { display: block; position: absolute; z-index: 0;}
#nav li:hover li { float: none; font-size: 11px; }
#nav li:hover a { background: #f0e8d8; }
#nav li:hover li a:hover { background: #fff7e7; }
/* This is for sublevels in the drop down */
#nav li:hover ul li ul {display: none}
#nav li ul li:hover ul { display: block; }
#nav li ul li ul li { position: absolute; left: 125px; bottom: 0px; border-style: solid; border-width: 1px; border-color:purple; z-index: 1; }
</style>
</head>
<body>
<div id="nav">
<ul id="menu">
<li>Column 1
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</li>
<li> Column 2
<ul>
<li>Link 1</li>
<li>Link 2</li>
</ul>
</li>
<li>Column 3<li>
</ul>
</div>
</body>
</html>
Try these CSS rules for your sublevels in the drop down:
/* This is for sublevels in the drop down */
#nav li:hover ul li ul {
display: none
}
#nav li ul li:hover ul {
display: block;
position:absolute;
top:0;
left:100%;
}
#nav li ul li ul li {
position:relative;
display: block;
float: left;
border: 1px solid purple;
z-index: 1;
}