为什么我的导航栏以垂直方式添加菜单项?

时间:2018-01-14 13:33:52

标签: html css twitter-bootstrap bootstrap-4 nav

我正在学习使用Bootstrap,我制作了一个菜单,但是菜单项在右侧,垂直而不是左侧,水平添加。帮我识别我的错误。

Html文件:

    <html>
    <head>
        <title>Register</title>
        <link rel ="stylesheet" href="css/bootstrap.min.css"/>
        <link rel ="stylesheet" href="css/main.css"/>
        <meta name="viewport" content="width=device-width, initial-scale=1 user-scalable=no">
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
    </head>
    <body>
        <nav class="navbar navbar-default " id="navbar">
            <div class="container-fluid">
                <div class = "navbar-header">
                    <a href="#" class="navbar-brand" id="text">Brand</a>
                </div>
                <ul class = "nav navbar-nav" >
                    <li><a href="" id="text"> Home <span class="caret"></span></a></li>
                    <li><a href="" id="text">Team</a></li>
                <!--    <a href="" id="text">Gallery</a>
                    <a href="" id="text">Partners</a>
                    <a href="" id="text">News</a>
                    <a href="" id="text">Contact us</a>
                    <a href="" id="text">Register</a> -->
                </ul>
            </div>
        </nav>  

        <div></div>
    </body>
</html>

的main.css:

#navbar{
    /*back ground color*/
    background-color: #4311dd
}
/*change color to white*/
#text{
    color:#FFFFFF;
}

我看到的网页,

enter image description here

帮我找出错误的地方。

2 个答案:

答案 0 :(得分:1)

这是因为您遗漏了重要的组件,即设计为Bootstrap导航栏一部分的东西。重新包含这些组件,一切都会按预期显示。

Bootstrap 4首先设计为移动设备。

因此,如果您遗漏了navbar-toggler组件,那么您就可以看到所看到的内容,因为在移动设备上,导航是垂直堆叠的。

将此示例代码用作模板:

<nav class="navbar 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="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <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="#">Link</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link disabled" href="#">Disabled</a>
      </li>
    </ul>
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
      <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    </form>
  </div>
</nav>

P.S。最新版本的Bootstrap 4是 beta 3 。与beta 2相比,Beta 3确实引入了一些重大变化(尽管它们都没有影响导航栏组件)

答案 1 :(得分:-1)

您应该在navbar-expand-lg<nav> mr-auto添加ul课程,如下所示:

<nav class="navbar navbar-default navbar-expand-lg " id="navbar">
<div class="container-fluid">
    <div class="navbar-header">
        <a href="#" class="navbar-brand" id="text">Brand</a>
    </div>
    <ul class="nav navbar-nav mr-auto" >
        <li><a href="" id="text"> Home <span class="caret"></span></a></li>
        <li><a href="" id="text">Team</a></li>
    <!--    <a href="" id="text">Gallery</a>
        <a href="" id="text">Partners</a>
        <a href="" id="text">News</a>
        <a href="" id="text">Contact us</a>
        <a href="" id="text">Register</a> -->
    </ul>
</div>
</nav>