You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Hamburger Menu图标置于网页标题右侧并解决重叠问题

解决方案

要实现汉堡图标在「Tools」文字左侧并排显示且位于导航栏右侧,只需调整导航栏布局和汉堡菜单的定位方式,具体修改如下:

修改后的HTML

<nav class="navbar navbar-expand-lg navbar-custom">
  <!--Hamburger Menu-->
  <div class="hamburger-menu">
    <input id="menu__toggle" type="checkbox" />
    <label class="menu__btn" for="menu__toggle">
      <span></span>
    </label>

    <ul class="menu__box">
      <li><a class="menu__item" href="#">Home</a></li>
      <li><a class="menu__item" href="#">test</a></li>
    </ul>
  </div>
  
  <h1>Tools</h1>
</nav>

修改后的CSS

* {
  margin: 0;
  padding: 0;
}

.my-container {
  border: 1px solid green;
}

.my-row {
  border: 3px solid red;
  height: 300px;
}

.my-col {
  border: 3px dotted blue;
}

.navbar-custom {
  background-color: #bb0008;
  /* 设置flex布局,内容靠右、垂直居中 */
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 1rem 1.5rem;
}

#menu__toggle {
  opacity: 0;
}

#menu__toggle:checked+.menu__btn>span {
  transform: rotate(45deg);
}

#menu__toggle:checked+.menu__btn>span::before {
  top: 0;
  transform: rotate(0deg);
}

#menu__toggle:checked+.menu__btn>span::after {
  top: 0;
  transform: rotate(90deg);
}

#menu__toggle:checked~.menu__box {
  left: 0 !important;
}

.menu__btn {
  /* 移除fixed定位,改为相对定位让元素回到文档流 */
  position: relative;
  width: 26px;
  height: 26px;
  cursor: pointer;
  z-index: 1;
}

.menu__btn>span,
.menu__btn>span::before,
.menu__btn>span::after {
  display: block;
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: white;
  transition-duration: .25s;
}

.menu__btn>span::before {
  content: '';
  top: -8px;
}

.menu__btn>span::after {
  content: '';
  top: 8px;
}

.menu__box {
  display: block;
  position: fixed;
  top: 0;
  left: -100%;
  width: 300px;
  height: 100%;
  margin: 0;
  padding: 80px 0;
  list-style: none;
  background-color: #ECEFF1;
  box-shadow: 2px 2px 6px rgba(0, 0, 0, .4);
  transition-duration: .25s;
}

.menu__item {
  display: block;
  padding: 12px 24px;
  color: #333;
  font-family: 'Roboto', sans-serif;
  font-size: 20px;
  font-weight: 600;
  text-decoration: none;
  transition-duration: .25s;
}

.menu__item:hover {
  background-color: #CFD8DC;
}

/* 给标题设置间距和白色文字,保证视觉可读性 */
.navbar-custom h1 {
  color: white;
  margin-left: 1rem;
  font-size: 1.5rem;
}

关键修改说明

  • 给.navbar-custom添加flex布局属性,让导航栏内的元素自动靠右对齐且垂直居中,同时添加内边距避免内容贴紧边缘。
  • 移除.menu__btn的fixed定位,改为relative,让汉堡图标回到文档流中,和「Tools」文字自然并排。
  • 给h1添加左边距和白色文字,优化视觉间距和可读性。

内容的提问来源于stack exchange,提问作者DewNohMad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 23:36:24