在 flexbox 导航栏中创建一个下拉菜单

Yas*_*tan 3 html css flexbox

我制作了一个带有 flex 属性的导航栏。现在我希望向导航栏中的元素添加一个下拉菜单,但它没有按预期工作。

@font-face {
    font-family: Oxygen;
    src: url('https://fonts.googleapis.com/css?family=Oxygen');
}
body{
    margin: 0;
}
nav {
    background-color: #f8f8f8;
    display: flex;
    justify-content: space-between;
}
nav ul {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin: 0;
}
nav a {
    text-decoration: none;
    color: #777;
    padding: 15px;
    font-family: sans-serif;
    font-size: 18px;
}
nav a:hover {
    color: #000;
}
.logo a {
    font-size: 25px;
}
nav ul {
    list-style: none;
}
ul.drop-menu li {
    display: none;
    list-style: none;
}
li:hover > ul.drop-menu li {
    display: flex;
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Navbar</title>
    <link rel="stylesheet" href="nav.css">
</head>
<body>
    <nav>
        <ul class="left">
            <li class="logo"><a href="#">SoulOTrip</a></li>
            <li >
                <a href="#">Adventure Trips</a>
                <ul class="drop-menu">
                    <li>1</li>
                    <li>2</li>
                    <li>3</li>
                    <li>4</li>
                </ul>
            </li>
            <li><a href="#">Top Destinations</a></li>
            <li><a href="#">Explore</a></li>
        </ul>
        <ul class="right">
            <li class=""><a href="#">Username</a></li>
            <li class=""><a href="/login">Login</a></li>
            <li class=""><a href="/register">Register</a></li>
        </ul>
</nav>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我想要带有 flex 容器的下拉菜单,以便我可以创建 4 列全页宽度,我也会在其他选项卡上创建下拉菜单。

LGS*_*Son 7

通过添加position: relativenav,并position: absolutedropdown,将使dropdown相对位置/大小nav

flex: 1dropdown的 flex 项目 ( li)上设置将使它们水平拉伸

更新/添加了这 3 条规则

nav {
    position: relative;
    background-color: #f8f8f8;
    display: flex;
    justify-content: space-between;
}

li:hover > ul.drop-menu li {
    display: flex;
    flex: 1;
}
li > ul.drop-menu {
    position: absolute;
    display: flex;
    left: 0;
    top: 100%;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)

堆栈片段

nav {
    position: relative;
    background-color: #f8f8f8;
    display: flex;
    justify-content: space-between;
}

li:hover > ul.drop-menu li {
    display: flex;
    flex: 1;
}
li > ul.drop-menu {
    position: absolute;
    display: flex;
    left: 0;
    top: 100%;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)
@font-face {
    font-family: Oxygen;
    src: url('https://fonts.googleapis.com/css?family=Oxygen');
}
body{
    margin: 0;
}
nav {
    position: relative;
    background-color: #f8f8f8;
    display: flex;
    justify-content: space-between;
}
nav ul {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin: 0;
}
nav a {
    text-decoration: none;
    color: #777;
    padding: 15px;
    font-family: sans-serif;
    font-size: 18px;
}
nav a:hover {
    color: #000;
}
.logo a {
    font-size: 25px;
}
nav ul {
    list-style: none;
}
ul.drop-menu li {
    display: none;
    list-style: none;
}
li:hover > ul.drop-menu li {
    display: flex;
    flex: 1;
}
li > ul.drop-menu {
    position: absolute;
    display: flex;
    left: 0;
    top: 100%;
    width: 100%;
}
Run Code Online (Sandbox Code Playgroud)