Bootstrap 4导航栏项的自定义对齐

时间:2019-01-09 10:23:04

标签: html css twitter-bootstrap

我正在使用Bootstrap 4.1,目前根本不熟悉Bootstrap。很抱歉,如果这个问题是重复的,但找不到适合我的答案(也对MS Paint图像感到抱歉)。因此,我必须做一个定制的导航栏,而我被卡住了。 The navbar links, when expanded should look like these ones。收起时,我必须使用此button, aligned right。最后,当单击按钮时,元素必须居中-collapsed navbar。预先感谢!

2 个答案:

答案 0 :(得分:0)

尝试一下

 <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
    </head>
    <body>
    <style> 
    @media only screen and (max-width: 768px) {
    .navbar-nav li a {
    text-align:center;
    }
    }

    </style>
    <nav class="navbar navbar-expand-md bg-dark navbar-dark">
      <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="collapsibleNavbar">
        <ul class="navbar-nav ml-auto">
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="#">Link</a>
          </li>    
        </ul>
      </div>  
    </nav>
    <br>

    <div class="container">
      <h3>Collapsible Navbar</h3>
      <p>In this example, the navigation bar is hidden on small screens and replaced by a button in the top right corner (try to re-size this window).</p>
      <p>Only when the button is clicked, the navigation bar will be displayed.</p>
      <p>Tip: You can also remove the .navbar-expand-md class to ALWAYS hide navbar links and display the toggler button.</p>
    </div>

    </body>
    </html>

答案 1 :(得分:0)

只需使用Bootstrap的导航组件:

   <nav class="navbar justify-content-end navbar-expand-lg navbar-light bg-light">
     <a class="navbar-brand" href="#">Navbar</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
      </button>
       <div class="collapse navbar-collapse " id="navbarNav">
        <ul class="navbar-nav  ml-auto" style="text-align: center;">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
        </li>
      </ul>
    </div>
  </nav>

在以下截图中,导航栏看起来像您的导航栏:http://next.plnkr.co/edit/iZ1TUqZGqnzf74BG?open=lib%2Findex.html&preview

希望有帮助!