Reactjs Bootstrap Navbar没有填充容器

时间:2018-02-15 15:36:55

标签: javascript twitter-bootstrap reactjs bootstrap-4

我正在使用bootstrap 4在Reactjs中创建一个简单的Navbar。 我目前有一个组件(Header.js),如:

import React from "react";

export class Header extends  React.Component {
    render (){
        return(
            <nav className="navbar navbar-light bg-light">
                <a className="navbar-brand" href="#">Navbar</a>
            </nav>
        );
    }
}

在后面的index.js中呈现,如下所示:

import React from "react";
import { render } from "react-dom";
import { Header } from "./components/Header";

import 'bootstrap/dist/css/bootstrap.css';

class App extends React.Component {
    render() {
        return (
            <div className="container">
                <div className="row">
                    <Header/>
                </div>
            </div>
        )
    }
}
render(<App/>, window.document.getElementById("root"));

以下是将标题添加到

的模板
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ReactJS Basics</title>
</head>
<body>
<div id="root"></div>
</body>
</html>

最终结果是渲染了一个Navbar,但它只有一个背景(Navbar)Output of above code,而不是我期望的整个容器着色。

2 个答案:

答案 0 :(得分:2)

问题是导航栏位于 .row 内。只需删除.row ...

即可

https://www.codeply.com/go/JyTwJ50OHY

<div class="container">
    <nav class="navbar navbar-light bg-light">
         <a class="navbar-brand" href="#">Navbar</a>
    </nav>
</div>

Bootstrap 4 .rowdisplay:flex,它仅指 包含网格列(col-*),但不包含导航栏,或其他组件。作为explained in the docs,你可以把...

容器内的导航栏 - 将导航栏包含在页面中心的容器宽度上。

- 或 -

导航栏内的容器 - 全角导航栏背景颜色,但包含导航栏组件到中心。

enter image description here

Bootstrap 4 Navbar & Containers

答案 1 :(得分:1)

您的容器需要内部导航栏,如下面的代码段所示。虽然在技术上可以将导航栏放入容器,但会产生您可能想要的外观/结果。

最重要的是:不应该为导航栏使用任何行

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
   <div class="container">
       <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>
   </div>
</nav>