Mr_Green
Mr_Green

Reputation: 41822

Create second level dropdown using css

I am trying to create a second level dropdown. I successfully created first level dropdown but bit stuck in making it level 2. Please assist me to complete it..

and also please explain me what mistake I am doing that I cant get the second level dropdown even the css part is good (I think so)

EDIT: I know there are many tutorials on dropdown css. But I want to know why this is not working.

Here is the link to jsbin

HTML

<ul id="nav">
  <li>Home</li>
  <li>Details
    <ul id="subNav">
    <li>x details<li>
      <li>y details</li>
    </ul></li>

  <li>About Us
    <ul id="xSubNav">
      <li>About company
        <ul>
          <li>full information</li>
          <li>summary</li>
        </ul></li>
      <li>About Author</li>
    </ul></li>
</ul>

CSS

*{font-family:consolas;}

li{line-height:20px;}

ul#nav>li{float:left;width:100px;list-style:none;
          cursor:hand;cursor:pointer;}

ul#nav li li
{display:none;width:150px;}

ul#nav li ul
{padding:0;margin:0;}

ul#nav>li:hover>ul>li
{display:block;}

ul#nav>li:hover{color:grey;}

ul li li{color:black;}

ul li li:hover
{color:cornflowerblue;}

ul li li:hover li   /* level 2 dropdown part */
{display:block;margin-left:150px;width:300px;}

Upvotes: 0

Views: 495

Answers (2)

Monkviper
Monkviper

Reputation: 1507

Here is solution with your code

Just add the below css:

ul ul li { position:relative;}
ul ul li ul { position:absolute; display:none; left:0px; top:0px;}
ul ul li:hover ul { display:block;}
ul#nav li li li {display:block;}

Check this working fiddle

Upvotes: 2

Adrian Ber
Adrian Ber

Reputation: 21360

The problem is the specificity of CSS rules. Just add #nav to the last three rules, to not get overridden by the first ones.

ul#nav li li{color:black;}

ul#nav li li:hover
{color:cornflowerblue;}

ul#nav li li:hover li
{display:block;margin-left:150px;width:300px;}

And I think some other tuning is needed, but that's the idea.

Upvotes: 1

Related Questions