我试图让我的导航栏负责,并且当页面变小时,导航栏会转换为汉堡菜单。它正在尝试中,但现在还没有转变为汉堡菜单。当屏幕变小时,我的按钮和开关仅从导航栏中消失。
<nav class="navbar navbar-expand-lg">
<a class="navbar-brand"><i class="far fa-chart-bar"></i></a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="news/news.html">News</a>
</li>
</ul>
</ul>
</div>
<!-- Here is a switch for Light/Dark mode -->
<div class="theme-switch-wrapper" id="switch">
<label class="theme-switch" for="checkbox">
<input type="checkbox" id="checkbox" />
<div class="slider round"></div>
</label>
<em></em>
</div>
</nav>
CSS
.navbar.navbar-expand-lg {
background-color:#6b7fdf;
height: 55px;
width: 100%;
}
.nav-link.dropdown-toggle {
color: black;
font-weight: bold;
margin-right: 0 auto;
}
.navbar-brand {
margin-top: 10px;
}
答案 0 :(得分:0)
像这样更改HTML
标记。您有两个ul
结束标签</ul>
,这是不正确的。
<nav class="navbar navbar-expand-lg">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<i class="fas fa-bars"></i>
</button>
<a class="navbar-brand"><i class="far fa-chart-bar"></i></a>
<!-- Here is a switch for Light/Dark mode | For Mobile -->
<div class="theme-switch-wrapper d-lg-none" id="switch">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1"></label>
</div>
</div>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto mr-auto">
<li class="nav-item">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="news/news.html">News</a>
</li>
</ul>
</div>
<!-- Here is a switch for Light/Dark mode | for Desktop -->
<div class="theme-switch-wrapper d-none d-lg-block" id="switch">
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1"></label>
</div>
</div>
</nav>
CSS
.navbar.navbar-expand-lg {
background-color:#6b7fdf;
height: auto;
width: 100%;
}
.nav-link{
color: white;
}
.nav-link.dropdown-toggle {
color: black;
font-weight: bold;
margin-right: 0 auto;
}
.navbar-toggler{
border: 1px solid white;
color: white;
}
您必须为CSS
和navbar-toggler
添加自定义switch
。我的HTML
标记格式正确。
这里是CodePen。
桌面视图:
移动视图: