gpt4 book ai didi

html - 内容溢出 flex 容器

转载 作者:太空宇宙 更新时间:2023-11-04 08:51:39 25 4
gpt4 key购买 nike

我希望下拉菜单是这样的

enter image description here

这是我在网上找到的,它使用简单的下拉菜单。

我想要使用 flexbox 及其属性的下拉菜单。问题是列中元素的数量是动态的,但 flexbox 容器不会根据它改变它的高度。

这是我得到的

enter image description here

代码

* {
font-family: sans-serif;
}
@font-face {
font-family: Oxygen;
src: url('https://fonts.googleapis.com/css?family=Oxygen');
}
body{
margin: 0;
background-color: #cbcbcb;
}
nav {
position: relative;
background-color: #f8f8f8;
display: flex;
justify-content: space-between;
border-bottom-width: 1px;
border-bottom-color: black;
}
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;
}
li > ul.drop-menu {
display: none;
}
li:hover > ul.drop-menu {
position: absolute;
top: 100%;
background-color: white;
left: 0;
width: 100%;
display: flex;
justify-content: space-around;
padding-left: 0;
padding-top: 10px;
padding-bottom: 10px;
}
.drop-col {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.drop-col > ul {
flex-direction: column;
justify-content: center;
align-items: flex-start;
padding-left: 0;
}
.drop-header {
font-size: 18px;
font-family: sans-serif;
color: #ff3546;
padding-top: 5px;
padding-bottom: 5px;
line-height: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Navbar</title>
<script src="live.js"></script>
<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">
<div class="drop-col">
<ul>
<li class="drop-header">Airforce</li>
<li>Flying fox</li>
<li>Bungee Jumping</li>
<li>Paragliding</li>
</ul>
</div>
<div class="drop-col">
<ul>
<li class="drop-header">Army</li>
<li>Skiing</li>
<li>Mountaineering</li>
<li>Trekking</li>
<li>Rock Climbing</li>
</ul>
</div>
<div class="drop-col">
<ul>
<li class="drop-header">Navy</li>
<li>River Rafting</li>
<li>Parasailing</li>
<li>Scuba Diving</li>
<li>Swimming</li>
<li>Kayaking</li>
<li>Surfing</li>
</ul>
</div>
</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>

最佳答案

问题是,您不小心也将下拉菜单作为目标,因此也将其限制为 50px。您可以通过更改选择器来解决此问题

nav ul { 
/* this affects all ul elements inside the nav, the first level
.left as well as the .drop-menu */
}

以下内容:

nav > ul { /* <- this affects only the direct descendant (ul.left) */ }

或者,您可以将 height 声明更改为 min-height

* {
font-family: sans-serif;
}
@font-face {
font-family: Oxygen;
src: url('https://fonts.googleapis.com/css?family=Oxygen');
}
body{
margin: 0;
background-color: #cbcbcb;
}
nav {
position: relative;
background-color: #f8f8f8;
display: flex;
justify-content: space-between;
border-bottom-width: 1px;
border-bottom-color: black;
}
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;
}
li > ul.drop-menu {
display: none;
}
li:hover > ul.drop-menu {
position: absolute;
top: 100%;
background-color: white;
left: 0;
width: 100%;
display: flex;
justify-content: space-around;
padding-left: 0;
padding-top: 10px;
padding-bottom: 10px;
}
.drop-col {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.drop-col > ul {
flex-direction: column;
justify-content: center;
align-items: flex-start;
padding-left: 0;
}
.drop-header {
font-size: 18px;
font-family: sans-serif;
color: #ff3546;
padding-top: 5px;
padding-bottom: 5px;
line-height: 30px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Navbar</title>
<script src="live.js"></script>
<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">
<div class="drop-col">
<ul>
<li class="drop-header">Airforce</li>
<li>Flying fox</li>
<li>Bungee Jumping</li>
<li>Paragliding</li>
</ul>
</div>
<div class="drop-col">
<ul>
<li class="drop-header">Army</li>
<li>Skiing</li>
<li>Mountaineering</li>
<li>Trekking</li>
<li>Rock Climbing</li>
</ul>
</div>
<div class="drop-col">
<ul>
<li class="drop-header">Navy</li>
<li>River Rafting</li>
<li>Parasailing</li>
<li>Scuba Diving</li>
<li>Swimming</li>
<li>Kayaking</li>
<li>Surfing</li>
</ul>
</div>
</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>

关于html - 内容溢出 flex 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43400002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com