CSS學習案例(14):下拉導航選單

Rengarhunt發表於2020-12-06

在這裡插入圖片描述

	<div class="container">
        <nav class="menu">
            <ul>
                <li><a href="#">Home</a></li>
                <li>
                    <a href="#">About Me</a>
                    <div class="sub">
                        <ul>
                            <li> <a href="#">Photo</a></li>
                            <li> <a href="#">Video</a></li>
                        </ul>
                    </div>
                </li>
                <li>
                    <a href="#">Articles on HTML5&CSS3</a>
                    <div class="sub">
                        <ul>
                            <li> <a href="#">Difference
                                    between SVG vs. Canvas</a></li>
                            <li> <a href="#">New features in HTML5</a></li>
                            <li> <a href="#">Creating
                                    links to sections within a webpage</a></li>
                        </ul>
                    </div>
                </li>
                <li><a href="#">News</a></li>
                <li><a href="#">Contact Us</a></li>
            </ul>
        </nav>
    </div>
*{margin: 0;padding: 0;}

body{
    height: 100vh;width: 100vw;
    background-color: #2c3e50;
    display: flex;
    justify-content: center;
    align-items: center;
}

.container{
    transform: translateY(-100px);
    text-align: center;
    font-weight: bold;
    color: #fff;
}

.menu>ul>li{
    width: auto;
    list-style: none;
    float: left;
    position: relative;
}

.menu>ul>li>a{
    display: block;
    background-color: #0984e3;
    text-decoration: none;
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    line-height: 60px;
    padding: 0 20px;
    min-width: 100px;
    transition: all .5s ease-in-out;
}

.menu>ul>li:hover>a{
    background-color: #000;
}

.sub>ul>li {
    border-bottom: 1px solid #777;
}

.sub>ul>li:last-child {
    border-bottom: none;
}

.sub>ul {
    list-style: none;
    transform: translateY(-100%);
    transition: all .5s ease-in-out;
}

.menu>ul>li:hover .sub{
    height: auto;
}

.menu>ul>li:hover .sub>ul{
    transform: translateY(0);
}

.sub{
    position: absolute;
    width   : 100%;
    height: 0;
    overflow: hidden;
}

.sub>ul>li>a{
    display: block;
    background-color: #333;
    text-decoration: none;
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    line-height: 60px;
    padding: 0 20px;
    
}

.sub>ul>li>a:hover{
    background-color: #555;
}

案例參考:學習:純CSS製作二級下拉導航選單

相關文章