I'm trying to make a dropdown menu that opens when you hover over an item. But i have two problems:
When my mouse leaves the item the div goes away, so i'd like the div to be shown as long as i hover either over the item or the dropdownmenu.
This problem goes hand in hand with the first i believe. It's that the item text should keep a green color as long as you hover over it or the dropdown menu (currently it only does while you hover the item).
Here is the code i've tried so far:
#menu {
float: left;
}
#menu ul.top-menu {
list-style: none;
padding: 0;
}
#menu ul.top-menu li {
display: inline-block;
float: left;
margin: 0 5px;
position: relative;
font-family: 'Roboto', sans-serif;
font-size: 18px;
}
#menu ul.top-menu li a {
margin: 0;
padding: 0 5px;
font-weight: 400;
font-size: 18px;
color: black;
display: inline-block;
text-decoration: none !important;
outline: none;
}
#menu ul.top-menu li a:hover {
color: green;
}
#menu ul.top-menu li a:hover + div {
display: block;
}
#menu .sub-container {
display: none;
position: absolute;
top: 32px;
left: -20px;
width: 400px;
background: black;
border-top: 2px solid green;
z-index: 10;
}
#menu .sub-container ul.sub-menu {
padding: 10px;
overflow: hidden;
}
#menu .sub-container ul.sub-menu li {
width: 30%;
float: left;
margin: 0 1.65%;
}
#menu .sub-container ul.sub-menu li:hover {
background-color: green;
color:white;
}
#menu .sub-container ul.sub-menu li a {
color:white;
height: 30px;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
padding: 0 10px;
display: block;
border-bottom: none !important;
}
<div class="container">
<div id="menu">
<ul class="top-menu">
<li>
Sub Menu 1
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 1.1</a></li>
<li><a href='#'>Sub Option 1.2</a></li>
<li><a href='#'>Sub Option 1.3</a></li>
<li><a href='#'>Sub Option 1.4</a></li>
<li><a href='#'>Sub Option 1.5</a></li>
<li><a href='#'>Sub Option 1.6</a></li>
<li><a href='#'>Sub Option 1.7</a></li>
<li><a href='#'>Sub Option 1.8</a></li>
<li><a href='#'>Sub Option 1.9</a></li>
</ul>
</div>
</li>
<li>
Sub Menu 2
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 2.1</a></li>
<li><a href='#'>Sub Option 2.2</a></li>
<li><a href='#'>Sub Option 2.3</a></li>
<li><a href='#'>Sub Option 2.4</a></li>
<li><a href='#'>Sub Option 2.5</a></li>
<li><a href='#'>Sub Option 2.6</a></li>
<li><a href='#'>Sub Option 2.7</a></li>
<li><a href='#'>Sub Option 2.8</a></li>
<li><a href='#'>Sub Option 2.9</a></li>
</ul>
</div>
</li>
<li>
No Sub Menu 1
</li>
<li>
No Sub Menu 2
</li>
</ul>
</div>
</div>
Every kind of help is appreciated! Thanks in advance.
This will fix the main problem and also this will work with items color stays green while hover the submenu.
#menu {
float: left;
}
#menu ul.top-menu {
list-style: none;
padding: 0;
}
#menu ul.top-menu li {
display: inline-block;
float: left;
margin: 0 5px;
position: relative;
font-family: 'Roboto', sans-serif;
font-size: 18px;
}
#menu ul.top-menu > li {
padding-bottom: 10px;
}
#menu ul.top-menu li a {
margin: 0;
padding: 0 5px;
font-weight: 400;
font-size: 18px;
color: black;
display: inline-block;
text-decoration: none !important;
outline: none;
}
#menu ul.top-menu li:hover a {
color: green;
}
#menu ul.top-menu li:hover div {
display: block;
}
#menu .sub-container {
display: none;
position: absolute;
top: 32px;
left: -20px;
width: 400px;
background: black;
border-top: 2px solid green;
z-index: 10;
}
#menu .sub-container ul.sub-menu {
padding: 10px;
overflow: hidden;
}
#menu .sub-container ul.sub-menu li {
width: 30%;
float: left;
margin: 0 1.65%;
}
#menu .sub-container ul.sub-menu li:hover {
background-color: green;
color: white;
}
#menu .sub-container ul.sub-menu li a {
color:white;
height: 30px;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
padding: 0 10px;
display: block;
border-bottom: none !important;
}
<div class="container">
<div id="menu">
<ul class="top-menu">
<li>
Sub Menu 1
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 1.1</a></li>
<li><a href='#'>Sub Option 1.2</a></li>
<li><a href='#'>Sub Option 1.3</a></li>
<li><a href='#'>Sub Option 1.4</a></li>
<li><a href='#'>Sub Option 1.5</a></li>
<li><a href='#'>Sub Option 1.6</a></li>
<li><a href='#'>Sub Option 1.7</a></li>
<li><a href='#'>Sub Option 1.8</a></li>
<li><a href='#'>Sub Option 1.9</a></li>
</ul>
</div>
</li>
<li>
Sub Menu 2
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 2.1</a></li>
<li><a href='#'>Sub Option 2.2</a></li>
<li><a href='#'>Sub Option 2.3</a></li>
<li><a href='#'>Sub Option 2.4</a></li>
<li><a href='#'>Sub Option 2.5</a></li>
<li><a href='#'>Sub Option 2.6</a></li>
<li><a href='#'>Sub Option 2.7</a></li>
<li><a href='#'>Sub Option 2.8</a></li>
<li><a href='#'>Sub Option 2.9</a></li>
</ul>
</div>
</li>
<li>
No Sub Menu 1
</li>
<li>
No Sub Menu 2
</li>
</ul>
</div>
</div>
#menu {
float: left;
}
#menu ul.top-menu {
list-style: none;
padding: 0;
}
#menu ul.top-menu li {
display: inline-block;
float: left;
margin: 0 5px;
position: relative;
font-family: 'Roboto', sans-serif;
font-size: 18px;
}
#menu ul.top-menu li a {
margin: 0;
padding: 0 5px;
font-weight: 400;
font-size: 18px;
color: black;
display: inline-block;
text-decoration: none !important;
outline: none;
padding-top: 10px;
padding-bottom: 10px;
}
#menu ul.top-menu li a:hover {
color: green;
}
#menu ul.top-menu li:hover div.sub-container {
display: block;
}
#menu .sub-container {
display: none;
position: absolute;
top: 39px;
left: -20px;
width: 400px;
background: black;
border-top: 2px solid green;
z-index: 10;
}
#menu .sub-container ul.sub-menu {
padding: 10px;
overflow: hidden;
}
#menu .sub-container ul.sub-menu li {
width: 30%;
float: left;
margin: 0 1.65%;
}
#menu .sub-container ul.sub-menu li:hover {
background-color: green;
color:white;
}
#menu .sub-container ul.sub-menu li a {
color:white;
height: 30px;
line-height: 30px;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
padding: 0 10px;
display: block;
border-bottom: none !important;
}
<div class="container">
<div id="menu">
<ul class="top-menu">
<li>
Sub Menu 1
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 1.1</a></li>
<li><a href='#'>Sub Option 1.2</a></li>
<li><a href='#'>Sub Option 1.3</a></li>
<li><a href='#'>Sub Option 1.4</a></li>
<li><a href='#'>Sub Option 1.5</a></li>
<li><a href='#'>Sub Option 1.6</a></li>
<li><a href='#'>Sub Option 1.7</a></li>
<li><a href='#'>Sub Option 1.8</a></li>
<li><a href='#'>Sub Option 1.9</a></li>
</ul>
</div>
</li>
<li>
Sub Menu 2
<div class='sub-container'>
<ul class='sub-menu'>
<li><a href='#'>Sub Option 2.1</a></li>
<li><a href='#'>Sub Option 2.2</a></li>
<li><a href='#'>Sub Option 2.3</a></li>
<li><a href='#'>Sub Option 2.4</a></li>
<li><a href='#'>Sub Option 2.5</a></li>
<li><a href='#'>Sub Option 2.6</a></li>
<li><a href='#'>Sub Option 2.7</a></li>
<li><a href='#'>Sub Option 2.8</a></li>
<li><a href='#'>Sub Option 2.9</a></li>
</ul>
</div>
</li>
<li>
No Sub Menu 1
</li>
<li>
No Sub Menu 2
</li>
</ul>
</div>
</div>
Related
I am making a site with a navbar, but when I resize the page, sometimes one link goes on an extra line like this: Navbar Example
How can I fix this? Here is my code:
body {
margin: 0;
background-color: white;
}
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}
ul {
list-style-type: none;
margin: 0;
padding-left: 0px;
padding-right: 0px;
overflow: hidden;
background-color: #F27930;
box-shadow: 5px 0px 0px 0px 5px lightgrey;
}
li {
float: left;
font-family: roboto;
color: white;
}
li a.link {
display: block;
color: white;
text-align: center;
padding: 14px 14px;
text-decoration: none;
}
li a.current {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.divider {
display: block;
top: 50%;
font-size: 25px;
color: #FCD031;
text-align: center;
padding: 8px 2px;
text-decoration: none;
}
li a:hover {
background-color: #feae02;
}
li a.current:hover {
background-color: #F27930;
}
li a.divider:hover {
background-color: #F27930;
}
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<div class="sticky">
<ul>
<li><a class="current"><b>Jump to Article:</b></a></li>
<!-- <hr class="hrNav"> -->
<li><a class="link" href="#top">Top</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article1">Article 1</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article2">Article 2</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article3">Article 3</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article4">Article 4</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article5">Article 5</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article6">Article 6</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article7">Article 7</a></li>
</ul>
</div>
Any help would be much appreciated!
Thanks,
Jasper
The only way you can avoid the nav bar word wrapping is by setting a minimum width on the nav bar, or add a media query to change it when the screen is too small.
Using the min-width CSS attribute.
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width-device-width, initial-scale-1.0">
<title>Navbar Example</title>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<style>
body {
margin: 0;
background-color: white;
}
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}
ul {
list-style-type: none;
margin: 0;
padding-left: 0px;
padding-right: 0px;
overflow: hidden;
background-color: #F27930;
box-shadow: 5px 0px 0px 0px 5px lightgrey;
}
li {
float: left;
font-family: roboto;
color: white;
}
li a.link {
display: block;
color: white;
text-align: center;
padding: 14px 14px;
text-decoration: none;
}
li a.current {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.divider {
display: block;
top: 50%;
font-size: 25px;
color: #FCD031;
text-align: center;
padding: 8px 2px;
text-decoration: none;
}
li a:hover {
background-color: #feae02;
}
li a.current:hover {
background-color: #F27930;
}
li a.divider:hover {
background-color: #F27930;
}
</style>
</head>
<body>
<div class="sticky">
<ul style="min-width:884px">
<li><a class="current"><b>Jump to Article:</b></a></li>
<!-- <hr class="hrNav"> -->
<li><a class="link" href="#top">Top</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article1">Article 1</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article2">Article 2</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article3">Article 3</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article4">Article 4</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article5">Article 5</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article6">Article 6</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article7">Article 7</a></li>
</ul>
</div>
</body>
</html>
That is a padding issue.
So, there'll be 2 solutions.
use #media to let the menu contains breadcrumb when the browser width is small.
if you're going to force display the navigation items by line breaking, then use flex
Just add 2 lines to your ul style
ul {
...
display: flex;
flex-flow: row wrap;
}
So, the final result will be
body {
margin: 0;
background-color: white;
}
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
}
ul {
list-style-type: none;
margin: 0;
padding-left: 0px;
padding-right: 0px;
overflow: hidden;
background-color: #F27930;
box-shadow: 5px 0px 0px 0px 5px lightgrey;
}
li {
float: left;
font-family: roboto;
color: white;
}
li a.link {
display: block;
color: white;
text-align: center;
padding: 14px 14px;
text-decoration: none;
}
li a.current {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.divider {
display: block;
top: 50%;
font-size: 25px;
color: #FCD031;
text-align: center;
padding: 8px 2px;
text-decoration: none;
}
li a:hover {
background-color: #feae02;
}
li a.current:hover {
background-color: #F27930;
}
li a.divider:hover {
background-color: #F27930;
}
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<div class="sticky">
<ul>
<li><a class="current"><b>Jump to Article:</b></a></li>
<!-- <hr class="hrNav"> -->
<li><a class="link" href="#top">Top</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article1">Article 1</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article2">Article 2</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article3">Article 3</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article4">Article 4</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article5">Article 5</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article6">Article 6</a></li>
<li><a class="divider">|</a></li>
<li><a class="link" href="#article7">Article 7</a></li>
</ul>
</div>
I am doing horizontal navigation with css only.
I can’t position first submenu to the middle of it’s parent of the smaller width.
I'm not using display: because I need transitions. Also first submenu's min-width must be the same of it's parent.
Thanks in advance!
Here's a codepen link!
It needs to be like this http://i.stack.imgur.com/G4sUV.jpg
CSS
nav {
position:relative;
border: 1px solid #222;
background-image: linear-gradient(#444, #111);
border-radius: 6px;
box-shadow: 0 1px 1px #777, 0 1px 0 #666 inset;
height:40px;
margin:50px auto;
padding:0;
width:90%;
z-index:10;
}
nav ul,
nav ul ul {
list-style:none;
padding:0;
margin:0;
}
nav>ul {
clear:left;
display:flex;
justify-content:center;
align-content: stretch;
text-align:center;
font:12px Arial, Helvetica, sans-serif;
text-transform:uppercase;
}
nav ul li {
border-right: 1px solid #222;
box-shadow: 1px 0 0 #444;
height:40px;
padding:0;
margin:0;
position: relative;
text-align: center;
}
nav ul li:last-child {
border:none;
box-shadow:none
}
nav ul li a {
display:block;
text-align: center;
color: #ccc;
text-decoration: none;
padding:13px 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
nav ul li a:hover {
background-image: linear-gradient(#04acec, #0186ba);
color: #fafafa
}
nav ul li ul {
background: linear-gradient(#444, #111);
border-radius: 3px;
transition: all 1s ease-in-out;
text-transform:none;
opacity: 0;
visibility: hidden;
font-size: 12px;
}
nav ul li:hover>ul {
opacity: 1;
visibility: visible;
margin: 0;
width:auto;
}
nav ul li>ul {
position: absolute;
min-width: 100%;
}
nav ul li ul li ul {
min-width: 0px;
}
nav ul li ul li {
border:none;
box-shadow: 0 1px 0 #111, 0 2px 0 #666;
float:none;
margin:0;
}
nav ul li ul li:last-child {
box-shadow: none;
}
nav ul li ul li a {
padding:10px 8px;
border:none
}
nav ul li ul li ul {
left: 90%;
right: auto;
top: 10%;
margin: 0% 0 0 20%;
box-shadow: -1px 0 0 rgba(255,255,255,.3);
z-index:20;
}
nav ul li:last-child ul li ul {
left: auto;
right: 90%;
margin: 0 20% 0 0%;
}
HTML
<nav class ="topusermenu">
<ul class="first">
<li><a href=#>Home Veryyyyy LOOOOONNNNGGGGGGG Home Veryyyyy LOOOOONNNNGGGGGGG</a>
<ul>
<li><a href=#>Home 1Sub 1</a>
<ul>
<li><a href=#>Home 2Sub 1</a></li>
<li><a href=#>Home 2Sub 2</a></li>
<li><a href=#>Home 2Sub 3</a></li>
</ul>
</li>
<li><a href=#>Home 1Sub 2</a></li>
<li><a href=#>Home 1Sub 3</a>
<ul>
<li><a href=#>Home 2Sub 1</a></li>
</ul>
</li>
</ul>
</li>
<li><a href=#>News Very LONG</a>
<ul>
<li><a href=#>News 1Sub 1 Very very bvery Long</a></li>
<li><a href=#>News 1Sub 2</a>
<ul>
<li><a href=#>News 2Sub 1</a></li>
<li><a href=#>News 2Sub 2 long</a>
<ul>
<li><a href=#>1</a></li>
<li><a href=#>2</a>
<ul>
<li><a href=#>1</a></li>
<li><a href=#>22</a>
<ul>
<li><a href=#>1333</a></li>
<li><a href=#>2333</a></li>
<li><a href=#>3333</a></li>
</ul>
</li>
<li><a href=#>333</a></li>
</ul>
</li>
<li><a href=#>3</a></li>
</ul>
</li>
<li><a href=#>News 2Sub 3</a></li>
</ul>
</li>
<li><a href=#>News 1Sub 3</a></li>
</ul>
</li>
<li><a href=#>Contact</a>
<ul>
<li><a href=#>Contact1</a></li>
<li><a href=#>Contact2 long</a></li>
<li><a href=#>Contact3</a></li>
</ul>
</li>
<li><a href=#>About Lonoooooo o o o o</a>
<ul>
<li><a href=#>About1 Very Long</a></li>
<li><a href=#>About2 Long</a>
<ul>
<li><a href=#>subAbout2</a></li>
<li><a href=#>subAbout2</a>
<ul>
<li><a href=#>subAbout2</a></li>
<li><a href=#>subAbout2</a>
<ul>
<li><a href=#>subAbout2</a></li>
<li><a href=#>subAbout2</a>
<ul>
<li><a href=#>22</a></li>
<li><a href=#>22</a>
</li>
<li><a href=#>s22</a></li>
</ul>
</li>
<li><a href=#>subAbout2</a></li>
</ul>
</li>
<li><a href=#>subAbout2</a></li>
</ul>
</li>
<li><a href=#>2</a></li>
</ul>
</li>
<li><a href=#>About3</a></li>
</ul>
</li>
</ul>
</nav>
I'm using float: right for my horizontal menu (.drop_menu li) as I want the menu to be aligned to the right side of the screen (and logo to the left side). It works OK, the only issue is that my menu items are now in the wrong order (Link 3 then Link 3 then Link 1 instead of the opposite). Is there a way to fix that?
Many thanks
http://jsfiddle.net/eLSbq/
<div class="header">
<div class="logo">Logo</div>
<ul class="drop_menu">
<li><a href='#'>Link 1</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
</ul>
</li>
<li><a href='#'>Link 2</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
<li><a href='#'>Link 3</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
</ul>
</div>
.header {
width: 100%;
background: #fff;
color: #124191;
font-weight: 300;
font-size: 28px;
height: 120px;
display: table;
position: fixed;
z-index: 999999;
opacity: 0.7;
background: aqua;
}
.logo {
display: inline-block;
vertical-align: middle;
left:0;
color: #333;
font-size: 30px;
font-weight: 800;
letter-spacing: -1px;
margin-left: 60px;
background: red;
}
.drop_menu {
padding:0;
margin:0;
list-style-type:none;
right: 0;
display: table;
z-index: 3000;
display: table-cell;
vertical-align: middle;
right: 0;
}
.drop_menu li { display: table-cell;
vertical-align: middle; float: right;}
.drop_menu li a {
padding:9px 20px;
display:block;
color:#666;
text-decoration:none;
font-size: 15px;
font-weight: 400;
text-transform: uppercase;
}
/* Submenu */
.drop_menu ul {
position:absolute;
left:-9999px;
top:-9999px;
list-style-type:none;
}
.drop_menu li:hover { position:relative; background:#5FD367; }
.drop_menu li:hover ul {
left:0px;
top:30px;
background:#5FD367;
padding:0px;
}
.drop_menu li:hover ul li a {
padding:5px;
display:block;
width:168px;
text-indent:15px;
background-color:#5FD367;
}
.drop_menu li:hover ul li a:hover { background:#005555; }
Remove float:right from li which prevent the reverse order.
Add float:right to the ul's .dropdown class which put your entire menu at right side.
Add float:left to the li which helps your sub-menu to stay align.
.drop_menu {
float: right;
}
.drop_menu li {
display: table-cell;
vertical-align: middle;
float:left;
}
Js Fiddle Demo
flex solution:-
This is what worked for me:
ul {
display: flex;
justify-content: flex-end;
}
ul li {
outline: 2px violet solid;
display: list-item;
list-style: none;
padding: 10px;
margin: 0 5px;
}
ul li:hover {
outline: 2px deeppink solid;
cursor: pointer;
}
<div>
<h1>Hello World....</h1>
<ul>
<li>Foo</li>
<li>Bar</li>
<li>Baz</li>
</ul>
</div>
Here is the fiddle: https://jsfiddle.net/appsparkler/emjst7f3/4/
Good Luck...
Add one more div around menu items and set float to right
<div style='float:right'>
<!-- put menu controls here -->
</div>
Remove float right from following class
.drop_menu li { display: table-cell;
vertical-align: middle;}
for demo click on jsfiddle link
Try this...
.drop_menu li { display: table-cell;
vertical-align: middle; float: left;
}
It results in LINK 1 LINK 2 LINK 3 order and reduce the width of submenu links.
It works.
Float the <ul> to the right, then float its <li> elements to the left.
So i have a veritcal menu bar posited to the right of the page however I do not want the sub-menu to cross down onto the the main div (blue background).
I have created a JS fiddle link to show this clearer: http://jsfiddle.net/uzeZ6/
currently the menu bar will overflow onto the main div. How do I stop this from happening?
Thank You
HTML
<div id="top">
<div id="nav">
<ul>
<li><a href "#">Test </a>
<ul>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
</ul>
</ul>
</li>
<ul>
<li><a href "#">Test </a>
<ul>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
</ul>
</ul>
</li>
<ul>
<li><a href "#">Test </a>
<ul>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
</ul>
</ul>
</li>
<ul>
<li><a href "#">Test </a>
<ul>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
<li><a href "#">Link 1</a></li>
</ul>
</ul>
</li>
</div>
</div>
<div id ="main">text goes here
</div>
CSS
#charset"utf-8";
#top {
background-color: #CCC;
padding: 0px;
height: 150px;
width: 640px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;}
#top #nav {
margin: 0px;
padding: 0px;
}
#top #nav ul {
margin: 0px;
padding: 0px;
}
#top #nav ul li {
background-color: #666;
float: left;
position: relative;
border: 1px solid #000;
list-style-type: none;
}
#top #nav ul li:hover ul {
visibility: visible;
left: -152px;
top: -1px;
}
#top #nav ul li a {
font-size: 14px;
color: #FFF;
line-height: 30px;
text-decoration: none;
display: block;
height: 30px;
width: 150px;
}
#top #nav ul li ul {
position: absolute;
visibility: hidden;
}
#nav {
background-color: #333;
padding: 0px;
float: right;
height: 150px;
width: 180px;
}
#main {
height: 300px;
width: 640px;
background-color: #00F;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
Here's the JS fiddle: http://jsfiddle.net/yE8ae/
You can adjust the position as you wish by changing the bottom value on these lines:
#top #nav ul:nth-child(3) li:hover ul {
top: initial; bottom: -33px;
}
#top #nav ul:nth-child(4) li:hover ul {
top: initial; bottom: -1px;
}
HTML
<div id="top">
<div id="nav">
<ul>
<li><a href "#">Test 1</a>
<ul>
<li><a href "#">Test 1, Link 1</a>
</li>
<li><a href "#">Test 1, Link 2</a>
</li>
<li><a href "#">Test 1, Link 3</a>
</li>
</ul>
</ul>
</li>
<ul>
<li><a href "#">Test 2</a>
<ul>
<li><a href "#">Test 2, Link 1</a>
</li>
<li><a href "#">Test 2, Link 2</a>
</li>
<li><a href "#">Test 2, Link 3</a>
</li>
</ul>
</ul>
</li>
<ul>
<li><a href "#">Test 3</a>
<ul>
<li><a href "#">Test 3, Link 1</a>
</li>
<li><a href "#">Test 3, Link 2</a>
</li>
<li><a href "#">Test 3, Link 3</a>
</li>
</ul>
</ul>
</li>
<ul>
<li><a href "#">Test 4</a>
<ul>
<li><a href "#">Test 4, Link 1</a>
</li>
<li><a href "#">Test 4, Link 2</a>
</li>
<li><a href "#">Test 4, Link 3</a>
</li>
</ul>
</ul>
</li>
</div>
<p> </p>
</div>
<div id="main">text goes here</div>
CSS
#charset"utf-8";
#top {
background-color: #CCC;
padding: 0px;
height: 150px;
width: 640px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
#top #nav {
margin: 0px;
padding: 0px;
}
#top #nav ul {
margin: 0px;
padding: 0px;
float: left;
}
#top #nav ul li {
background-color: #666;
float: left;
position: relative;
border: 1px solid #000;
list-style-type: none;
cursor: pointer;
}
#top #nav ul li:hover ul {
display: block;
}
#top #nav ul:nth-child(3) li:hover ul {
top: initial; bottom: -33px;
}
#top #nav ul:nth-child(4) li:hover ul {
top: initial; bottom: -1px;
}
#top #nav ul li a {
font-size: 14px;
color: #FFF;
line-height: 30px;
text-decoration: none;
display: block;
height: 30px;
width: 150px;
}
#top #nav ul li ul {
position: absolute;
display: none;
right: 151px;
top: -1px;
}
#nav {
background-color: #333;
padding: 0px;
float: right;
height: 150px;
width: 180px;
}
#main {
height: 300px;
width: 640px;
background-color: #00F;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}
I have a basic CSS dropdown menu that looks like this: http://jsfiddle.net/qfTt3/ (same code below)
HTML
<ul id="main-navigation">
<li class="active"><a href='#'>Plans</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
</ul>
</li>
<li><a href='#'>How it Works</a></li>
<li><a href='#'>About</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
<li><a href='#'>Testimonials</a>
<ul>
<li><a href='#'>Sub Link 1</a></li>
<li><a href='#'>Sub Link 2</a></li>
<li><a href='#'>Sub Link 3</a></li>
<li><a href='#'>Sub Link 4</a></li>
</ul>
</li>
<li><a href='#'>FAQ</a></li>
<li><a href='#'>Contact</a></li>
</ul>
CSS
#main-navigation {
background: #FFF;
padding: 0;
margin: 0;
list-style-type: none;
height: 100px;
float: right;
font-size: 18px;
}
#main-navigation li {
float: left;
}
#main-navigation>li {
line-height: 100px;
background-color: #FFF;
margin-left: 10px;
}
#main-navigation>li>ul>li {
line-height: 30px;
background: #FFF;
margin-left: 0px;
border-bottom: 1px solid #999;
position: relative;
z-index: 100;
}
#main-navigation li a {
padding: 0px 12px;
display: block;
text-decoration: none;
color: #6d6e71;
}
#main-navigation ul {
position: absolute;
left: -9999px;
top: -9999px;
list-style-type: none;
margin: 0;
}
#main-navigation li:hover {
position: relative;
background: #10b794;
}
#main-navigation li a:hover {
color: #FFF;
}
#main-navigation li:hover ul {
left: 0px;
top: 100px;
background: #10b794;
padding: 0px;
}
#main-navigation li:hover ul li a {
padding: 5px;
display: block;
width: 168px;
text-indent: 15px;
background: #10b794;
}
#main-navigation li:hover ul li a:hover {
color: #FFF;
}
#main-navigation li.active {
border-bottom: 4px solid #10b794;
height: 96px;
}
As you can see, the text color changes to white when and individual item is hovered over. What I would like to do is have the text color of both the main <li> as well as the submenu items change to white if any part of that menu/submenu is hovered over. If someone hovers over 'Plans' in the menu, all the submenu links should have white text as well. If this possible with CSS selectors alone or do I need to look into a JS solution?
You want to change:
#main-navigation li a:hover {
color: #FFF;
}
to be:
#main-navigation li:hover > a {
color: #FFF;
}
JSFiddle here.
Basically, you want the a element's color to change when you are hovered over the list item. That way, when you hover over other submenu items, you're still hovering over the li containing the submenu.
I use the child selector > so that the submenu item links are not affected when you're hovering over the main menu item link.
To target the Plans submenu link colors, you should apply the styling to a class to specifically target them. Since you already have a class specifically on Plans (.active), I'll just use that for demonstration purposes.
CSS:
#main-navigation li:hover > a, #main-navigation .active:hover a {
color: #FFF;
}
JSFiddle here.
I get rid of the child selector when targeting .active so that it makes all child a elements white when hovering over the main link.
You must add this to your css
#main-navigation > li:hover > ul > li > a {
color: #FFF;
}
http://jsfiddle.net/sijav/qfTt3/1/