"Need help with resolving issues in my React Bootstrap Navbar? Learn the latest updates on fixing Navbar Brand Collapse here

Having issues with my React.js website navbar created using bootstrap. The menu button doesn't show up when the navbar collapses at a certain point. Can someone spot what's missing in the code below?

Tried toggling by following a tutorial, but it doesn't work. Seems like something is off. Any ideas on how to fix this?

EDIT: Fixed the original issue, but now facing alignment problems. Updated the code below accordingly.

  1. In desktop view, I want everything aligned right on the navbar except for the logo which should stay in place.

  2. In tablet view, the navbar always appears and I don't want that. How can I change this behavior?

  3. In mobile view, need the logo to remain static while aligning the button vertically centered and to the right side.

Appreciate any help!

Navbar.js:

import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import Button from 'react-bootstrap/Button';
import {
  Link
} from 'react-router-dom';

class NavBar extends React.Component {
  render() {
    return (
      <Navbar collapseOnSelect expand="lg" className="custom-nav-bg fixed-top">
        <Navbar.Brand href="#home">
        <Link to="/home"><img className="custom-nav-logo"
            src="logohero.png"
            alt="StatHero Logo"
            width="105px"
            height="50px"
          /></Link>
        </Navbar.Brand>
        <Navbar.Toggle aria-controls="responsive-navbar-nav" className="navbar-toggler-css" />
        <Navbar.Collapse id="responsive-navbar-nav">
          <Nav className="align-nav">
            <Nav.Link className="custom-nav-text" href="#about">
            <Link to="/about">ABOUT</Link>
            </Nav.Link>

            <Nav.Link className="custom-nav-text" href="#stats">
            <Link to="/stats">STATS</Link>
            </Nav.Link>

            <Nav.Link className="custom-nav-text" href="#faqs">
            <Link to="/faq">FAQS</Link>
            </Nav.Link>

            <Nav.Link className="custom-nav-text" href="#contact">
            <Link to="/contact">CONTACT</Link>
            </Nav.Link>

            <Nav.Link className="custom-nav-text" href="#signup">
              <Link to="/signup"><Button className="custom-nav-button">SIGN UP</Button></Link>
            </Nav.Link>

            <Nav.Link className="custom-nav-text" href="#login">
            <Link to="/login">LOGIN</Link>
            </Nav.Link>

          </Nav>
        </Navbar.Collapse>
    </Navbar>
    )
  }
}
export default NavBar;

Navbar CSS (For context):

  background-color: red ! important;
  display: flex;
  align-items: flex-end;
}

.align-nav {
  display: flex;
  align-content: flex-end;
}

.custom-nav-bg {
  max-width: 100%;
  padding-right: 50px;
  padding-left: 60px;

  border-radius: 0px;
  background-color: #132A42;
}

.custom-nav-logo {
  height: 100%;
}

a.custom-nav-text:hover {
  color: #00DF8D ! important;

  font-family: 'Assistant', sans-serif ! important;
}

a.custom-nav-button:hover {
  color: #00DF8D ! important;

  font-family: 'Assistant', sans-serif ! important;
}

.custom-nav-text {
  max-width: 100%;
  padding-top:10px ! important;
  padding-right:20px ! important;
  padding-bottom:10px ! important;
  padding-left:20px ! important;

  color: white ! important;

  font-family: 'Assistant', sans-serif;
  font-size: 20px;
  font-weight: 600;
}

.custom-nav-button {
  padding-right: 15px;
  padding-left: 15px;

  border-color: #00DF8D;
  border-radius: 10px;
  background-color: #00DF8D;

  font-size: 20px;
  font-weight: 600;
  }

@media only screen and (max-width: 768px) {
  .custom-nav-text {
    max-width: 100%;
    padding-top:10px ! important;
    padding-right:10px ! important;
    padding-bottom:10px ! important;
    padding-left:10px ! important;

    font-size: 15px;
  }
  .custom-nav-logo {
    align-items: flex-start;
    justify-content: flex-start;
  }
  .custom-nav-bg {
    padding-left: 0px;
    padding-right: 0px;
    display: block;
  }
}

@media only screen and (max-width: 768px) {
  .custom-nav-button {
    padding-right: 10px;
    padding-left: 10px;

    border-color: #00DF8D;
    border-radius: 5px;
    background-color: #00DF8D;

    font-size: 15px;
    }
  }

Answer №1

This is an illustration using React-Boostrap

import React from "react";
import{ Navbar, Nav, Button, NavDropdown}  from "react-bootstrap";

class NavigationBar extends React.Component {
  render() {
    return (
<Navbar collapseOnSelect expand="lg" bg="dark" variant="dark">
  <Navbar.Brand href="#home">React-Bootstrap</Navbar.Brand>
  <Navbar.Toggle aria-controls="responsive-navbar-nav" />
  <Navbar.Collapse id="responsive-navbar-nav">
    <Nav className="mr-auto">
      <Nav.Link href="#features">Features</Nav.Link>
      <Nav.Link href="#pricing">Pricing</Nav.Link>
      <NavDropdown title="Dropdown" id="collasible-nav-dropdown">
        <NavDropdown.Item href="#action/3.1">Action</NavDropdown.Item>
        <NavDropdown.Item href="#action/3.2">Another action</NavDropdown.Item>
        <NavDropdown.Item href="#action/3.3">Something</NavDropdown.Item>
        <NavDropdown.Divider />
        <NavDropdown.Item href="#action/3.4">Separated link</NavDropdown.Item>
      </NavDropdown>
    </Nav>
    <Nav>
      <Nav.Link href="#deets">More details</Nav.Link>
      <Nav.Link eventKey={2} href="#memes">
        Dank memes
      </Nav.Link>
    </Nav>
  </Navbar.Collapse>
</Navbar>
    );
  }
}
export default NavigationBar;

Also, refer to the documentation of react-bootstrap for responsive features

I suggest aligning elements with bootstrap classes for consistency, without directly modifying the CSS (although possible, try to utilize their alignments). Consult the documentation on how to position elements effectively.

Answer №2

Being a newcomer to the online world, I've come up with something similar for the desktop version

import {Navbar,Nav} from 'react-bootstrap';
import '../styless/topBar.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import logo from '../assets/logo.svg';



const  Header = () => {
    return(
<>
<Navbar  collapseOnSelect  variant='dark' expand="lg" >
<Navbar.Brand className='logo-app' >
<div class="container-fluid">
    <img
      src={logo}
      width='30'
      height='30'
      className='d-inline-block align-top'
      alt='logo'
    />
    </div>
  </Navbar.Brand>
  <Navbar.Brand className='brand ' href='/'>
    Header
  </Navbar.Brand>
  <Navbar.Toggle aria-controls="responsive-navbar-nav" className="navbar-toggler-css"/>
  <Navbar.Collapse id="responsive-navbar-nav" className = "justify-content-end">
   
    <Nav >
      
      <Nav.Link href='/'>Home</Nav.Link>
      <Nav.Link href='/articles'>Article</Nav.Link>
      <Nav.Link  href ='/about'>About Us</Nav.Link>
     
    <span >
      <a className="btn btn_nav btn-dark " href="/login">login</a>
      </span>
    </Nav>
</Navbar.Collapse>
</Navbar>
</>
 )}
    export default Header;
'''

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieve the contents of a script using JavaScript

I have a script on my webpage that looks like this: <script> window.__INITIAL_STATE__ = {"meReducer":{"me":{"id":1234,"groupId":789,"},},//more code removed}; </script> I am looking to trigger a ...

Using HTML5 datetime-local to only accept dates in the format Y-m-d

I am currently working on a form that includes a datetime-local input field. <input type="datetime-local" id="datetime" /> After clicking on the today button, the date is automatically set to today's date like this: 2018-11-05 --:-- However ...

Error message: "Reactjs - TypeError: The property 'map' cannot be read as it is undefined in the table"

I have encountered an issue while using the material-ui table. I am able to map the items and display them successfully, but when trying to map the orders.items in the table, I get the following error: TypeError: Cannot read property 'map' of u ...

The asynchronous ajax request is leading to a browser freeze

In the HTML page, I have two sets of a and p elements that are initially set to display:none. At the bottom of the page, there is a function being called with their respective ID's and values, which will enable one of them based on certain conditions ...

The issue with Thymeleaf recursive function not functioning as expected

I am attempting to create a recursive list using Thymeleaf. I have a simple Java object that represents a node with a description and an array list of child nodes. However, my current HTML/Thymeleaf structure is not properly iterating through the nested le ...

The getJSON API functions properly on a local machine but encounters issues when deployed on a

I have a vision to create a web application that can display the weather of any city based on user input. I've divided my project into three files - index.html for collecting user input, index2.html for retrieving and displaying the data, and a CSS fi ...

best practices for choosing items from a dropdown menu using Angular

I have a dropdown list displaying existing tags/chips created by users in the past. However, I'm having an issue where when I select a tag from the dropdown list, it doesn't show up in my input field (similar to the Chart tag currently). I am abl ...

What is causing the Bootstrap accordion to not display content when clicked on?

As a beginner in web development, I am currently working on my very first website. One issue I'm facing is with incorporating an accordion-style card in my webpage. Although it allows me to click on different titles, the collapsed ones fail to expand ...

"Implement a smooth scrolling animation on the page to automatically move a div element upward and lock

I am attempting to create a unique animation for a header div that involves moving it to the top of the screen when the user scrolls down. Once the header div reaches the top, I want it to stay fixed in that position. As the header div moves upwards, it wi ...

Make the text stand out by highlighting it within a div using a striking blue

Currently, I am working with Angular2 and have incorporated a div element to display multiple lines of text. Positioned below the text is a button that, when clicked, should select the entirety of the text within the div (similar to selecting text manually ...

I'm looking for assistance in changing my mascot image to a pixelated image when I hover over it. Can anyone provide guidance on how to achieve this?

Seeking assistance on how to change my mascot image to a pixelated image when hovered over. Any tips or advice would be appreciated! Here is the CSS I'm currently using: .mascot { display: block; margin-left: auto; margin-right: auto; ...

Looping through PHP to set the background color of a map

A basic code I have writes out a 'map' or grid. My goal is to define specific areas - for example, if the value for $i or $j equals 1, it should mark the edge of my 'map' by setting the cell's class to 'edge', which curre ...

When using HLS with Media Source Extensions on mobile devices, the <video> element with the muted and autoplay attributes may freeze on the first frame

I recently added a background video to our website using mux.com. The video is set to autoplay with HLS, but Chrome requires Media Source Extensions for playback. To ensure the HTML5 video auto plays, I included the necessary mute parameters as well. How ...

Material-UI Grid in Full Width Mode is malfunctioning

I've been delving into the Material-UI@next grid layout system to grasp its intricacies. What I'm aiming for is to have two papers that stretch across the entire width of the screen and collapse into one paper when the screen size shrinks. The d ...

Setting the userAgent correctly for server-side rendering with Material-UI

Getting an unexpected warning message: Material-UI: userAgent should be provided in the muiTheme context for server-side rendering In the current server side rendering setup, what could be amiss : match({routes: R(), location}, (error, redi ...

CSS problem: alignment issue between text and checkbox

I'm experiencing a CSS style issue where the text box and text are not aligned properly. Can someone assist me in fixing this problem? Below is the code I am using: Thank you for your help. <div> <input type="checkbox" ...

Step-by-step guide on updating the content of a hidden text input box upon selecting a different option from a menu

When a user selects an option from the drop-down menu, I want to automatically change the existing value in a hidden text input box to an empty string ('') when the box is closed. However, the value does not change until the button is clicked a s ...

Why does the getComputedStyle function return null for IE11 and Edge when using Kendo React Grid within a Popout window, while it works fine in Chrome, Opera, and Firefox?

Check out my stackblitz demo where I am experimenting with rendering a Kendo React Grid inside a popout window using the react-popout-component. The demo runs smoothly in Chrome, Opera, and Firefox, but encounters issues in Edge and IE11 due to a null valu ...

What are the steps to create a "load more" button that displays additional rows?

Struggling to get the code right for my webpage. I have a layout with 6 rows, each containing 3 columns filled with images. However, when I click on the "load more" button, nothing happens. I've attempted to modify the jQuery code from .slice(0, 3) t ...

reactjs function continually returning undefined

My approach is rather simple. It involves iterating through an array of objects and retrieving the object with the specified id in the parameter. Here's how it looks: returnValueToDelete(id) { this.state.rows.map(function(value) { if (value ...