gpt4 book ai didi

html - CSS Bootstrap navbar- 中心搜索栏,在折叠时占据整个宽度

转载 作者:行者123 更新时间:2023-11-28 04:32:55 25 4
gpt4 key购买 nike

我有一个包含 3 个元素的 Bootstrap v.3 导航栏。我想均匀对齐元素,以便搜索栏在导航栏中居中。当它折叠时,它会占据整个宽度。但是很难做到这一点。

这是它的 html:

      <nav class="navbar navbar-transparent navbar-fixed-top" role="navigation">

<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="col-lg-4 navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src="assets/img/logo.png" alt="logo">
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-left navbar-search-form active" role="search">
<div class="form-group">
<input type="text" value="" class="form-control" placeholder="Search...">
</div>
</form>
<ul class="nav navbar-nav">
<li class="active">
<a href="javascript:void(0);"><i class="fa fa-search"></i></a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><button href="#gsdk" class="btn btn-round btn-default">Sign in</button></li>
</ul>

</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>

我试图制作一个 fiddle对于它,但结果并不好,但希望那里的代码可以提供一些想法。

最佳答案

最简单的方法是使用flex,但是浏览器兼容性差,所以可以使用media queriesposition:absolute; .navbar-form.

position-relative 赋予 nav 内的 div .container。我给了它一些边界,所以你可以看到居中。您的 sign in 按钮一直停留在右上角,因为您已经为它指定了 navbar-right 类。

.navbar-header img{
width:70px;
margin:0;
}
.navbar-container{
position:relative;
border:1px solid red;
}
.navbar-header button span{
color:black;
background-color:green;
}
@media screen and (min-width:768px){
form.navbar-form{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
margin:0;
}
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<nav class="navbar navbar-transparent navbar-fixed-top" role="navigation">
<div class="container navbar-container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<img src="http://www.chinabuddhismencyclopedia.com/en/images/thumb/b/b8/Nature.jpg/240px-Nature.jpg" alt="logo">
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<form class="navbar-form navbar-search-form active" role="search">
<div class="form-group">
<input type="text" value="" class="form-control" placeholder="Search...">
<a href="javascript:void(0);"><i class="fa fa-search"></i></a>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li><button href="#gsdk" class="btn btn-round btn-default">Sign in</button></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>

关于html - CSS Bootstrap navbar- 中心搜索栏,在折叠时占据整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com