gpt4 book ai didi

网页 | CSS : Issues with hamburger menu

转载 作者:行者123 更新时间:2023-11-28 01:13:16 39 4
gpt4 key购买 nike

我叫特洛伊,多年来我一直在尝试使用 HTML 和 CSS 创建汉堡包菜单。

我在 YouTube 上找到了一个教程,它运行良好,直到我添加了 <nav></nav>在它下面。我将包括指向 youtube 视频的链接 here .

所以基本上问题是我尝试添加 <nav></nav>在标题下,然后当我点击汉堡菜单时,它会破坏整个菜单,直到我再次关闭汉堡菜单:视频上有很多评论说我应该添加一个 min-height: 70px;在 CSS 中。试过了,但它什么也没做。我感谢正在阅读本文的读者花时间尝试找到帮助我的方法 <3

此外,这里有我的 HTML 代码:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Home | Tingle</title>
<link rel="stylesheet" href="./css/style.css">
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<div class="nav">
<h1>Tingle</h1>
<label for="toggle">&#9776;</label>
<input type="checkbox" id="toggle">
<div class="menu">
<a href="#">Home</a>
<a href="#">Documentation</a>
<a href="#">Invite</a>
</div>
</div>
<div id="discord-header">
<h1>Join the Discord Support Server!</h1>
<p>If you need support, or send suggestions, or want to talk about Tingle, just join our discord server!</p>
<button type="button" name="button"><a href="#">Invite</a></button>
</div>
</body>
</html>

这里是 CSS 代码:

body{
font-family: 'Poppins', sans-serif;
width: 100%;
height: 100%;
margin: 0;
}

.nav{
background: #5fffcb;
border-bottom: 1px solid #EAEAEB;
text-align: right;
height: 70px;
line-height: 70px;
}

.nav h1{
font-size: 25px;
float: left;
line-height: 70px;
padding-left: 20px;
margin: 0 30px 0 0;
}

.menu{
margin: 0 30px 0 0;
}

.menu a{
clear: right;
font-weight: bold;
text-decoration: none;
color: #282828;
margin: 0 10px;
line-height: 70px;
}

label{
margin: 0 40px 0 0;
font-size: 26px;
line-height: 70px;
display: none;
width: 26px;
float: right;
}

#toggle{
display: none;
}


@media only screen and (max-width: 500px){

nav{
height: auto;
min-height: 70px;
line-height: 70px;
}
label{
font-weight: bold;
display: block;
cursor: pointer;
}

.menu{
text-align: center;
width: 100%;
display: none;
}
.menu a{
display: block;
border-bottom: 1px solid #EAEAEB;
margin: 0;
}

#toggle:checked + .menu{
display: block;
}

这里你有一个关于实际发生的事情的图像: CSS Issue

-特洛伊

最佳答案

好的,我想我已经找到了解决您的问题的方法。最初,我怀疑与 z-index 有关,因为它看起来像是在其他文本的下方。然而,事实证明你有几个问题:

  1. 您的菜单在文档流中。它会将您的其余内容向下推。

  2. 您的菜单在展开时会覆盖顶部栏,因此一旦展开就无法收回。

  3. 您的菜单没有背景色,因此展开时其下方的所有内容都会显示。

在您的@media .menu CSS 下,您需要具有以下代码:

 .menu{
text-align: center;
width: 100%;
display: none;
position: fixed;
top: 72px;
z-index: 5;
background-color: #fff;
}

Position: fixed 将其从文档流中移除。顶部:72px 确保它不会覆盖顶部栏。 Z-index: 5 和 background-color: #fff 使它出现在页面其余部分的顶部。现在看起来像这样:

fixed image

亲自尝试一下,如果它能解决您的问题或您是否仍需要帮助,请告诉我。无需跳转到 Bootstrap 或任何复杂的框架 - 它是一个基本的 CSS 实现,您就在那里,只是缺少几行代码。

关于网页 | CSS : Issues with hamburger menu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51993247/

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