user5948213
user5948213

Reputation:

Reactjs Bootstrap Navbar not filling container

I am creating a simple a Navbar in Reactjs with bootstrap 4. I currently have a component (Header.js) like so:

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>
        );
    }
}

which is rendered in the later index.js like so:

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"));

Here is the template the compenent is added to

<!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>

The end result is a Navbar is rendered but it only has a background under the words (Navbar)Output of above code instead of what I was expecting with the entire container coloured.

Upvotes: 2

Views: 6151

Answers (2)

Carol Skelly
Carol Skelly

Reputation: 362430

The problem is that the Navbar is inside the .row. Just remove the .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>

The Bootstrap 4 .row is display:flex, and it's meant only to contain grid columns (col-*), but not the navbar, or other components. As explained in the docs, you can put the...

navbar inside container - to contain the navbar to the width of the container which is centered on the page.

--OR--

container inside navbar - full-width navbar background color, but contains the navbar components to the center.

enter image description here

Bootstrap 4 Navbar & Containers

Upvotes: 1

WebDevBooster
WebDevBooster

Reputation: 14954

Your container needs to go inside the navbar like shown in the code snippet below. While putting your navbar into a container is technically possible, it's not gonna produce the look/result you are likely to ever want.

Most importantly: You shouldn't use any rows for your navbar.

<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>

Upvotes: 0

Related Questions