首页 > 解决方案 > HTML | CSS:汉堡菜单的问题

问题描述

我的名字是 Troy,我一直在尝试在 HTML 和 CSS 上创建一个汉堡菜单。

我在 YouTube 上找到了一个教程,效果很好,直到我<nav></nav>在它下面添加。我将在此处包含指向 youtube 视频的链接。

所以基本上问题是我尝试<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 问题

-特洛伊

标签: htmlcss

解决方案


好的,我想我已经找到了解决您问题的方法。最初,我怀疑与 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;
}

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

固定图像

自己尝试一下,让我知道它是否可以解决您的问题或者您是否仍需要帮助。无需跳转到 Bootstrap 或任何复杂的框架——它是一个基本的 CSS 实现,你就在那里,只是缺少几行代码。


推荐阅读