Reputation: 71
I have created my nav bar which was working fine but now i tried to add sub menu in my navbar and its not showing sub menu on hover. kindly check and correct me.
First I added <ul>
in my <li>
tag then I added css to hide nested <ul>
then I tried to show <ul>
on hover <li>
*{
margin: 0;
padding: 0;
}
nav{
background-color: red;
}
ul{
background-color: purple;
width: 50%;
}
nav ul li {
list-style: none;
padding: 5px;
display: inline-block;
}
nav ul li a{
text-decoration: none;
color:black;
font: bold 12px Arial;
}
nav ul li:hover{
background-color: blue;
color: red;
}
nav ul li:hover a{
color: red;
}
ul li ul {
display: none;
position:absolute;
}
nav ul li:hover ul {
display:block;
}
<nav>
<ul>
<li> <a href="">Home</a></li>
<li> <a href="">About Us</a></li>
<li> <a href="">Contact Us</a></li>
<li> <a href="">Privacy Policy</a></li>
<li>
<ul>
<li><a href="">Submenu 1</a></li>
<li><a href="">Submenu 2</a></li>
<li><a href="">Submenu 3</a></li>
<li><a href="">Submenu 4</a></li>
</ul>
</li>
</ul>
</nav>
Upvotes: 0
Views: 1418
Reputation: 7066
Looks like your <li>
wrap is incorrect!
here is the fiddle
After Privacy Policy
you have created another <li>
that shouldn't be needed. you have to wrap the sub-menus with in privacy policy
tag not a new one that is one of the reason why the css was not showing data as expected and you were almost there regarding CSS
I just fixed it for you! hope it helps.
* {
margin: 0;
padding: 0;
}
nav {
height: 30px;
}
nav ul {
display: block;
position: relative;
z-index: 100;
}
nav ul li {
float: left;
list-style: none;
margin: 0;
padding: 0;
}
nav ul li ul {
display: none;
}
nav ul li a {
width: 100px;
height: 30px;
display: block;
text-decoration: none;
text-align: center;
line-height: 30px;
background-color: black;
color: white;
}
nav ul li a:hover {
background-color: red;
}
nav ul li:hover ul {
position: absolute;
top: 30px;
display: block;
width: 100px;
}
nav ul li:hover ul li {
display: block;
}
<nav>
<ul>
<li> <a href="#">Home</a></li>
<li> <a href="#">About Us</a></li>
<li> <a href="#">Contact Us</a></li>
<li> <a href="#">Privacy Policy</a>
<ul>
<li><a href="#">Submenu 1</a></li>
<li><a href="#">Submenu 2</a></li>
<li><a href="#">Submenu 3</a></li>
</ul>
</li>
</ul>
</nav>
Upvotes: 2
Reputation: 29
You have to add an anchor tag in the sub nav you have created. Because currently those sub-tabs are created but are not associated with any super-tab.
<a href="">Subnav</a>
So add this above your sub-nav code. You are good to go.
Upvotes: 0
Reputation: 86
*{
margin: 0;
padding: 0;
}
nav{
background-color: red;
}
ul{
background-color: purple;
width: 50%;
}
nav ul li {
list-style: none;
padding: 5px;
display: inline-block;
}
nav ul li a{
text-decoration: none;
color:black;
font: bold 12px Arial;
}
nav ul li:hover{
background-color: blue;
color: red;
}
nav ul li:hover a{
color: red;
}
ul li ul {
display: none;
position:absolute;
}
nav ul li:hover ul {
display:block;
}
<nav>
<ul>
<li> <a href="">Home</a></li>
<li> <a href="">About Us</a></li>
<li> <a href="">Contact Us</a></li>
<li> <a href="">Privacy Policy</a></li>
<li>
<a href="">test</a>
<ul>
<li><a href="">Submenu 1</a></li>
<li><a href="">Submenu 2</a></li>
<li><a href="">Submenu 3</a></li>
<li><a href="">Submenu 4</a></li>
</ul>
</li>
</ul>
Upvotes: 0
Reputation: 121
Why don't you start from this working snippet and try to change data according to your needs :)
Upvotes: 0