如何在 Bootstrap 导航栏中设置汉堡菜单中项目的样式?
How can I style the items in the hamburger menu in a Bootstrap navbar?
我使用 Bootstrap4 创建了响应式导航栏。
当屏幕很大时,有 2 个按钮。屏幕小的时候有汉堡包菜单。
[在此处输入图片描述][1][在此处输入图片描述][2]
如何设置导航栏中元素的样式,使它们在汉堡菜单中看起来不再像按钮?
谢谢!
这是我的代码:
<a class="navbar-brand" href="index.html">MyJobBoard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav navbar-right ml-auto mt-2 mt-lg-0 float-right text-right">
<li class="nav-item active">
<a class="nav-link post-a-job-button pull-right">Create a profile</a>
</li>
<li class="nav-item">
<button type="button" class="btn btn-light mr-3 pull-right">Sign In</button>
</li>
<li class="nav-item">
<button type="button" class="btn btn-primary mr-3 pull-right">Post a Job</button>
</li>
</ul>
</div>
</nav> ```
[1]: https://i.stack.imgur.com/ujYIG.png
[2]: https://i.stack.imgur.com/lE4ib.png
一个解决方案是将所有导航项目结构化为 link,这样在较小的屏幕上,您将拥有正常的 Bootstrap 下拉菜单,然后到自定义按钮 类 应用于更大的显示器。
如果您的登录和 post-a-job 表单位于单独的页面上,那么您可以直接 link 使用其他页面,但如果您使用的是模态要显示表单,则需要使用 JavaScript 打开模式。
我在下面的示例中禁用了 post-a-job 按钮,因此您可以看到如何禁用按钮,以便未登录的人无法 post a工作。
$('a[href$="#signInModal"]').on("click", function() {
$("#signInModal").modal('show');
});
$('a[href$="#postJobModal"]').on("click", function() {
$("#postJobModal").modal('show');
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<style>
@media (min-width:768px) {
.btn-md {
display: inline-block;
font-weight: 400;
color: #212529;
text-align: center;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
}
.btn-primary-md {
color: #fff;
background-color: #007bff;
border-color: #007bff;
}
.navbar-light .navbar-nav .nav-link {
color: #fff;
}
.navbar-light .navbar-nav .nav-link.disabled {
color: rgba(255, 255, 255, 0.6);
}
.btn-md:not(:disabled):not(.disabled) {
cursor: pointer;
}
}<nav class="navbar navbar-expand-md navbar-light bg-light">
<!-- Container wrapper -->
<div class="container-fluid">
<!-- Navbar brand -->
<a class="navbar-brand" href="index.html">MyJobBoard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav navbar-right ml-auto mt-2 mt-lg-0 float-right text-right">
<li class="nav-item active">
<a class="nav-link post-a-job-button mr-md-3" href="#">Create a profile</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md" href="#signInModal" role="button" style="width: 6.125rem">Sign In</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md disabled" href="#postJobModal" role="button" style="width: 6.125rem" tabindex="-1" aria-disabled="true">Post a Job</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="modal fade" id="signInModal" tabindex="-1" aria-labelledby="signInModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="signInModalLabel">Sign In</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for signing in (or registering) goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Sign in</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="postJobModal" tabindex="-1" aria-labelledby="postJobModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="postJobModalLabel">Post a job</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for posting a job</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Post</button>
</div>
</div>
</div>
</div>
</style>
<nav class="navbar navbar-expand-md navbar-light bg-light">
<!-- Container wrapper -->
<div class="container-fluid">
<!-- Navbar brand -->
<a class="navbar-brand" href="index.html">MyJobBoard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav navbar-right ml-auto mt-2 mt-lg-0 float-right text-right">
<li class="nav-item active">
<a class="nav-link post-a-job-button mr-md-3" href="#">Create a profile</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md" href="#signInModal" role="button" style="width: 6.125rem">Sign In</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md disabled" href="#postJobModal" role="button" style="width: 6.125rem" tabindex="-1" aria-disabled="true">Post a Job</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="modal fade" id="signInModal" tabindex="-1" aria-labelledby="signInModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="signInModalLabel">Sign In</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for signing in (or registering) goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Sign in</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="postJobModal" tabindex="-1" aria-labelledby="postJobModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="postJobModalLabel">Post a job</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for posting a job</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Post</button>
</div>
</div>
</div>
</div>
我使用 Bootstrap4 创建了响应式导航栏。 当屏幕很大时,有 2 个按钮。屏幕小的时候有汉堡包菜单。
[在此处输入图片描述][1][在此处输入图片描述][2]
如何设置导航栏中元素的样式,使它们在汉堡菜单中看起来不再像按钮?
谢谢!
这是我的代码:
<a class="navbar-brand" href="index.html">MyJobBoard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav navbar-right ml-auto mt-2 mt-lg-0 float-right text-right">
<li class="nav-item active">
<a class="nav-link post-a-job-button pull-right">Create a profile</a>
</li>
<li class="nav-item">
<button type="button" class="btn btn-light mr-3 pull-right">Sign In</button>
</li>
<li class="nav-item">
<button type="button" class="btn btn-primary mr-3 pull-right">Post a Job</button>
</li>
</ul>
</div>
</nav> ```
[1]: https://i.stack.imgur.com/ujYIG.png
[2]: https://i.stack.imgur.com/lE4ib.png
一个解决方案是将所有导航项目结构化为 link,这样在较小的屏幕上,您将拥有正常的 Bootstrap 下拉菜单,然后到自定义按钮 类 应用于更大的显示器。
如果您的登录和 post-a-job 表单位于单独的页面上,那么您可以直接 link 使用其他页面,但如果您使用的是模态要显示表单,则需要使用 JavaScript 打开模式。
我在下面的示例中禁用了 post-a-job 按钮,因此您可以看到如何禁用按钮,以便未登录的人无法 post a工作。
$('a[href$="#signInModal"]').on("click", function() {
$("#signInModal").modal('show');
});
$('a[href$="#postJobModal"]').on("click", function() {
$("#postJobModal").modal('show');
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script>
<style>
@media (min-width:768px) {
.btn-md {
display: inline-block;
font-weight: 400;
color: #212529;
text-align: center;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: transparent;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
}
.btn-primary-md {
color: #fff;
background-color: #007bff;
border-color: #007bff;
}
.navbar-light .navbar-nav .nav-link {
color: #fff;
}
.navbar-light .navbar-nav .nav-link.disabled {
color: rgba(255, 255, 255, 0.6);
}
.btn-md:not(:disabled):not(.disabled) {
cursor: pointer;
}
}<nav class="navbar navbar-expand-md navbar-light bg-light">
<!-- Container wrapper -->
<div class="container-fluid">
<!-- Navbar brand -->
<a class="navbar-brand" href="index.html">MyJobBoard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav navbar-right ml-auto mt-2 mt-lg-0 float-right text-right">
<li class="nav-item active">
<a class="nav-link post-a-job-button mr-md-3" href="#">Create a profile</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md" href="#signInModal" role="button" style="width: 6.125rem">Sign In</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md disabled" href="#postJobModal" role="button" style="width: 6.125rem" tabindex="-1" aria-disabled="true">Post a Job</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="modal fade" id="signInModal" tabindex="-1" aria-labelledby="signInModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="signInModalLabel">Sign In</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for signing in (or registering) goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Sign in</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="postJobModal" tabindex="-1" aria-labelledby="postJobModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="postJobModalLabel">Post a job</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for posting a job</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Post</button>
</div>
</div>
</div>
</div>
</style>
<nav class="navbar navbar-expand-md navbar-light bg-light">
<!-- Container wrapper -->
<div class="container-fluid">
<!-- Navbar brand -->
<a class="navbar-brand" href="index.html">MyJobBoard</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav navbar-right ml-auto mt-2 mt-lg-0 float-right text-right">
<li class="nav-item active">
<a class="nav-link post-a-job-button mr-md-3" href="#">Create a profile</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md" href="#signInModal" role="button" style="width: 6.125rem">Sign In</a>
</li>
<li class="nav-item">
<a class="nav-link btn-md btn-primary-md mr-md-3 text-white-md disabled" href="#postJobModal" role="button" style="width: 6.125rem" tabindex="-1" aria-disabled="true">Post a Job</a>
</li>
</ul>
</div>
</div>
</nav>
<div class="modal fade" id="signInModal" tabindex="-1" aria-labelledby="signInModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="signInModalLabel">Sign In</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for signing in (or registering) goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Sign in</button>
</div>
</div>
</div>
</div>
<div class="modal fade" id="postJobModal" tabindex="-1" aria-labelledby="postJobModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="postJobModalLabel">Post a job</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Form for posting a job</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">Post</button>
</div>
</div>
</div>
</div>