实例效果图

IT料理

实例结构分析

  在nav标签里嵌入列表,在第二个列表项中嵌入列表。

实例样式分析

  设置nav中的列表水平显示,将第二个列表项中嵌入的列表垂直显示。当页面加载时,嵌套子列表隐藏,当鼠标悬停在第二个列表项上时,显示里列表空间。

实例代码

<!DOCTYPE html>  
<html>  
<head>  
    <title>简洁的HTML+CSS下拉菜单-HoverTree</title>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK" />
    <style type="text/css">
        html {
            -moz-box-sizing: border-box;
            box-sizing: border-box;
        }
        a,a:hover{color:#FFF;text-decoration:none}
        body {
            background: #27ae60;
        }
        h1 {
            width: 500px;
            margin: 100px auto 20px;
            color: #f0f0f0;
            text-align: center;
        }
        .nav {
            width: 500px;
            margin: 0 auto;
            background: #1e8449;
            color: #f0f0f0;
        }
        nav ul {
            list-style: none;
            padding-left: 0;
            margin-top: 0;
            margin-bottom: 0;
        }
        .nav a {
            padding: 0 16px;
            cursor: pointer;
        }
        .nav_menu {
            line-height: 45px;
            font-weight: 700;
        }
        .nav_menu-item {
            display: inline-block;
            position: relative;
        }
        .nav_menu-item:hover {
            background-color: #FF9600;
        }
        .nav_menu-item:hover .nav_submenu {
            display: block;
        }
        .nav_submenu {
            font-weight: 300;
            display: none;
            position: absolute;
            width: 220px;
            background-color: #9b59b6;
        }
        .nav_submenu-item:hover {
            background: rgba(0, 0, 0, 0.5);
        }
    </style>
</head>  
<body>  
<!-- 代码 开始 -->  
<h1>简洁的HTML+CSS下拉菜单</h1>  
<nav class="nav">  
    <ul class="nav_menu">
        <li class="nav_menu-item"><a href="#">首页</a></li>
        <li class="nav_menu-item">
            <a href="#">web前端</a>
            <ul class="nav_submenu">
                <li class="nav_submenu-item"> <a href="#">html</a></li>
                <li class="nav_submenu-item"> <a href="#">CSS</a></li>
                <li class="nav_submenu-item"> <a href="#">JS</a></li>
            </ul>
        </li>
        <li class="nav_menu-item">
            <a href="#">编程语言</a>
            <ul class="nav_submenu">
                <li class="nav_submenu-item"> <a href="#">C</a></li>
                <li class="nav_submenu-item"> <a href="#">Java</a></li>
                <li class="nav_submenu-item"><a href="#">C#</a></li>
                <li class="nav_submenu-item"><a href="#">C++</a></li>
            </ul>
        </li>
        <li class="nav_menu-item"><a href="#">数据结构</a></li>
        <li class="nav_menu-item"><a href="#">软件工程</a></li>
    </ul>
</nav>  
<!-- 代码 结束 -->  
</body>  
</html>