CSS Drop Down Menu Not working

2020-02-15 04:08发布

i'm learning the basic of CSS and trying to create a dropdown menu, i tried creating a dropdown menu using plain CSS, but it's not working.

So far I tried this code:

CSS

<!-- because of the * default code it takes out all margin and padding or idententation -->
*{
    margin: 0px;
    padding: 0px;}

body
{
    font-family: verdana;
    background-color: ABC;
    padding: 50px;
}

h1
{
    text-align: center;
    border-bottom: 2px solid #009;
    margin-bottom: 50px;
}

/*rules for navigation menu */
/*============================================*/
ul#navmenu, ul.sub1
{
    list-style-type: none;<!-- sets bullets to none -->

}

ul#navmenu li
{
    outline: 1px solid red;
    width: 125px;
    text- align: center;
    position: relative;
    float: left;
    margin-right: 4px;
}

ul#navmenu a
{
    text-decoration: none;
    display: block; <!-- this code makes the link a button instead  pointing specifically on the link -->
    width: 125px;
    height: 25px;
    line-height: 25px;
    background-color: #FFF;
    border: 1px solid #CCC;
    border-radius: 5px;
}

ul#navmenu .sub1 li
{

}

ul#navmenu .sub1 a 
{
    margin-top: 0px;
}

ul#navmenu li:hover > a
{
 background-color: #CFC; <!-- sets all link color when hovering to yellow  -->
}

ul#navmenu li:hover a: hover
{
    background-color: #FF0;  <!-- sets all link color when hovering to yellow  -->
}

ul#navmenu ul.sub1
{
    display: none;
    position: absolute;
    top: 26px;
    left: 0px;

}

ul#navmenu li:hover .sub1
{
    display: block;
}

HTML

<h1>Navigation Menu</h1>

<ul id="navmenu">
<li><a href="#">Hyperlink 1</a></li>
<li><a href="#">Hyperlink 2</a></li>
    <ul id="sub1"> 
        <li><a href="#">Hyperlink 2.1</a></li>
        <li><a href="#">Hyperlink 2.2</a></li>
    </ul>
<li><a href="#">Hyperlink 3</a></li>
<li><a href="#">Hyperlink 4</a></li>
</ul>

</body>
</html>

The dropdown menu is not working, it's not hiding the sub menus, i don't know why.

Here is the picture screenshot using Internet Explorer:

IE

While using Google Chrome:

Chrome

I can't move on:( Any suggestion why dropdown menu is not working and why it's showing differently using other browsers? Is there a way on how to code CSS dropdown menu where it will show the same on any browser? Thanks in advance.

标签: css
4条回答
何必那么认真
2楼-- · 2020-02-15 04:34

You need to do three things..

1.Correction in HTML, Where there is 'li' tag with child 'ul'with id="sub1".You need to write it as

<li><a href="#">Hyperlink 2</a>
    <ul id="sub1"> 
        <li><a href="#">Hyperlink 2.1</a></li>
        <li><a href="#">Hyperlink 2.2</a></li>
    </ul>
</li>

// there was a typo mistake.You close 'li' before the 'ul'. It should be closed in the end.

  1. You need to display:none for sub1 menu.

    li ul{ display:none; }

  2. Then show it when you hover that 'li'

    li:hover ul{ display:block; }

EDITED :Write this in style tag....

*{
            margin: 0px;
            padding: 0px;}

        body
        {
            font-family: verdana;
            background-color: ABC;
            padding: 50px;
        }

        h1
        {
            text-align: center;
            border-bottom: 2px solid #009;
            margin-bottom: 50px;
        }

        /*rules for navigation menu */
        /*============================================*/
        ul
        {
            list-style-type: none;<!-- sets bullets to none -->

        }

        ul#navmenu li
        {
            width: 125px;
            text- align: center;
            float: left;
            margin-right: 4px;
            background-color: #FFF;
            border: 1px solid #CCC;
            border-radius: 5px;
        }

        ul#navmenu a
        {
            text-decoration: none;
            display: block; <!-- this code makes the link a button instead  pointing specifically on the link -->
            width: 125px;                
            line-height: 25px;

        }
    ul#navmenu li:hover > a
        {
         background-color: #CFC; 
        }
    li ul{
        display:none;
        left: -40px;
        position: relative;
    }        
    li:hover ul{
        display:block;

    }
    ul#sub1 li:hover{
        background-color:red; 
    }

TRY IT

查看更多
等我变得足够好
3楼-- · 2020-02-15 04:36

Your html is wrong use, ul#sub1 should be child of <li>

<ul id="navmenu">
<li><a href="#">Hyperlink 1</a></li>
<li><a href="#">Hyperlink 2</a>
    <ul id="sub1"> 
        <li><a href="#">Hyperlink 2.1</a></li>
        <li><a href="#">Hyperlink 2.2</a></li>
    </ul>
</li>
<li><a href="#">Hyperlink 3</a></li>
<li><a href="#">Hyperlink 4</a></li>
</ul>
查看更多
We Are One
4楼-- · 2020-02-15 04:42

JSFIDDLE

screen capture:

enter image description here

Use correct HTML buddy:

<ul id="navmenu">
<li><a href="#">Hyperlink 1</a></li>
<li><a href="#">Hyperlink 2</a>
    <ul id="sub1"> 
        <li><a href="#">Hyperlink 2.1</a></li>
        <li><a href="#">Hyperlink 2.2</a></li>
    </ul>
</li>
<li><a href="#">Hyperlink 3</a></li>
<li><a href="#">Hyperlink 4</a></li>
</ul>

And, add this CSS:

li ul{
    display:none;
}

li:hover ul{
    display:block;
}
查看更多
Ridiculous、
5楼-- · 2020-02-15 04:48

Like this

please put ul in

  • submenu
  • DEMO

    HTML

    <ul id="navmenu">
    <li><a href="#">Hyperlink 1</a></li>
    <li><a href="#">Hyperlink 2</a>
        <ul id="sub1"> 
            <li><a href="#">Hyperlink 2.1</a></li>
            <li><a href="#">Hyperlink 2.2</a></li>
        </ul>
        </li>
    <li><a href="#">Hyperlink 3</a></li>
    <li><a href="#">Hyperlink 4</a></li>
    </ul>
    

    CSS

     *{
            margin: 0px;
            padding: 0px;}
    
        body
        {
            font-family: verdana;
            background-color: ABC;
            padding: 50px;
        }
    
        h1
        {
            text-align: center;
            border-bottom: 2px solid #009;
            margin-bottom: 50px;
        }
    
        /*rules for navigation menu */
        /*============================================*/
        ul#navmenu, ul.sub1
        {
            list-style-type: none;<!-- sets bullets to none -->
    
        }
    
        ul#navmenu li
        {
    
            width: 125px;
            text- align: center;
            position: relative;
            float: left;
            margin-right: 4px;
        }
    
        ul#navmenu a
        {
            text-decoration: none;
            display: block; <!-- this code makes the link a button instead  pointing specifically on the link -->
            width: 125px;
            height: 25px;
            line-height: 25px;
            background-color: #FFF;
            border: 1px solid #CCC;
            border-radius: 5px;
        }
    
        ul#navmenu .sub1 li
        {
    
        }
    
        ul#navmenu .sub1 a 
        {
            margin-top: 0px;
        }
    
        ul#navmenu li:hover > a
        {
         background-color: #CFC; <!-- sets all link color when hovering to yellow  -->
        }
    
        ul#navmenu li:hover a: hover
        {
            background-color: #FF0;  <!-- sets all link color when hovering to yellow  -->
        }
    
        ul#navmenu ul.sub1
        {
            display: none;
            position: absolute;
            top: 26px;
            left: 0px;
    
        }
    
        ul#navmenu li:hover .sub1
        {
            display: block;
        }
    li ul{
        display:none;
    }
    
    li:hover ul{
        display:block;
    }
    

    DEMO2

    DEMO3

    查看更多
    登录 后发表回答