【问题标题】:Bootstrap navbar with variable and fixed width buttons?带有可变和固定宽度按钮的引导导航栏?
【发布时间】:2015-10-05 14:42:53
【问题描述】:

我正在尝试使用 Bootstrap 制作一个导航栏,左侧有品牌标志,中间有几个可变宽度的链接,右侧有一些固定宽度的按钮。我希望所有内容都在一条线上,并且可变宽度按钮适合任何可用空间。

请看这里:https://jsfiddle.net/wfu6yxqd/

2 个固定宽度的按钮应位于可变宽度链接的右侧

我不需要 Bootstrap 的内置导航栏折叠行为,因此在我的导航栏定义中不需要(我在某些屏幕尺寸下实现自己的自定义折叠)

非常感谢您的帮助!

代码:

<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>

【问题讨论】:

  • 尝试了什么?
  • 大量研究,使用各种导航栏和按钮对齐的许多不同调整和方法,没有任何效果......

标签: javascript jquery html twitter-bootstrap navbar


【解决方案1】:

这是一种方法:您可以在 navbar-nav 部分之外/之后为您的按钮创建一个 div,并使该部分符合 navbar

*我保留了可折叠功能,因为我认为您将拥有一些用于移动导航的东西,其中有类似的元素可以解决。

查看示例

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>

【讨论】:

  • 谢谢,但是当我运行该代码 sn-p 并单击全屏时,我看到 3 个居中的链接,它们的宽度固定,两边有很大的间隙——链接应该扩展到这个空间- 即最左边的品牌和最右边的固定宽度按钮之间的所有备用导航栏空间......
  • “展开”和“中间有几个可变宽度的链接”是什么意思。你是说你想要导航栏上的零可用空间吗?这些链接的宽度不是固定的,它们是其中任何文本的宽度。
  • 正确,导航栏上的可用空间为零(或至少相当接近) - 即中间的几个链接只是扩大或缩小(根据窗口宽度),因此它们一起填充所有左侧品牌和右侧固定宽度按钮之间的可用空间。而且它们从不换行,因此导航栏变得更厚 - 在某个折叠点(例如 800 像素或其他任何东西)我的 Javascript 代码为小屏幕宽度实现了不同的手机/平板电脑 UI(但我可以处理的折叠位,我只是在问关于此处的可扩展导航栏链接)
【解决方案2】:

这个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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多