"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

Glitch found in Safari involving innerText of elements

Hey everyone, I posted this question not too long ago but now I have some images to share regarding the issue with Safari. When checking the console in Safari, the following text is displayed: <div id="rot3posDisp" class="rotDisp">C</div> Ho ...

Manipulating the content of a specific row's table cells with a text box in jQuery Datatables

I am working with a jQuery datatable that consists of only one column. Every time a row is selected, a panel opens with a text box that automatically populates with the name of the selected td. I am trying to figure out how to edit the name of the selected ...

Why isn't it possible to send POST data to a JSON file using JQuery/AJAX?

I'm currently learning how to use JQuery/Ajax from a helpful tutorial on YouTube. To watch the video, simply click here. While I can successfully retrieve data from the order.json file, I encounter an error whenever trying to send POST requests. Bel ...

Tips for successfully passing multiple properties to a function in React

<DeleteForeverIcon className={classes.deleteHwIcon} onClick={() => { deleteHomework(value.name, value.class); }} /> I'm looking to modify the function deleteHomework so that it can receive two properties instead of just one. In add ...

Best practice for including the language tag in Next.js Head tag

I'm struggling to find a clear example of how to use the language tag with Next Head. Here are two examples I have come across. Can you help me determine which one is correct, or if neither are appropriate? Just a reminder: it doesn't need to be ...

Is Material UI equipped to handle CSS Container Queries?

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries Recently introduced container queries for CSS are an exciting development. I'm curious if MUI 5.0 already supports them out of the box. It seems that the current SxProps do not in ...

What is the best method for enclosing all text within an HTML document with a different element?

For example: FROM <div> whats up! <div> whats up2! </div> thank you for your help <div></div> </div> TO <div> <span>whats up!</span> <div><span> whats up2! </span&g ...

Is there a method to verify the status of an onSnapshot listener?

There have been instances when I've observed that the link between the browser tab and Firestore gets disconnected for unknown reasons, leading to unsuccessful re-connection. Is there a reliable method to determine if your onSnapshot is still activel ...

Is there a method to make an <img> automatically adjust its width to fit that of its containing <div> even if the style/attributes are

If I'm unable to customize the style, tag, or attributes of an image element, is there a way to make its width adjust to fit a parent division or another container? This situation pertains to an email template, so external stylesheets, <style> ...

What could be the reason for the lack of styling on the select element when using PaperProps in the MenuProps of my Select component?

I've been struggling to remove the white padding in this select box for a while now. I've tried countless variations and places to tweak the CSS with no success. Any suggestions on how to make it disappear? The project is using MUI 5, and I even ...

How can I open a new window, redirect the current one, and bring focus to the new window using JavaScript?

Trying to troubleshoot a problem I'm having with the following setup: - Using SAP Portal, I am launching an HTML page containing this code. - The goal is for the HTML page to open a new window. - Once the new window opens, the original HTML page ...

Tips for creating space between a label and radio button in Bootstrap 4

I attempted to use this solution as suggested, but unfortunately, it did not yield the desired results. I seek advice on how to achieve the intended outcome. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="s ...

Is it possible to change the maximum column count in material-ui by transposing the grid?

Currently, I am working on setting up a grid layout of cards : https://codesandbox.io/s/u93zn In our scenario, we are facing an issue where the number of columns will always exceed the number of rows (and it surpasses the maximum limit that the grid can ...

Disappearing sticky-top navbar in Bootstrap when scrolling

I'm having trouble with the navbar on my website. Currently, it sticks to the top until I scroll out of the hero-image and then disappears. I would prefer the navbar to remain present throughout the entire page. Any suggestions on how to achieve this? ...

Notification window when the page is being loaded

My goal is to have a module or alert box display when my website is opened, and then disappear after 5 minutes. To see an example of what I'm looking for, take a look at this website: On that website, there is a facebook.com box that automatically d ...

Implementing tailwindcss styles in a typescript interface with reactjs

In my code, I have a file named types.ts that defines an interface called CustomCardProps. I pass this interface to the CustomCard component within the home.tsx file. Additionally, I have a folder named constant with an index.ts file where I store values o ...

What is the best way to initially hide a div using slide toggle and then reveal it upon clicking?

Is there a way to initially hide the div tag and then animate it in a slide toggle effect? I attempted using display:none on my '.accordion_box' followed by show() in slideToggle, but this results in adding the CSS property display: block. My goa ...

Mobile view causes malfunction in Bootstrap Toggle Burger Menu functionality

<nav class="navbar navbar-expand-lg navbar-dark px-2 " style="background-color:#E53935;"> <a class="navbar-brand" href="#">FoodFusion</a> <button class=&quo ...

How can you show images in Nextjs using a browser if they are not saved in the /public folder?

In my current project, I am utilizing Next.js as a full-stack framework with API route handlers in the backend. One issue I have encountered is related to storing images under the /uploads directory using the fs.writeFile method and only saving the path to ...

Scrolling with animation

While exploring the Snapwiz website, I came across a captivating scroll effect that I would love to implement on my own site. The background picture changes seamlessly as you scroll, with the front image sliding elegantly into view. A similar type of scro ...