Multiple layer Dropdown menu using CSS - html

I am able to get my dropdown menu to work perfectly, only issue is that i'm having trouble trying to figure out how I can add another dropdown level.
For instance, I want another level to drop down when I hover over Test3. What am I missing in the code to do so?
CSS:
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a: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: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
HTML:
<nav>
<ul id="menu">
<li class="dropdown">
Location
<div class="dropdown-content">
Test1
Test2
Test3
</div>
</ul>
</nav>

Maybe you didn't close the li tag? Placing a div inside the li seems to make things complicated.
I think it's easier if you make a new level just wrap everything in a ul-li-ul loop. Like this:
<ul id="menu">
<li>SOMETHING LV1</li>
<li>SOMETHING LV1</li>
<li>DROPDOWN 1
<ul>
<li>SOMETHING LV2</i></li>
<li>DROPDOWN 2</i>
<ul>
<li>SOMETHING LV3</li>
</ul>
</li>
</ul>
</li>
</ul>
Then hide it and show when hover:
#menu li ul{
display: none;
}
#menu li:hover>ul{
display: block;
position: absolute;
width: 100%;
}
So you can have two or three levels, no problem.
Please see the fiddle example: https://jsfiddle.net/fp1x1v05/
Hope this helps.

Sorry, posted an answer before that didn't isolate the links.
What you want to do, ideally, is target hover events on elements that wrap links, then use a bit more specificity in your selectors.
Try this one:
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a: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: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropdown-content ul li {
display: none;
}
.dropdown:hover .dropdown-content li:hover ul li {
display: block;
}
</style>
<nav>
<ul id="menu">
<li class="dropdown">
Location
<ul class="dropdown-content"><li>Test1<ul><li>Subtest 1</li></ul></li>
<li>Test2</li>
<li>Test3</li></ul>
</ul>
</nav>

nav ul
{
list-style: none;
padding: 0;
text-align:center;
}
nav li
{
background-color: rgba(0,100,0,0.5);
position: relative;
display: inline-block;
}
nav li:hover
{
background-color: rgba(100,0,0,0.5)
}
nav a
{
display:block;
padding: 0.5em;
text-decoration: none;
color: rgba(0,0,100,0.9);
}
nav ul ul
{
display: none;
position: absolute;
}
nav li:hover > ul
{
display: block;
}
nav ul ul ul
{
left: 100%;
top: 0;
}
nav > ul > li > ul > li
{
min-width: 100%;
}
<nav>
<ul>
<li>
1.First
<li>
2.Second
<ul>
<li> 2.1
<li> 2.2
<ul>
<li> 2.2.1
<li> 2.2.2
<ul>
<li> 2.2.2.1
<li> 2.2.2.2
</ul>
</ul>
</ul>
<li> 3.Third
</ul>
</nav>

Related

exemple on w3school. navbar in Css does not display list item

Its is example on w3school.
I don't understand why if I add position:relative on ancestor class of dropDown-content.
Navbar does not display dropdown item and an element with position: absolute; is positioned relative to the nearest positioned ancestor, but without position:relative, is till not positioned body tag as ancestor?
Is there any way to solve this?
<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
position:relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</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>
As per this post, the child element of a relative parent should have position: fixed
Add
.dropdown-content {
position: fixed;
}
That will work for you.
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
li.dropdown {
display: inline-block;
position:relative;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
position: fixed;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1;}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

How to Center a Nav bar

I'm new to programming. I'm having trouble centering a <nav> bar a made. I followed this specific one.
How would I be able to center entire nav bar?
Click Here
set text-align:center to ul and remove float:none from li and add display:inline-block
check with demo
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
text-align:center;
}
li {
float: none;
display:inline-block;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a: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: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

Dropdown Menu Not Working?

I've searched through this code for errors that would keep it from working, but can't seem to find any. All appears well, except the menu will not dropdown. Does anyone have any tips?
Here is my code as snippet:
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #5BC8C3;
position: fixed;
top: 60px;
width: 100%;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: #f7f7f7;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: #F7f7f7;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #284A64;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
color: #f7f7f7;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f7f7f7
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li><a class="active" href="#index">Home</a>
</li>
<li>Catalog
</li>
<li>Calendar
</li>
<li class="dropdown">
Library Information
<div class="dropdown-content">
Children's Programs
Adult Programs
Help Services
Other Services
Library Policies
</div>
</li>
<li>Contact
</li>
</ul>
Thank you for any advice!
Well, the dropdown-menu actually shows up :)
But we cannot see it, because your menu is
overflow: hidden;
If you delete this css-row, your menu will work:
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #5BC8C3;
position: fixed;
top: 60px;
width: 100%;
}
li {
float: left;
}
li a,
.dropbtn {
display: inline-block;
color: #f7f7f7;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover,
.dropdown:hover .dropbtn {
background-color: #F7f7f7;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #284A64;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
color: #f7f7f7;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f7f7f7
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li><a class="active" href="#index">Home</a>
</li>
<li>Catalog
</li>
<li>Calendar
</li>
<li class="dropdown">
Library Information
<div class="dropdown-content">
Children's Programs
Adult Programs
Help Services
Other Services
Library Policies
</div>
</li>
<li>Contact
</li>
</ul>
ul{
margin:0px;
padding:0px;
list-style:none;
}
ul li{
background-color:#5BC8C3;
width:150px;
height:50px;
float:left;
line-height:50px;
color:#0FC;
text-align:center;
}
ul li a{
color: #FFF;
text-decoration:none;
display:block;
}
ul li a:hover{
background-color:#F7f7f7;
}
<ul>
<li>Home</li>
<li>Catalog</li>
<li>Calender</li>
<li>Library Information
<ul>
<li>Children's Programs</li>
<li>Adult Programs</li>
<li>Help Services</li>
</ul>
</li>
<li>Contact</li>
</ul>

dropdown box removing styles

I am having a bit of trouble with the drop down box. I want my <a> elements to be without underline and in the middle of the dropdown-content. You can see my code, I want to remove the underlines from the dropdown-content and I want it to be red when it is hovered. However whenever I try to do that, it gives a false defect.
Here is the code. Test it to see the problem logically!
<!DOCTYPE html>
<html>
<head>
<style>
/* Building navigation bar */
ul {
text-align: center;
list-style-type: none;
margin: 0;
padding-bottom: 5px;
background-color: black;
display: block;
}
li a {
text-decoration: none;
padding: 14px 20px;
color: white;
}
li a:hover {
background-color: red;
}
li {
display: inline;
}
/* Building about dropdown and the dropdown content */
.container {
width: 15em;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
text-align: center;
position: absolute;
display: none;
background-color: black;
color: white;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<ul>
<li>Home</li>
<li>News</li>
<div class="dropdown">
<li>About</li>
<div class="dropdown-content"><ul>
<a style="text-decoration = none;" href="#theMan.html">Hey</a>
Hey
</ul>
</div>
</div>
</ul>
</body>
</html>
You have a fair amount of invalid HTML in there. Divs cannot be children of ul. You might want to validate your HTML first.
Therefore with a little re-structuring of the HTML to make it valid.
ul {
text-align: center;
list-style-type: none;
margin: 0;
padding: 0;
padding-bottom: 5px;
background-color: black;
display: block;
}
li a {
text-decoration: none;
display: block;
padding: 10px;
color: white;
}
li a:hover {
background-color: red;
}
li {
display: inline-block;
}
/* Building about dropdown and the dropdown content */
.container {
//width: 15em;
}
.dropdown {
position: relative;
}
.dropdown-content {
text-align: center;
position: absolute;
top: 100%;
left: 0;
display: none;
background-color: black;
color: white;
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li>Home
</li>
<li>News
</li>
<li class="dropdown">About
<ul class="dropdown-content">
<li><a style="text-decoration = none;" href="#theMan.html">Hey</a>
</li>
<li>Hey
</li>
</ul>
</li>
</ul>
Check My Fiddle
ul {
text-align: center;
list-style-type: none;
margin: 0;
padding-bottom: 5px;
background-color: black;
display: block;
}
li a {
text-decoration: none;
padding: 14px 20px;
color: white;
}
li a:hover {
background-color: red;
}
li {
display: inline;
}
/* Building about dropdown and the dropdown content */
.container {
width: 15em;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
text-align: center;
position: absolute;
display: none;
background-color: black;
color: white;
}
/*added lines*/
.dropdown-content a{
text-decoration:none;
}
.dropdown-content a:hover{
color:red;/*font-color will be red*/
}
/*added lines ends here*/
.dropdown:hover .dropdown-content {
display: block;
}
<body>
<ul>
<li>Home</li>
<li>News</li>
<div class="dropdown">
<li>About</li>
<div class="dropdown-content"><ul>
Hey
Hey
</ul>
</div>
</div>
</ul>
</body>
1.) You have to put your submenu entries into li tags (they are in a ul tag!)
2.) The .dropdown-content selector/rule has to be changed to .dropdown-content ul
3.) quite some other stuff, but that wasn't part of the question.
ul {
text-align: center;
list-style-type: none;
margin: 0;
padding-bottom: 5px;
background-color: black;
display: block;
}
li a {
text-decoration: none;
padding: 14px 20px;
color: white;
}
li a:hover {
background-color: red;
}
li {
display: inline;
}
/* Building about dropdown and the dropdown content */
.container {
width: 15em;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content ul {
text-align: center;
position: absolute;
display: none;
background-color: black;
color: white;
padding: 0;
}
.dropdown:hover .dropdown-content ul {
display: block;
}
<ul>
<li>Home
</li>
<li>News
</li>
<div class="dropdown">
<li>About
</li>
<div class="dropdown-content">
<ul>
<li><a style="text-decoration = none;" href="#theMan.html">Hey</a>
</li>
<li>Hey
</li>
</ul>
</div>
</div>
</ul>

Css div center alignment not working

I have tried to make horizontal menu in center of page but 'div align=center' not working properly.
.nav ul {
display: block;
list-style-type: none;
margin: 0;
position: absolute;
}
.nav li {
display: inline-block;
float: left;
}
.nav li a {
display: block;
min-width: 140px;
height: 50px;
text-align: center;
line-height: 50px;
font-family: "Helvetica
Neue", Helvetica, Arial, sans-serif; color: #fff; background: #2f3036; text-decoration: none; } .nav li:hover ul a { background: #f3f3f3; color: #2f3036; height: 40px; line-height: 40px; } .nav div ul {display:block;} .nav li ul { display: none; } .nav
li ul li {
display: block;
float: none;
}
.nav li ul li a {
width: auto;
min-width: 100px;
padding: 0 20px;
}
.nav ul > li:hover ul {
display: block;
visibility: visible;
}
.nav ul li:hover > ul {
display: block;
}
<div class="nav">
<ul>
<li>Home
</li>
<li>
About
<ul>
<li>A1
</li>
<li>A2
</li>
</ul>
</li>
</ul>
</div>
sample code
I have tried to make horizontal menu in center of page but 'div align=center' not working properly.
Remove position:absolute; from your ul and add these minor changes
.nav{
width: 100%;
}
.nav ul {
display:block;
list-style-type:none;
margin: 0px auto;
width: 50%;
}
Here's the solved fiddle
Remove position:absolute;. It doesn't take any margin or padding values.
.nav ul {
list-style-type:none;
margin:0 auto;
display:table;
}
Here solved fiddle link
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #19c589;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li class="dropdown">
About
<div class="dropdown-content">
A1
A2
A3
</div>
</li>
<li>New</li>
</ul>
Use as the below:
.nav {
margin:0 auto;
}
please remove the ul position: absolute; style other styling left as it is. These are the additional changes you have to do.
.nav {
display: -webkit-box;
}
.nav ul {
margin: 0 auto;
}