bootstrap navbar左右浮动

时间:2018-03-01 14:19:16

标签: html css html5 twitter-bootstrap css3

大家好我使用bootstrap 3并试图让我的导航栏看起来像这样: enter image description here

我面临的问题是试图让徽标向左浮动,然后图标浮动到右边,我已经做了研究并看到了其他堆栈问题,但对于我的生活我根本无法修复它,我不明白为什么,



.navbar-nav {
    padding-left: 15px;
    padding-right: 0;
}
.navbar-nav>li>a {
    line-height: 80px;
    padding: 0 15px !important;
}
.navbar-brand {
    color: #cccccc !important;
    font-size: 20px;
    line-height: 20px;
    font-weight: 500;
    height: auto;
    padding: 20px 15px;
}
.navbar-brand>img {
    width: auto;
    max-height: 28px;
    margin-top: 5px;
}
.navbar-default {
    background-color: white;
    border: none !important;
    border-bottom: 1px solid #e8e9eb !important;
}
.secondary-nav a {
    color: #111111;
}
.secondary-nav a:hover {
    color: #c39d6d;
}
.secondary-nav a.shopping-cart {
    line-height: 14px;
    position: relative;
    width: 45px;
    height: 20px;
}
.secondary-nav a.shopping-cart i {
    margin-right: 15px;
}
.secondary-nav .cart-badge {
    position: absolute;
    top: -4px;
    right: 6px;
    color: white;
    background: #ad8b60;
    border-radius: 50px;
    font-size: 9px;
    font-weight: 400;
    line-height: 10px;
    padding: 2px 4px;
}

.space-right {
    padding-right: 15px !important;
}

.navbar-nav {
    float: right;
}
nav .secondary-nav {
    float: right;
    margin-top: 30px;
    margin-left: 30px;
    padding-right: 15px;
}
.nav-sec .navbar-nav {
    float: none;
}
.nav-sec .navbar-nav>li {
    float: none;
    display: inline-block;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-default nav-sec navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="index.html"><img src="Images/logo.png" alt="logo"></a>
            </div><!-- / navbar-header -->
            <div class="secondary-nav">
                <a href="login-register.html" class="my-account space-right"><i class="fa fa-search"></i></a>
                <a href="shopping-cart.html" class="shopping-cart"><i class="fa fa-shopping-cart"></i> <span class="cart-badge">2</span></a>
            </div>
            <div class="navbar-collapse text-center collapse" aria-expanded="false" style="height: 1px;">
                <ul class="nav navbar-nav">
                    <li><a href="index.html"><span>HOME</span></a></li>
                    <li><a href="about.html"><span>ABOUT US</span></a></li>
                    <li><a href="about.html"><span>PRODUCTS</span></a></li>
                    <li><a href="about.html"><span>INSPIRATION</span></a></li>
                    <li><a href="contact.html"><span>CONTACT</span></a></li>
                </ul>
            </div><!--/ nav-collapse -->
        </div><!-- / container -->
 </nav>
&#13;
&#13;
&#13;

希望有人可以救我:)

再次感谢

4 个答案:

答案 0 :(得分:2)

您可以尝试在容器上使用display:flex和justify-content:space-between,这样如果您的容器有3个子项(徽标,主菜单,图标)自动放在左边,菜单放在中心,图标放在容器上右边。

我创建这个jsfiddle尝试看看它

https://jsfiddle.net/DTcHh/48838/

.navbar .container {
  display: flex;
  justify-content: space-between;
}


<nav class="navbar navbar-default nav-sec navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="index.html"><img src="Images/logo.png" alt="logo"></a>
            </div><!-- / navbar-header -->
            <div class="navbar-collapse text-center collapse" aria-expanded="false" style="height: 1px;">
                <ul class="nav navbar-nav">
                    <li><a href="index.html"><span>HOME</span></a></li>
                    <li><a href="about.html"><span>ABOUT US</span></a></li>
                    <li><a href="about.html"><span>PRODUCTS</span></a></li>
                    <li><a href="about.html"><span>INSPIRATION</span></a></li>
                    <li><a href="contact.html"><span>CONTACT</span></a></li>
                </ul>
            </div><!--/ nav-collapse -->
            <div class="secondary-nav">
                <a href="login-register.html" class="my-account space-right"><i class="fa fa-search"></i></a>
                <a href="shopping-cart.html" class="shopping-cart"><i class="fa fa-shopping-cart"></i> <span class="cart-badge">2</span></a>
            </div>
        </div><!-- / container -->
 </nav>

答案 1 :(得分:0)

好的,我想办法在下班后以某种方式修复它:

HTML:

 <nav class="navbar navbar-default" role="navigation">
  <div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </button>    
  </div>
  <div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <a class="navbar-brand" href="index.html"><img src="Images/logo.png" alt="logo"></a>
    </ul>
    <ul class="nav navbar-nav navbar-center">
                    <li><a href="index.html"><span>HOME</span></a></li>
                    <li><a href="about.html"><span>ABOUT US</span></a></li>
                    <li><a href="about.html"><span>PRODUCTS</span></a></li>
                    <li><a href="about.html"><span>INSPIRATION</span></a></li>
                    <li><a href="contact.html"><span>CONTACT</span></a></li>
    </ul>
    <ul class="nav navbar-nav navbar-right">
                  <div class="secondary-nav">
                <a href="login-register.html" class="my-account space-right"><i class="fa fa-search"></i></a>
                <a href="shopping-cart.html" class="shopping-cart"><i class="fa fa-shopping-cart"></i> <span class="cart-badge">2</span></a>
            </div>
    </ul>
  </div>
</nav>

CSS:

@media (min-width: 768px) {
  .navbar-nav.navbar-center {
    position: absolute;
    left: 50%;
    transform: translatex(-50%);
  }
}

nav .secondary-nav {
    float: right;
    margin-top: 30px;
    margin-left: 30px;
    padding-right: 15px;
}

答案 2 :(得分:0)

我为.container设置了一个弹性箱,并使用了.navbar-header(徽标)和.secondary-nav(图标)的边距自动设置。

查看仅在整页模式下正确。

&#13;
&#13;
.navbar .container {
  display: flex;
  align-items: center;
}

.navbar .container .navbar-header {
  margin-right: auto;
}

.navbar .container .secondary-nav {
  margin-left: auto;
}

.navbar-nav>li>a {
  line-height: 80px;
  padding: 0 15px !important;
}

.navbar-brand {
  color: #cccccc !important;
  font-size: 20px;
  line-height: 20px;
  font-weight: 500;
  height: auto;
  padding: 20px 15px;
}

.navbar-brand>img {
  width: auto;
  max-height: 28px;
  margin-top: 5px;
}

.navbar-default {
  background-color: white;
  border: none;
  border-bottom: 1px solid #e8e9eb;
}

.secondary-nav a {
  color: #111111;
}

.secondary-nav a:hover {
  color: #c39d6d;
}

.secondary-nav a.shopping-cart {
  width: 45px;
  height: 20px;
}

.secondary-nav a.shopping-cart i {
  margin-right: 15px;
}

.secondary-nav .cart-badge {
  color: white;
  background: #ad8b60;
  border-radius: 50px;
  font-size: 9px;
  font-weight: 400;
  line-height: 10px;
  padding: 2px 4px;
}

.space-right {
  padding-right: 15px !important;
}

.nav-sec .navbar-nav>li {
  display: inline-block;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default nav-sec navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                </button>
      <a class="navbar-brand" href="index.html"><img src="Images/logo.png" alt="logo"></a>
    </div>
    <!-- / navbar-header -->
    <div class="navbar-collapse text-center collapse" aria-expanded="false" style="height: 1px;">
      <ul class="nav navbar-nav">
        <li><a href="index.html"><span>HOME</span></a></li>
        <li><a href="about.html"><span>ABOUT US</span></a></li>
        <li><a href="about.html"><span>PRODUCTS</span></a></li>
        <li><a href="about.html"><span>INSPIRATION</span></a></li>
        <li><a href="contact.html"><span>CONTACT</span></a></li>
      </ul>
    </div>
    <div class="secondary-nav">
      <a href="login-register.html" class="my-account space-right"><i class="fa fa-search"></i></a>
      <a href="shopping-cart.html" class="shopping-cart"><i class="fa fa-shopping-cart"></i> <span class="cart-badge">2</span></a>
    </div>

    <!--/ nav-collapse -->
  </div>
  <!-- / container -->
</nav>
&#13;
&#13;
&#13;

答案 3 :(得分:-1)

您可以使用最初在bootstrap中给出的以下设计。 https://bootsnipp.com

中有很多设计可供使用

                                                                                                                                      

                <div class="business-logo-align col-md-10" >
                    <a href="#"><!--<img class="img-responsive " src="images/logo.jpg" alt="logo"/>-->   LOGO 
                    </a>
                </div> 

            </div>

            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav navbar-right">
                    <li class="col-md-6 col-xs-6 text-center"> <a href="login-register.html" class="my-account space-right"><i class="fa fa-search"></i></a></li>
                    <li class="col-md-6 col-xs-6 text-center"><a href="shopping-cart.html" class="shopping-cart"><i class="fa fa-shopping-cart"></i> <span class="cart-badge">2</span></a></li> 
                </ul>

                <div class=" col-xs-9 col-sm-12 col-md-8 pd-t-10" >
                    <ul class="nav navbar-nav  navbar-centerr">
                        <li><a href="#" class="busi-btn-active">Home</a></li>
                        <li><a href="#" class="busi-btn">ABOUT US</a></li>
                         <li><a href="#" class="busi-btn">PRODUCTS</a></li>
                        <li><a href="#" class="busi-btn">INSPIRATION</a></li>
                        <li><a href="#" class="busi-btn">CONTACT</a></li>
                    </ul>
                </div> 
            </div>
        </nav>

.busi-btn{    border-radius: 0px;   color: #003333 !important;  font-size: 18px; font-weight: 400; }
    .busi-btn:hover{    border-radius: 0px; color: #ff3c2d !important;  font-size: 18px; font-weight: 400; }
    .busi-btn-active,.busi-btn-active:hover{    border-radius: 0px; color: #ff3c2d !important;  font-size: 18px; font-weight: 400; }


.hom_nav_btn_red{  /* sign-up button  */
    border-radius: 0px;
    background-color:#ff3333; /* red color */
    border:0px !important;
    font-size:18px;
    font-weight: 400;
    padding: 7px 30px;
    margin: 0 -25px;
}
.hom_nav_btn{
    border-radius: 0px;
    color:#003333 !important; /* blue color */
    font-size:18px;
    font-weight: 400;
    padding: 7px 30px;
}
.navbar-centerr{
    float:none;
    margin: 0 auto;
    display: table;
    table-layout: fixed;
} 

.business-logo-align {
    float: left;
    padding: 5px 25px;
    font-size: 18px;
    line-height: 20px;
    height: 50px;
}