Tips on updating the highlighted color of the item currently selected in NavBarDropdown using react-bootstrap

I've been utilizing the react-bootstrap NavDropDown component from this source. Although I initially set it up correctly, I'm struggling to customize the background color of the navdropdow-items when they are in their normal or active (selected) state.

For instance, I want to switch from using blue to a different color when an option is selected, similar to what's depicted in this image.

Is there a way for me to achieve this? Perhaps through CSS if there isn't an existing class or attribute in bootstrap for it.

Navbar.js

      <Navbar bg="dark" variant="dark">
        <Container>
          <Navbar.Brand href="/master">Hi {user.sub}</Navbar.Brand>
          <Nav className="me-auto">
            
            .
            .
            .
            .
            .
            .

            <NavDropdown drop="down-centered" menuVariant="dark"  title="3P-Verticals" id="navbarScrollingDropdown">
              {
                verticals_3p.map((item)=> {
                  return(
                    <NavDropdown.Item key={item.val}>
                      <Nav.Link onClick={()=>props.setTenantName(item.val)}>{item.label}</Nav.Link>
                    </NavDropdown.Item>
                  )
                })
              }  
            </NavDropdown>
            .
            .
            .
            .
            .
            .
          </Nav>
        </Container>
      </Navbar>

I attempted to address this in my App.css, but unfortunately, it's not yielding the desired results.

#navbarScrollingDropdown:active {
  font-weight: bold;
}

Answer №1

The element <NavDropdown> is identified by the ID navbarScrollingDropdown, while the <NavDropdown.Item> becomes active when selected. Due to how react-bootstrap renders the DOM, the resulting HTML structure is as follows:

<div menuvariant="dark" class="nav-item show dropdown">
  <a aria-haspopup="true" aria-expanded="true" id="navbarScrollingDropdown" href="#" class="dropdown-toggle nav-link" role="button">3P-Verticals</a>
  <div aria-labelledby="navbarScrollingDropdown" class="dropdown-menu show" style="margin: 0px;">
    <a href="#action/3.1" class="dropdown-item">Action</a>
    <a href="#action/3.2" class="dropdown-item">Another action</a>
    <a href="#action/3.3" class="dropdown-item active">Something</a>
    <div class="dropdown-divider" role="separator"></div>
    <a href="#action/3.4" class="dropdown-item">Separated link</a>
  </div>
</div>

Since the popup menu div is located outside of the menu item element with the ID navbarScrollingDropdown, using the CSS selector #navbarScrollingDropdown .active will not work.

To address this, you can override the CSS selector used by Bootstrap:

.dropdown-item.active,
.dropdown-item:active {
  background-color: green !important;
}

For a working demonstration, check out this CodeSandbox demo.

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

jQuery code unable to alter the class of a div

Need help with a script on my single page site that adds a "read more" style link to a content area's div. The goal is for the button to change the class of the content area when clicked, showing all of the content. Clicking again should hide the cont ...

Tips for creating a website with an integrated, easy-to-use editing tool

Recently, I designed a website for a friend who isn't well-versed in HTML/CSS/JavaScript. He specifically requested that the site be custom made instead of using a web creator. Now, he needs to have the ability to make changes to the site without nee ...

The initial render in a Kanban board seems to be causing issues with the functionality of react-beautiful-dnd

I recently integrated a Kanban board into my Next.js and TypeScript project. While everything seemed to be working fine, I encountered a minor glitch during the initial rendering. Interestingly, when I refreshed the page, the drag and drop functionality st ...

Utilize Flexbox to arrange elements in a column direction without relying on the position property in CSS

Flexbox - align element to the right using flex-direction: column; without relying on position in CSS Hello everyone, I am looking to move my row element to the right without using positioning. This code includes flex-direction: column; and I do not have ...

Can anyone suggest a solution to troubleshoot this issue with CSS Flexbox and absolute positioning?

I'm currently developing a React application featuring flex container cards (referred to as .FilmCard with movie poster backgrounds) within another flex container with flex-wrap. Each card has an item positioned absolutely (an FontAwesome arrow icon). ...

Develop a website using HTML and CSS for the front-end design, complemented by Java for the

I find myself at a standstill with a project I want to tackle, but unfortunately, my knowledge of modern web development, particularly full stack, is minimal. As a result, I am completely clueless on how to proceed. Here is what I am familiar with and what ...

How to utilize a defined Bootstrap Modal variable within a Vue 3 single file component

I'm diving into the world of TypeScript and Vue 3 JS. I created a single-file-component and now I'm trying to implement a Bootstrap 5 modal. However, my VSCode is showing an error related to the declared variable type. The error message reads: ...

Seeking a solution to the useRef problem. Encountering difficulties with React Hook useRef functionality within a NextJS application

Whenever I refresh the page, the 'Ref' value is displayed as null. This causes the if condition blocks not to work. I attempted to modify the useRef values but could only set it to null. When I console log the myDivRef.current, it returns "Ref: ...

What is a more effective way to showcase tab content than using an iframe?

I currently have a webpage set up with three tabs and an iframe that displays the content of the tab clicked. The source code for each tab's content is stored in separate HTML and CSS files. However, I've noticed that when a tab is clicked, the ...

Creating a sidebar that remains fixed in place even as the page is scrolled down can be achieved by using CSS and positioning properties

I am looking to create a website with a specific layout design as shown in this image: https://i.stack.imgur.com/ndKcz.png The main focus is on making the sidebar (F/T container) function as a social network link, sticking to the right side of the page ev ...

Assigning the style of the parent element based on the child element

Currently, I am attempting to develop a customized dropdown field and here is the code I have come up with: const list = document.querySelector('.list'); const listItems = list.getElementsByTagName('p'); document.querySelector('# ...

Imagine a webpage with a width of 1000 pixels. Can you determine the precise horizontal position (from the left) of the div element with the unique id of "inner_div"?

Hello everyone, I hope you're doing well. I recently took a test and unfortunately got this question wrong. Imagine a web page with a width of 1000px. What is the exact horizontal (from the left) position of the div element with id "inner_div"? < ...

Performing multiple requests using Axios library in a React application

My goal is to make 2 requests and define variables using this.setState({}) for future modifications. This is the code snippet I have: class App extends React.Component { constructor() { super(); this.state = {user: false, repository :false} ...

Ways to assign or remove value from material-ui's Input component in ReactJS

I'm having trouble clearing the value of a material-ui Input using refs instead of state. I've experimented with both types of refs that I am aware of: ref={this.input} - and - ref={el => (this.input = el)} however, neither method seems to ...

Using CSS to align and place elements

Currently facing a challenge with my HTML/CSS code regarding the horizontal centering of two divs (A, B) within one absolutely positioned div (C) that contains background images. Key aspects to consider: The bottom edge of the larger div (A) should alig ...

What is the reason behind the code breaking when a newline is added between `return` and `(`?

After investigating my query, it seems that a peculiar issue arises in the code where setState fires and render method gets hit, but nothing rerenders The code functions correctly when there is no newline between the return statement and opening parenthes ...

Ways to stop a form input value from disappearing when clicking on other fields?

Upon loading the page, the form's campaignid input value is automatically filled. However, clicking on other fields causes it to reset to default. Is there a way to prevent this default behavior and preserve the input value of campaignid? I've ...

Tips on placing a button at the bottom of the AppBar and keeping it fully responsive

I am attempting to place my login and log out buttons at the end of the AppBar. When I try forcing it with marginLeft: '70%',, it works but only on large resolutions. On smaller resolutions, the buttons go out of place. Here is an example of forc ...

What is the best way to make sure images and text are properly aligned for different screen sizes

I'm a beginner with Bootstrap and could use some assistance. In my code, I have text and images divided into four columns with alignment set based on screen size using media queries in CSS. However, when resizing the window to tablet view (check Scre ...

Unable to utilize the namespace 'RouteComponentProps' as a specified type

When using TypeScript to define an interface that extends RouteComponentProp, I encountered some issues: VSCode error: [ts] Cannot use namespace "RouteComponentProps" as a type. Console error: Cannot use namespace 'RouteComponentProps' as a typ ...