Reputation: 85
In the following code, I am trying to practice a drop down menu.I want the "Third" to appear below the "Second" whenever I hover on the "Second". For that, I have used the famous technique of ul li:hover > ul {display: block;}
but it does not seems to be working. The "Third" is permanently hidden and does not pops up on hovering. Could someone please advice as to where I am wrong?
code:
body {
height: 100%;
width: 100%;
}
ul {
background-color: rgba(0, 0, 0, 1);
color: rgba(255, 0, 0, 1);
/* [disabled]display: inline-block; */
text-align: center;
height: auto;
position: relative;
width: 50%;
margin-right: auto;
margin-left: auto;
}
ul li {
display: inline-block;
border: 1px solid #000;
list-style-type: none;
text-align: center;
margin-right: 10px;
margin-left: 10px;
}
ul ul {
display: none;
}
ul li:hover > ul {
display: block;
}
<ul>
<li>First</li>
<li>Second</li>
<ul>
<li>Third</li>
</ul>
</ul>
Upvotes: 1
Views: 73
Reputation: 8795
ul{
width:294px;
height:50px;
padding-left:0;
margin:0;
background:#CCCCCC;
}
ul > li{
list-style:none;
display:inline;
padding:13px 19px;
font-size:24px;
float:left;
}
ul > li > ul{
margin:10px -10px;
padding-left:0;
width:auto;
height:55px;
visibility: hidden;
}
ul > li > ul > li{
display:block;
list-style-type:none;
}
ul > li:hover ul{
visibility:visible;
}
<ul>
<li>First</li>
<li>
Second
<ul>
<li>Third</li>
</ul>
</li>
<li>Fourth</li>
</ul>
Upvotes: 0
Reputation: 39322
You 2nd ul
should be inside li
to make it work.
body {
height: 100%;
width: 100%;
}
ul {
background-color: rgba(0,0,0,1);
color: rgba(255,0,0,1);
list-style: none;
/* [disabled]display: inline-block; */
text-align: center;
height: auto;
position: relative;
width: 50%;
margin-right: auto;
margin-left: auto;
}
.nav > li {
display: inline-block;
border: 1px solid #000;
list-style-type: none;
position: relative;
text-align: center;
margin-right: 10px;
margin-left: 10px;
}
.nav ul {
position: absolute;
text-align: left;
padding: 0 10px;
display: none;
width: 150px;
top: 100%;
left: 0;
}
ul li:hover > ul {display: block;}
<ul class="nav">
<li> First </li>
<li> Second
<ul>
<li> Third </li>
</ul>
</li>
</ul>
Upvotes: 0
Reputation: 2825
Your drop down
menu should be child
of li
so your html must be like this
body {
height: 100%;
width: 100%;
}
ul {
background-color: rgba(0,0,0,1);
color: rgba(255,0,0,1);
/* [disabled]display: inline-block; */
text-align: center;
height: auto;
position: relative;
width: 50%;
margin-right: auto;
margin-left: auto;
}
ul li {
display: inline-block;
border: 1px solid #000;
list-style-type: none;
text-align: center;
margin-right: 10px;
margin-left: 10px;
}
ul ul {
display: none;
}
ul li:hover > ul {display: block;}
<ul>
<li> First </li>
<li>
Second
<ul>
<li> Third </li>
</ul>
</li>
</ul>
Upvotes: 2
Reputation: 43481
You have invalid HTML because ul > ul
is not valid. Move you inner ul
to li
of Second
:
body {
height: 100%;
width: 100%;
}
ul {
background-color: rgba(0, 0, 0, 1);
color: rgba(255, 0, 0, 1);
/* [disabled]display: inline-block; */
text-align: center;
height: auto;
position: relative;
width: 50%;
margin-right: auto;
margin-left: auto;
}
ul li {
display: inline-block;
border: 1px solid #000;
list-style-type: none;
text-align: center;
margin-right: 10px;
margin-left: 10px;
}
ul ul {
display: none;
}
ul li:hover > ul {
display: block;
}
<ul>
<li>First</li>
<li>Second
<ul>
<li>Third</li>
</ul>
</li>
</ul>
Upvotes: 2