How To Display Submenu On Dropdown Using Positioning And Css?
I have this simple navigation menu for my design. But the problem is I can't display the sub menu when hovered. Is there any way to solve this? Or does my CSS has some missing styl
Solution 1:
There are quite a few errors in your code. The most critical one is the top: 100%
on the submenu container, which moves it out of the visible area.
Apart from that it's important to define the submenu header li
as position: relative
and the submenu ul
itself by default display: none
and on hover display: block
. And put the submenu container into the li
that serves as its header. See my code below:
navul {
list-style-type: none;
background: #000;
}
navli {
display: inline-block;
position: relative;
}
navli>a {
padding: 15px15px;
display: inline-block;
text-decoration: none;
color: white;
text-align: center;
}
navli>a:hover {
background: violet;
}
navulliul {
display: none;
position: absolute;
top: 45px;
left: 0px;
width: 100px;
overflow: visible;
padding: 0;
}
navulli:hoverul {
display: block;
}
navululli {
background: green;
display: block;
}
navululli>a:hover {
color: red;
}
<!DOCTYPE html><html><head><metacharset="utf-8"><title>sample UL</title><stylemedia="screen"></style></head><body><nav><ul><li><ahref="#">Home</a></li><li><ahref="#">Menu 1</a><ul><li><ahref="#">submenu 1</a></li><li><ahref="#">submenu 2</a></li></ul></li></ul></nav></body></html>
Solution 2:
navul {
list-style-type: none;
overflow: hidden;
background: #000;
position: relative;
}
navli {
float: left;
}
navli > a {
padding: 15px15px;
display: inline-block;
text-decoration: none;
color: white;
text-align: center;
}
navli > a:hover {
background: violet;
}
navulul {
background: green;
top: 100%;
}
navullia + ul {
display: none;
}
navullia:hover + ul {
display: block;
}
.sub-menu:hover {
display: block;
}
<!DOCTYPE html><html><head><metacharset="utf-8"><title>sample UL</title></head><body><nav><ul><li><ahref="#">Home</a></li><li><ahref="#">Menu 1</a><ulclass="sub-menu"><li><ahref="#">submenu 1</a></li><li><ahref="#">submenu 2</a></li></ul></li></ul></nav></body></html>
Solution 3:
Just try this one. You could learn from it.
<!DOCTYPE html><html><head><style>ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
lia, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px16px;
text-decoration: none;
}
lia:hover, .dropdown:hover.dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px8px16px0pxrgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-contenta {
color: black;
padding: 12px16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-contenta:hover {background-color: #f1f1f1}
.dropdown:hover.dropdown-content {
display: block;
}
</style></head><body><ul><li><ahref="#home">Home</a></li><li><ahref="#news">News</a></li><liclass="dropdown"><ahref="javascript:void(0)"class="dropbtn">Dropdown</a><divclass="dropdown-content"><ahref="#">Link 1</a><ahref="#">Link 2</a><ahref="#">Link 3</a></div></li></ul><h3>Dropdown Menu inside a Navigation Bar</h3><p>Hover over the "Dropdown" link to see the dropdown menu.</p></body></html>
Solution 4:
<!DOCTYPE html><html><head><metacharset="utf-8"><title>sample UL</title><stylemedia="screen"></style></head><body><navclass="cf"><ul><li><ahref="#">Home</a></li><li><ahref="#">Menu 1</a><ulclass="submenu"><li><ahref="#">submenu 1</a></li><li><ahref="#">submenu 2</a></li></ul></li></ul></nav></body></html>
Please refer Clearfix
Also given class name to nav as cf and inner ul as submenu
.cf:before, .cf:after {
content:"";
display:table;
}
.cf:after {
clear:both;
}
.cf {
zoom:1;
}
nav {
background: #333A31;
height: 2.3em;
}
ul, li {
margin: 0;
padding: 0;
list-style: none;
float: left;
}
ul {
background: #D5333C;
height: 2em;
width: 100%;
}
li {
position: relative;
}
lia {
display: block;
line-height: 2em;
padding: 01em;
color: white;
text-decoration: none;
}
lia:hover {
background: #9155311;
height: 2em;
padding-top: .3em;
position: relative;
top: -.3em;
border-radius: .3em .3em00;
}
.current, a:hover.current {
background: #AD9B7F;
color: #eee;
padding-top: .3em;
border-radius: .3em .3em00;
position: relative;
top: -.3em;
border-bottom: .3em solid #917F63;
cursor: default;
}
/*dropdown menu styles*/ul.submenu {
float: none;
background: #916A31;
width: auto;
height: auto;
position: absolute;
top: 2em;
left: -9000em;
}
ul.submenuli {
float: none;
}
navli:hoverul {
left: 0;
}
ul.submenulia {
border-bottom: 1px solid white;
padding: .2em1em;
white-space: nowrap;
}
ul.submenuli:last-childa {
border-bottom: none;
}
ul.submenulia:hover {
background: #D5973C;
height: 2em;
padding-top: .2em;
top: 0;
border-radius: 0;
}
Hope this helps.
Post a Comment for "How To Display Submenu On Dropdown Using Positioning And Css?"