html - 为什么我的固定菜单在我的汉堡包图标下面?

标签 html css hamburger-menu

是否可以在固定菜单栏中找到汉堡包图标?

我试图将元素放在 body 标签中的 class="sidenav"中,但找不到图标。

span {
  font-size: 30px;
  cursor: pointer;
  position: absolute;
}


/* Style the navigation bar */

.navbar {
  width: 100%;
  margin-left: 0px;
  background-color: #555;
  overflow: auto;
}


/* Navbar links */

.navbar a {
  float: left;
  text-align: center;
  padding: 12px;
  color: white;
  text-decoration: none;
  font-size: 17px;
}
<body>
  <div id="main">
    <span onclick="openNav()">&#9776;</span>
    <div class="navbar">
      <a href="#">Home</a>
      <a href="#">Search</a>
      <a href="#">Contact</a>
      <a href="#">Login</a>
      <input type="text" placeholder="Search.." name="search">
      <button type="submit"><i class="fas fa-search"></i></button>
    </div>
  </div>
</body>

期待

Expectation

现实

Reality

最佳答案

试试这个,我注意到您的代码有一些可以改进的地方,首先,您可以使用 Flexbox 很容易地将所有元素对齐在一行上;从导航图标中删除绝对位置,以便一切正常,这也让您删除 float 属性。然后您可以将您的链接包裹在另一个 div(或 ul 标记)中并使用 margin-left: auto

将它们推到右边

span {
  font-size: 30px;
  cursor: pointer;
}


/* Style the navigation bar */

.navbar {
  width: 100%;
  background-color: #555;
  display: flex;
  align-items: center;
}


/* Navbar links */

.navlinks{
  margin-left: auto;
}

.navbar a {
  text-align: center;
  padding: 12px;
  color: white;
  text-decoration: none;
  font-size: 17px;
}
<body>
  <div id="main">
    <div class="navbar">
    <span onclick="openNav()">&#9776;</span>
      <div class="navlinks">
        <a href="#">Home</a>
      <a href="#">Search</a>
      <a href="#">Contact</a>
      <a href="#">Login</a>
      <input type="text" placeholder="Search.." name="search">
      <button type="submit"><i class="fas fa-search"></i></button>
      </div>
    </div>
  </div>

关于html - 为什么我的固定菜单在我的汉堡包图标下面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54413012/

相关文章:

jquery - 如何在将两者居中之前从屏幕外左侧滑入文本以附加到现有的居中句子

javascript - 我怎样才能给我的汉堡菜单动画?

html - 响应式导航

html - 如何获取同一行的菜单项

html - IE11浏览器显示文档模式为5(默认)

javascript - 如何在下拉列表更改时动态隐藏表格行?

javascript - 如何暂停 CSS 过渡?

jquery - 你能在网页上将图片叠加在一起吗?

javascript - 具有多个选项的多个列表

html - Angular JS 协助添加值和小数