保持所有屏幕尺寸的导航项目

时间:2016-02-09 16:41:49

标签: html css twitter-bootstrap

我正在尝试构建一个自举导航栏,左侧有导航链接,右侧有一些用户图标。
在移动设备上,导航链接应该折叠,但图标应该保留(或者甚至居中?)到汉堡图标。

它在桌面上看起来很好(宽度> 768px)。但是在较小的宽度上,图标会显示在彼此下方而不是彼此相邻。

到目前为止,这是我的代码:

<nav class="navbar navbar-inverse navbar-fixed-top bo-header">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <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="#">MY BRAND</a>
    </div>
    <!-- non-collapsing items -->
    <div class="navbar-header navbar-right">
      <ul class="nav navbar-nav">
        <li><a href="#"><i class="fa fa-envelope"></i></a></li>
        <li><a href="#"><i class="fa fa-users"></i></a></li>
        <li><a href="#"><i class="fa fa-wrench"></i></a></li>
      </ul>
    </div>
    <div id="navbar" class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li><a href="#">Nav Entry 1</a></li>
        <li><a href="#">Nav Entry 2</a></li>
        <li><a href="#">Nav Entry 3</a></li>
        <li><a href="#">Nav Entry 4</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

工作jsfiddle: http://jsfiddle.net/n9KtL/188/

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

以下是一个示例,说明如何将图标保留在导航栏container内(以便navbar内的所有元素保持一致),然后将图标放置在768px下的视口上。

@media (max-width: 767px) {
  .navbar .navbar-right.navbar-top {
    position: absolute;
    margin-top: 4px;
    right: 80px;
  }
  .navbar .navbar-right.navbar-top > li {
    display: inline-block;
  }
  .navbar .navbar-right.navbar-top > li > a {
    padding-right: 10px;
    padding-left: 10px;
  }
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<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.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-inverse navbar-fixed-top bo-header">
  <div class="container">
    <ul class="nav navbar-nav navbar-right navbar-top">
      <li><a href="#"><i class="fa fa-envelope"></i></a>
      </li>
      <li><a href="#"><i class="fa fa-users"></i></a>
      </li>
      <li><a href="#"><i class="fa fa-wrench"></i></a>
      </li>
    </ul>
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
        <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="#">MY BRAND</a>
    </div>
    <!-- non-collapsing items -->
    <div class="collapse navbar-collapse" id="navbar">
      <ul class="nav navbar-nav navbar-left">
        <li><a href="#">Nav Entry 1</a>
        </li>
        <li><a href="#">Nav Entry 2</a>
        </li>
        <li><a href="#">Nav Entry 3</a>
        </li>
        <li><a href="#">Nav Entry 4</a>
        </li>
      </ul>
    </div>
  </div>
</nav>