Bootstrap navbar with variable and fixed width buttons?

雨燕双飞 提交于 2019-12-24 12:33:45

问题


I'm trying to make a navbar with Bootstrap that has a brand logo on the left, a couple of variable width links in the middle, and some fixed width buttons on the right. I want everything on one line, and the variable width buttons to just fit in whatever space is available.

See here: https://jsfiddle.net/wfu6yxqd/

the 2 fixed width buttons should be to the right of the variable width links

I don't need Bootstrap's built in navbar collapse behaviour so not needed in my navbar definition (I am implementing my own custom collapse at certain screensizes)

Many thanks for any help!

Code:

<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="navbar-header">
    <a class="navbar-brand" href="#">Brand</a>

    <ul class="nav nav-justified">
        <li><a href="">Variable width link 1</a></li>
        <li><a href="">Variable width link 2</a></li>
    </ul>
</div>

<div> <!-- class="navbar-header" ?? -->
    <a class="btn btn-xs btn-default">Fixed width button</a>
    <div class="btn-group">
        <button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">FW drop down&nbsp;&nbsp;<span class="caret"></span></button>
        <ul class="dropdown-menu" role="menu">
            <li><a href="#">Menu item 1</a></li>
            <li><a href="#">Menu item 2</a></li>
        </ul>
    </div>
</div>
</nav>

回答1:


Here's one way to go: you can create a div outside/after the navbar-nav section for your buttons and conform that section to the navbar.

*I left the collapsible function in place since I image you'll have something for mobile navigation that will have similar elements to work around.

See example

body,
html {
  margin-top: 75px;
}
.navbar.navbar-custom {
  list-style: none;
  background: #444;
  line-height: 60px;
  margin: 0;
  height: 60px;
}
.navbar.navbar-custom .navi {
  display: table;
  width: 100%;
  list-style: none;
  text-align: center;
}
.navbar.navbar-custom .navi > li {
  display: table-cell;
  cursor: pointer;
}
.navbar.navbar-custom .navi > li > a {
  text-align: center;
  text-decoration: none;
  color: #fff;
}
.navbar.navbar-custom .logo-brand {
  margin-left: 15px;
  color: #fff;
  text-decoration: none;
}
.navbar.navbar-custom .btn-nav {
  text-align: right;
}
.navbar.navbar-custom .btn-nav .btn.btn-black {
  height: 50px;
  border-radius: 0;
  background: #444;
  color: #fff;
  border: none;
  outline: none;
  box-shadow: none;
}
.navbar-custom .btn-group .dropdown-menu,
.navbar-custom .btn-group .dropdown-menu:hover,
.navbar-custom .btn-group .dropdown-menu:focus {
  left: auto;
  right: 0;
  border: none;
  background: #444;
  border-radius: 0;
}
.navbar-custom .btn-group .dropdown-menu > li > a {
  color: #fff;
}
.navbar-custom .btn-group .dropdown-menu > li > a:hover {
  background: #222;
}
.no-gutter >[class*='col-'] {
  padding-right: 0;
  padding-left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-custom navbar-fixed-top">
  <div class="container-fluid">
    <div class="row no-gutter">
      <div class="col-xs-2"><a class="logo-brand" href="#">Logo Brand</a>

      </div>
      <div class="col-xs-8">
        <ul class="navi">
          <li><a href="#">Link 1</a>

          </li>
          <li><a href="#">Link 2</a>

          </li>
        </ul>
      </div>
      <div class="col-xs-2">
        <div class="btn-nav">
          <div class="btn-group" role="group" aria-label="...">
            <button type="button" class="btn btn-black">1</button>
            <div class="btn-group" role="group">
              <button type="button" class="btn btn-black dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">2 <span class="caret"></span>

              </button>
              <ul class="dropdown-menu">
                <li><a href="#">Button link 1</a>

                </li>
                <li><a href="#">Button link 1</a>

                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</nav>
<div class="container">
  <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up
    one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum
    et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section
    1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia,
    looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de
    Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from
    a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College
    in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33
    of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes
    from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
    College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32
    and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit
    amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor
    at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from
    sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem
    ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a
    Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem
    Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem
    Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard
    McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable
    source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first
    line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
</div>



回答2:


How about this https://jsfiddle.net/t58f873j/

<div class="container-fluid">
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">

        <div class="row">
        <div class="col-xs-2">
                <a class="navbar-brand" href="#">Brand</a>
            </div>

            <div class="col-xs-6 text-center">
                <ul class="nav nav-justified">
                    <li><a href="">Variable width link 1</a></li>
                    <li><a href="">Variable width link 2</a></li>
                </ul>
            </div>

            <div class="col-xs-4">
                <a class="btn btn-xs btn-default">Fixed width button</a>
                <div class="btn-group">
                    <button type="button" class="btn btn-xs btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">FW drop down&nbsp;&nbsp;<span class="caret"></span></button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Menu item 1</a></li>
                        <li><a href="#">Menu item 2</a></li>
                    </ul>
                </div>
            </div>
        </div>          

</nav>



来源:https://stackoverflow.com/questions/32951382/bootstrap-navbar-with-variable-and-fixed-width-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!