【问题标题】:Justify Navigation inside Twitter Bootstrap 3在 Twitter Bootstrap 3 中对齐导航
【发布时间】:2014-05-28 11:30:44
【问题描述】:

由于用于对齐导航的 Bootstrap 示例对我不起作用,因此我尝试构建自己的水平对齐导航。代码如下:

<div class="row">
    <div class="col-lg-12">
        <ul id="navigation">
             <li class=""><a href="#">One</a></li>
             <li class=""><a href="#">Two</a></li>
             <li class=""><a href="#">Three</a></li>
             <li class=""><a href="#">Four</a></li>
             <li class=""><a href="#">Five</a></li>
             <li class=""><a href="#">Six</a></li>
             <li class=""><a href="#">Seven<</a></li>
             <li class="stretch"></li>
        </ul>
    </div>
</div>

使用的 CSS:

#navigation {
    list-style-type: none;
    text-align: justify;
    height: 21px;
    background: #ccc
}

#navigation li {
    display: inline
}

#navigation .stretch {
    display: inline-block;
    width: 100%;

    /* if you need IE6/7 support */
    *display: inline;
    zoom: 1
}

当导航一开始就存在时,就像静态代码一样,一切看起来都很好。当我的应用程序动态创建导航时,稍后会插入链接。那么这个理由就不起作用了。

如何解决?

【问题讨论】:

  • 能否请您更清楚您的问题。此代码不会动态创建导航。当你说“理由”时,你是什么意思?什么是菜单“理由”?

标签: javascript html css twitter-bootstrap-3


【解决方案1】:

检查这个小提琴链接http://jsfiddle.net/Mohinder/PAvnp/

在你的 Bootstrap CSS 中添加这个 HTML 和 CSS,然后检查一下,我刚刚为你做了。

这里是 HTML

<div class="row">
    <div class="col-lg-12">
        <ul id="navigation">
             <li class=""><a href="#">One</a></li>
             <li class=""><a href="#">Two</a></li>
             <li class=""><a href="#">Three</a></li>
             <li class=""><a href="#">Four</a></li>
             <li class=""><a href="#">Five</a></li>
             <li class=""><a href="#">Six</a></li>
             <li class=""><a href="#">Seven</a></li>
        </ul>
    </div>
</div>

这里是 CSS

#navigation {float:none; text-align:center; display:table; list-style:none; width:100%; }
#navigation li { float:none; display:table-cell; }
#navigation li a { display:block; line-height:20px; margin:0px 5px; }

【讨论】:

    【解决方案2】:

    我已经设法让合理的导航栏与折叠一起使用。

    HTML:

     <nav class="navbar navbar-default" role="navigation">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
    <button type="button" class="navbar-toggle" 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" id="bs-example-navbar-collapse-1">
        <ul class="nav nav-justified">
              <li class="active"><a href="#">Home</a></li>
              <li><a href="#">Projects</a></li>
              <li><a href="#">Services</a></li>
              <li><a href="#">Downloads</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Contact us</a></li>
            </ul>
    
    
      </div><!-- /.navbar-collapse -->
    </nav>
    

    CSS:

      .navbar-collapse {
      padding-left: 0px;
      padding-right: 0px;
    }
     .nav-justified {
      background-color: #;
      border-radius: 0px;
      border: 0px solid #;
    }
     .nav-justified > li > a {
      margin-bottom: 0;
      padding-top: 15px;
      padding-bottom: 15px;
      color: #;
      font-weight: ;
      text-align: center;
      border-bottom: 0px  #;
      background-color: #; /* Old browsers */
      background-repeat: repeat-x; /* Repeat the gradient */
      background-image: -moz-linear-gradient(top, # 0%, # 100%); /* FF3.6+ */
      background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#f5f5f5), color-stop(100%,#e5e5e5)); /* Chrome,Safari4+ */
      background-image: -webkit-linear-gradient(top, #f5f5f5 0%,#e5e5e5 100%); /* Chrome 10+,Safari 5.1+ */
      background-image: -o-linear-gradient(top, #f5f5f5 0%,#e5e5e5 100%); /* Opera 11.10+ */
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f5f5f5', endColorstr='#e5e5e5',GradientType=0 ); /* IE6-9 */
      background-image: linear-gradient(top, #f5f5f5 0%,#e5e5e5 100%); /* W3C */
    }
     .nav-justified > .active > a,
     .nav-justified > .active > a:hover,
     .nav-justified > .active > a:focus {
      background-color: #ddd;
      background-image: none;;
    }
     .nav-justified > li:first-child > a {
      border-radius: 5px 5px 0 0;
    }
     .nav-justified > li:last-child > a {
      border-bottom: 0;
      border-radius: 0 0 5px 5px;
    }
    
    @media (min-width: 768px) {
     .nav-justified {
      max-height: 52px;
    }
     .nav-justified > li > a {
      border-left: 1px solid #fff;
      border-right: 1px solid #d5d5d5;
    }
     .nav-justified > li:first-child > a {
      border-left: 0;
      border-radius: 5px 0 0 5px;
    }
     .nav-justified > li:last-child > a {
      border-radius: 0 5px 5px 0;
      border-right: 0;
    }
    }
     @media (max-width: 768px) {
     .navbar {
      border-radius: 5px;
    }
    }
     @media screen and (max-width: 550px) {
     .navbar {
      border-radius: 5px;
    }
    }
    

    【讨论】:

      【解决方案3】:

      如果您想利用 Bootstrap 的预构建类,我相信这就是您的目标。

         <div class="row">
              <div class="col-lg-12">
                  <table id="navigation" class="table">
                       <td class="text-center"><a href="#">One</a></td>
                       <td class="text-center"><a href="#">Big</a></td>
                       <td class="text-center"><a href="#">Jump</a></td>
                  </table>
              </div>
          </div>
      

      【讨论】:

        【解决方案4】:

        你必须在 li 中使用“float:left”

        【讨论】:

        • 如果您要浮动
        • 的,您需要知道有多少个,以便您可以设置一个 % 宽度值以平均间隔它们。但要真正证明它们的合理性,您需要使用 元素预先应用的“table”、“table-row”和“table-cell”显示:值,或者按顺序将这些声明应用于子元素。
        猜你喜欢
        相关资源
        最近更新 更多
        热门标签