1. 程式人生 > >用position來寫導航的下拉選單

用position來寫導航的下拉選單

今天來用position來寫下拉選單,話不多說,直接上程式碼,

<!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>下拉選單</title>
    <style>
        *{
            padding: 0;
            margin: 0;
            list-style: none;
            text-decoration: none;
        }
        #nav{
            height: 40px;
            background-color:black;
        }
        #nav li{
            display: inline-block;
            width: 100px;
            height: 40px;
            line-height: 40px;
            text-align: center;
            position: relative;
            border-right: 2px white solid;
        }
        #nav li a{
            color: aliceblue;
        }
        li .b{
            position: absolute;
            width: 100px;
            background-color: black;
            display: none;
        }
        li:hover .b{
            display: block;
        }
    </style>
</head>
<body>
    <div id="nav">
        <ul>
            <li><a href="#">首頁</a>
                <div class="b">
                    <ul>
                        <li><a href="#">1</a></li>
                        <li><a href="#">2</a></li>
                        <li><a href="#">3</a></li>
                    </ul>    
                </div></li>
            <li><a href="#">附頁1</a></li>
            <li><a href="#">附頁2</a></li>
            <li><a href="#">附頁3</a></li>
        </ul>
    </div>
</body>
</html>

如上程式碼,我們就可以實現一個下拉選單的實現。