将导航栏切换按钮向右对齐
Posted
技术标签:
【中文标题】将导航栏切换按钮向右对齐【英文标题】:Align navbar toggle button to the right 【发布时间】:2017-04-30 09:42:15 【问题描述】:我正在玩 Bootstrap 4,更具体地说是导航栏菜单。有没有一种方法可以让导航栏的小切换按钮与页面右侧对齐,而不是让它漂浮在徽标旁边的左侧?
这是我当前的代码。
@media (min-width: 992px)
.navbar-nav li a line-height: 85px;
@media (max-width: 991px)
.navbar-brand float: none;
.navbar-toggler
border:none;
width:1em;
<nav class="navbar navbar-light bg-faded navbar-full">
<a class="navbar-brand" href="#"><img class="img-fluid" src="img/logoMedium.png" /></a>
<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
<div class="collapse navbar-toggleable-md" id="navbarResponsive">
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Solar Power</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">No Obligation Quote</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</nav>
感谢您的帮助:)
【问题讨论】:
"navbar-toggler-right" 按钮上的类来切换可以帮助你。 【参考方案1】:我无法让上述任何解决方案发挥作用。所以这个简单的 hack 就可以了:
HTML:
<nav class="navbar navbar-expand-lg navbar-light bg-light sticky-top">
<div class="container-fluid">
<div class="row d-inline">
<div class="col">
<a class="navbar-brand" href="% url 'home' %">
<img src="% static 'img/logo.png' %" class="d-inline-block">
<span class="fs-2">Abstract Spacecraft</span>
</a>
</div>
<div class="col">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation" id="as-navbar-toggler">
<span class="navbar-toggler-icon"></span>
</button>
</div>
</div>
CSS:
#as-navbar-toggler
position: absolute;
right: 5px;
top: 5px;
height: 35px;
padding: 0px;
结果(在 Chrome 的 iPhone5 屏幕模拟器中):
这样做的好处是有 0 填充,它很好地覆盖了默认(居中)页面标题。
【讨论】:
我认为如果徽标很小,导航栏切换按钮会与标题发生冲突。我真的是一个引导新手,我也在寻找关于我的代码的任何建议。【参考方案2】:试试这段代码:
.navbar-toggler
margin-left: 90% !important;
【讨论】:
【参考方案3】:只需使用navbar-expand-lg
,您的问题就会得到解决。
应该是这样的:
<nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top">
【讨论】:
【参考方案4】:你必须像另一个按钮标签一样对齐它,首先将它包装在一个容器中以便以后能够移动它,然后创建一个带有 .text-right 类的 div,然后将你的切换按钮放在里面。请参阅下面的示例。
<div class='container'>
<div class='text-right'>
<button> Button1 </button>
</div>
</div>
或
<div class='container text-right'>
<button> Button1 </button>
</div>enter code here
两种解决方案都可以在 Bootstrap 4.5 中完美运行
【讨论】:
【参考方案5】: .navbar-light .navbar-nav .nav-link
color: rgb(255, 255, 255) !important;
.navbar
display: block !important;
min-height: 54px;
.pull-right
float:right !important;
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<!-- Latest compiled javascript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light" style="background-color: #de7a22 !important;">
<button class="navbar-toggler pull-right" type="button" data-toggle="collapse" data-target="#myNavbar" aria-controls="myNavbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#"><i class="fa fa-home"></i><span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Home</a></li>
<li class="nav-item">
<a class="nav-link" href="#">About</a></li>
<li class="nav-item">
<a class="nav-link" href="#">Gallery</a></li>
<li class="nav-item">
<a class="nav-link" href="#">Services</a></li>
<li class="nav-item">
<a class="nav-link" href="#">Sponsorship</a></li>
<li class="nav-item">
<a class="nav-link" href="#">Contact us</a></li>
</ul>
</div>
</nav>
</body>
【讨论】:
对您的解决方案进行一些解释会有所帮助。【参考方案6】:这在 Bootstrap 4 中对我有用
<button type="button" class="navbar-toggler navbar-toggler-right"
data-toggle="collapse" data-target=".navbar-collapse"
aria-controls="navbar-collapse" aria-expanded="false" aria-label="Toggle navigation">
或者更简单的Bootstrap docs
<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>
【讨论】:
【参考方案7】:只需将ml-auto
类添加到您的切换按钮
这样你就可以让 margin-left:auto
<button type="button" class="navbar-toggler ml-auto hidden-sm-up float-xs-right" data-toggle="collapse" data-target="#navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
【讨论】:
【参考方案8】:这就是我解决这个问题的方法:
<header>
<nav class="navbar navbar-fixed-top navbar-dark bg-inverse">
<a class="navbar-brand" href="#">Fixed top</a>
<button type="button" class="navbar-toggler hidden-sm-up float-xs-right" data-toggle="collapse" data-target="#navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
<div class="clearfix"></div>
<div class="collapse navbar-toggleable-xs" id="navbarResponsive">
<ul class="nav navbar-nav">
<li class="nav-item active">
<a href="#" class="nav-link">Home<span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">Planets</a>
</li>
<li class="nav-item">
<a href="" class="nav-link">Spaceships</a>
</li>
<li class="nav-item">
<a href="" class="nav-link">About</a>
</li>
<li class="nav-item">
<a href="" class="nav-link">Contact</a>
</li>
</ul>
</div>
</nav>
</header>
请注意,您必须使用 clearfix,否则按钮的浮动会在折叠时对菜单做一些奇怪的事情。
【讨论】:
【参考方案9】:使用.float-xs-right
类:
<button class="navbar-toggler hidden-lg-up float-xs-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
或者将float: right;
添加到.navbar-toggler
,如下面的sn-p:
Note that pull-right has been removed in v4.
@media (min-width: 992px)
.navbar-nav li a
line-height: 85px;
@media (max-width: 991px)
.navbar-brand
float: none;
.navbar-toggler
border: none;
width: 1em;
float: right;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js"></script>
<nav class="navbar navbar-light bg-faded navbar-full">
<a class="navbar-brand" href="#">
<img class="img-fluid" src="img/logoMedium.png" />
</a>
<button class="navbar-toggler hidden-lg-up" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
<div class="collapse navbar-toggleable-md" id="navbarResponsive">
<ul class="nav navbar-nav">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About Us</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Solar Power</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">No Obligation Quote</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Contact Us</a>
</li>
</ul>
</div>
</nav>
【讨论】:
【参考方案10】:放置类pull-right,你会得到你正在寻找的结果。
<button class="navbar-toggler hidden-lg-up pull-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"></button>
或者您可以使用以下参数定义样式:
float: right !important;
【讨论】:
能否请您更具体地说明我应该添加哪个标签,因为我尝试过拉右标签,但没有成功。 请看一下。 感谢丹的帮助,但是我已经尝试过了,它什么也没做。 .pull-right 和 .pull-left 已从 Bootstrap 4 中删除:v4-alpha.getbootstrap.com/migration/#utilities以上是关于将导航栏切换按钮向右对齐的主要内容,如果未能解决你的问题,请参考以下文章