带有永久右对齐图标的Bootstrap导航栏

时间:2015-08-04 14:42:01

标签: html css twitter-bootstrap

您好我在创建一个包含不可折叠的右对齐图标的bootstrap(v3.3.4)导航栏时遇到问题。正确的解决方案应该是“品牌”,然后是主页按钮,然后是可折叠的左对齐菜单,最后是右对齐的图标,它们永远不会折叠。

我有一个我一直在研究的例子,它接近所需的输出:

但是,当单击右对齐的下拉菜单时(当页面很窄并且左对齐的链接被折叠时),右对齐的“信封”图标会移动。

这个问题与其他一些问题有关但不一样(这个模型部分基于此问题):

任何帮助非常感谢。堆栈溢出请求下面粘贴的代码

HTML:

<nav class="navbar navbar-default navbar-static">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <a class="navbar-brand" href="#">Brand</a>
      <ul class="nav mynav pull-left">
        <li>
          <a title="Homepage" href="#"><i class="glyphicon glyphicon-home"></i></a>
        </li>
      </ul>
      <button type="button" class="navbar-toggle collapsed" 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>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse navbar-left" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>

    </div>
    <!-- /.navbar-collapse -->

    <ul class="nav navbar-nav navbar-right mobileNav pull-right">
      <li class="dropdown">
        <a title="email" href="#"><span class="glyphicon glyphicon-envelope"></span></a>
      </li>
      <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-download-alt"></span><span class="caret"></span></a>
        <ul class="dropdown-menu" role="menu">
          <li><a href="#">Action</a></li>
          <li><a href="#">Another action</a></li>
          <li><a href="#">Something else here</a></li>
          <li class="divider"></li>
          <li><a href="#">Separated link</a></li>
        </ul>
      </li>
    </ul>

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

CSS:

.mynav.pull-left > li > a {
  color: #777;
  padding-top: 15px;
  padding-bottom: 15px;
}

.mobileNav {
  position: absolute;
  right: 20px;
  top: 0px;
}

.mobileNav > li {
  display: inline-block;
  vertical-align: text-top;
  float: left;
}

.navbar-right ul.dropdown-menu {
  text-align: right;
}

@media only screen and (max-width: 768px) {
  .mobileNav {
    text-align: right;
  }
  .navbar-nav .open .dropdown-menu {
    border: 1px solid #ccc;
    margin-top: 2px;
    background: white;
    box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
    -webkit-box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
  }
  .navbar-default .navbar-nav>.open>a,
  .navbar-default .navbar-nav>.open>a:hover,
  .navbar-default .navbar-nav>.open>a:active,
  .navbar-default .navbar-nav>.open>a:focus {
    background: none;
  }
}

.navbar-toggle {
  float: left
}

.navbar-inverse {
  background-color: #337ab7;
  border-color: #337ab7;
}

.navbar-inverse .navbar-brand {
  color: #ffffff;
  white-space: nowrap;
}

2 个答案:

答案 0 :(得分:1)

将此添加到您的CSS:

@media (max-width: 767px) {
   .navbar-nav .open .dropdown-menu {
       position: absolute;
       left: auto;
       right: 0;
   }
}

当前设置为position: static(默认为Bootstrap),未设置左/右。

修改:这是preview。更改是在CSS框内的第34行之后。

答案 1 :(得分:1)

这个css使用弹性盒,对我来说效果很好。我是在http://the-echoplex.net/flexyboxes/

生成的
.dropdown-menu > li
{
      display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: row;
    -ms-flex-direction: row;
    flex-direction: row;
    -webkit-flex-wrap: nowrap;
    -ms-flex-wrap: nowrap;
    flex-wrap: nowrap;
    -webkit-justify-content: space-between;
    -ms-flex-pack: justify;
    justify-content: space-between;
    -webkit-align-content: space-around;
    -ms-flex-line-pack: distribute;
    align-content: space-around;
    -webkit-align-items: flex-start;
    -ms-flex-align: start;
    align-items: flex-start;

}