【问题标题】:Is it possible to set width of "navbar-fixed-top" using Bootstrap "col-" classes?是否可以使用引导程序“col-”类设置“navbar-fixed-top”的宽度?
【发布时间】:2020-04-09 00:15:36
【问题描述】:

我不使用引导程序“col-”类控制“navbar-fixed-top”的宽度。到目前为止,我发现的建议建议添加到 Bootstrap CSS 或使用“容器流体”,尽管我还没有弄清楚如何在后一个选项中使用“col--”来控制宽度。 p>

基本上,我想对以下脚本进行相同的操作,但将导航栏固定在顶部。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
  <div class="col-md-2"></div>
  <div class="col-md-8">

    <nav class="navbar navbar-inverse">
      <div class="navbar-header">
        <a class="navbar-brand" href="#">WebSiteName</a>
      </div>
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
        <li><a href="#">Page 3</a></li>
      </ul>
    </nav>

    <p>1. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
    <p>2. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
    <p>3. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
    <p>4. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>

  </div>
  <div class="col-md-2"></div>
</body>
</html>

我已经使用this W3Schools page on navbar-fixed-top 来测试上面的 HTML。你有什么工作想法吗?

【问题讨论】:

    标签: twitter-bootstrap navbar grid-layout


    【解决方案1】:

    您可以在 CSS 中设置固定导航栏的宽度,并使用自动边距将其居中。

    .navbar-fixed-top {
      width: 75%;
      margin-left: auto;
      margin-right: auto;
    }
    

    CODEPEN

    【讨论】:

      【解决方案2】:

      一旦您将position:fixed(如navbar-fixed-top)添加到任何元素,它就会从正常的页面流中删除,因此它在引导程序col-*s 中不会很好地工作。

      最好只设置宽度并将导航栏与页面内容分开..

      .navbar-fixed-top {
           width:66.66%; /* same as width of col-md-8 */
           margin:0 auto;
      }
      

      这是一个工作演示: http://codeply.com/go/ogg6K1uhGU

      您会看到使用了媒体查询,以便导航栏在较小的屏幕上保持响应。

      【讨论】:

      • 不幸的是,此修复程序不会在调整浏览器大小期间模仿引导网格的行为。
      【解决方案3】:

      到目前为止,我使用的解决方案是使用 Bootstrap CSS 中的 .container。请参阅下面的脚本。到目前为止,我注意到的唯一缺陷是在浏览器调整大小的各个阶段,导航栏和内容的宽度存在像素级差异。

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <title>Bootstrap Case</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      
        <!-- start of css overwriting container, add 'container-overwrite' to <nav class> !-->
        <style>
          @media (min-width: 768px) {
          .container-overwrite{
              width:96.3%;
          }}
      
          @media (min-width: 992px) {
          .container-overwrite{
              width:63.7%;
          }}
      
          @media (min-width: 1200px) {
          .container-overwrite{
              width:64.7%;
          }}
      
          body {
                padding-top: 70px;
          }
        </style>
        <!-- end of css !-->
      
      </head>
      <body>
      
        <div class="col-md-2"></div>
        <div class="col-md-8">
          <div class="container">
      
            <nav class="navbar navbar-inverse navbar-fixed-top container container-overwrite">
              <div class="navbar-header">
                <a class="navbar-brand" href="#">WebSiteName</a>
              </div>
              <ul class="nav navbar-nav">
                <li class="active"><a href="#">Home</a></li>
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">Page 3</a></li>
              </ul>
            </nav>
          </div>
      
          <p>1. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
          <p>2. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
          <p>3. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
          <p>4. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
        </div>
        <div class="col-md-2"></div>
      </body>
      </html>
      

      【讨论】:

        【解决方案4】:

        解决方案是在您的代码中添加 &lt;div class="container-fluid"&gt;,如下所示:

         <!DOCTYPE html>
            <html lang="en">
            <head>
              <title>Bootstrap Case</title>
              <meta charset="utf-8">
              <meta name="viewport" content="width=device-width, initial-scale=1">
              <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
              <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
            </head>
            <body>
              <div class="col-md-2"></div>
              <div class="col-md-8">
        
                <div class="navbar navbar-inverse">
                 <div class="container-fluid">
                  <div class="navbar-header">
                    <a class="navbar-brand" href="#">WebSiteName</a>
                  </div>
                  <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Home</a></li>
                    <li><a href="#">Page 1</a></li>
                    <li><a href="#">Page 2</a></li>
                    <li><a href="#">Page 3</a></li>
                  </ul>
                 </div>
                </div>
        
                <p>1. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
                <p>2. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
                <p>3. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
                <p>4. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll. A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.</p>
        
              </div>
              <div class="col-md-2"></div>
            </body>
            </html>
        

        【讨论】:

          猜你喜欢
          • 2013-07-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-27
          • 1970-01-01
          • 2016-09-02
          • 1970-01-01
          相关资源
          最近更新 更多