css实现二级菜单

注意事项:
1.二级菜单的hover事件一定要写在父元素才起作用
2.为了防止导航栏下面的banner图或内容盖住二级菜单,需要给导航栏定位, 来提高二级菜单的层级
3.定位之后,二级菜单的宽将不再继承它父元素的宽,需要重新定义,否则它将被内容撑开

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin:0 ;
            padding:0
        }
    ul li{     
        width: 100px;
         height:30px;
         line-height:30px;  
         background-color: yellow;
         list-style: none;
         float: left;   
         position: relative
     }
    div{
       
        width: 100px;
        height: 100px;
        background-color: red;
        display: none;
        position: absolute;
        left:0;
        top:30px;
    }
    li:hover{
        background-color: yellow
    }
    ul li:hover div{
       display: block
    }
    </style>
</head>
<body>

    <ul>
        <li><a href="">菜单栏一</a>                
          <div>菜单栏二</div>
        </li>
        <li><a href="">菜单栏一</a>                
            <div>菜单栏二</div>
        </li>
        <li><a href="">菜单栏一</a>                
            <div>菜单栏二</div>
        </li>
        <li><a href="">菜单栏一</a>                
            <div>菜单栏二</div>
        </li>
    </ul>
    
</body>
</html>
    原文作者:李慧慧2016
    原文地址: https://blog.csdn.net/lihuihui2016/article/details/88096219
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞