Having trouble with applying a custom class to React Bootstrap tabs?

When utilizing the react bootstrap tabs component, I encountered an issue with implementing a custom CSS style for the nav-link element within it using a customized parent class indicator.

<Tabs
       defaultActiveKey="signup_renter"
       id="uncontrolled-tab-example"
       className="mb-3 approval-details-tab"
     >
     <Tab eventKey="signup_renter" title="About Car">
        <div className="signup-renter">
            this is signup renter tab            
        </div>
     </Tab>
     <Tab eventKey="signup_host" title="Details">
         <div className="signup-host">
            this is signup host tab          
        </div>
     </Tab>
    </Tabs>

Below is the CSS code for the parent indicator:

.approval-details-tab > .nav-tabs .nav-link.active::before {
  content: "";
  background: #524eb7;
  width: 30px;
  height: 3px;
  position: absolute;
  top: 63% !important;
}

The problem arises when I use the .approval-details-tab as the parent class of nav-tabs, causing the custom styling to not work. However, the styling applies without using a parent class. Unfortunately, I require a parent class for distinctive design purposes.

Answer №1

According to the information provided in the React-bootstrap documentation:

The React-Bootstrap library does not rely on a specific version of Bootstrap and therefore does not come with any built-in CSS. However, some stylesheet is necessary to utilize these components.

It is left to your discretion how and which Bootstrap styles you decide to include, but the easiest approach is to incorporate the most up-to-date styles from the CDN.

Here is the link to the CDN: https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css

Insert the CDN link in the index.html file within the <head> tag as shown below:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3835352e292e283b2a1a6f746b7469">[email protected]</a>/dist/css/bootstrap.min.css"/>

To override any class from `react-bootstrap', you must apply "!important" in your custom CSS. If you wish to change the background color, add "!important" next to that particular CSS property.

For example:

.approval-details-tab{
   background: #524eb7 !important;
}

In order for us to provide more targeted assistance, kindly specify which CSS property you intend to modify within a bootstrap class. Thank you!

Answer №2

In React, understanding the parent-child relationship can be a bit complex. While a component in React may appear to be the direct child of another component, this is not always the case when translated to normal HTML. Consider the following code:

<div className="parent">
    <Tabs className="children"> 
       <!-- some other components inside -->
    </Tabs>
</div>

Is the `div` element the direct parent of the Tabs component? Not quite. When this code is rendered as normal HTML, it would look something like this:

<div className="parent">
   <div>
     <div className="children">
       <!-- some other components inside -->
     </div>
   </div>
</div>

Notice how the element with the className children is no longer a direct child of the parent component. This is why styling components based on parent-direct children relationship is not recommended in React.

If you are considering this approach for styling due to naming conflicts, you may want to explore using CSS Modules instead.

To further understand the behavior of the Tabs component, you can examine the source code.

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

There is an error in ReactJS: TypeError - _this.props.match is not defined

I am experiencing a TypeError in my console tab and I can't seem to figure out where the error is occurring in my source code. I am relatively new to ReactJS so any help in identifying what I'm doing wrong would be greatly appreciated. Thank you ...

Tips for effectively simulating the formik useFormikContext function while writing unit tests using jest

I've created a simple component (shown below) that aims to fetch data from the Formik FormContext using the useFormikContext hook. However, I'm facing some challenges when writing unit tests for this component. It requires me to mock the hook, w ...

What is the best way to position an image in the center over a video using HTML and CSS?

I am currently developing a website with a background video and I want to overlay a company logo on top of it while keeping it centered. My coding language of choice for this project is using HTML and CSS. Here's the HTML code snippet I have for this ...

Upgrade from Next.js version 12

Greetings to all! I have recently been assigned the task of migrating a project from next.js version 12 to the latest version. The changes in page routing and app routing are posing some challenges for me as I attempt to migrate the entire website. Is ther ...

What steps do I need to take in order to ensure that this React/Redux application renders accurately?

I have been struggling for the past hour and a half to understand why my locally hosted React and Redux app is not displaying. I have checked a few other React apps to confirm that they are functioning correctly, which they are. However, this is my first t ...

Various instances of the jQuery niceScroll plugin

I successfully set up jQuery niceScroll on the body, but now I want different themed scrollbars on one page. Below is my code snippet: Here is my fiddle: http://jsfiddle.net/JPA4R/135/ <script> $(document).ready(function () { $("body").niceScroll ...

Mastering the Art of Positioning Arrows in Post Car

I am currently facing an issue with my post carousel. The carousel has two arrows on the side, and I have designed a background in the shape of circles for these arrows. However, I can't seem to figure out how to center the arrows inside the circles. ...

Aligning a child div within a parent div using HTML/CSS where the child div takes up 30% of the parent div and

Can anyone help me figure out why these two INPUT elements aren't aligning next to each other? I appreciate any assistance you can offer! <STYLE> html, body { width: 100%; height: 100%; margin: 0px; padding: 0px; } </STYLE& ...

What strategies can be used to effectively structure CSS and JavaScript in a project for optimal organization?

In my NetBeans project, I currently have a large web project with CSS files included in the header. Some CSS codes are needed on all pages, while others are only necessary for specific pages. I am looking to optimize high-traffic pages by removing any ...

Methods to modify the state of a Modal component beyond the boundaries of a React class

I am attempting to trigger my modal by modifying the state from outside of the react class. Unfortunately, I have had no success thus far. I have experimented with the following approach: In my code, I have a method named "Portfolio" that is responsible f ...

How can I dismiss a popup confirmation in React?

The confirmation popup should close when clicking anywhere outside of it. However, the popup does not disappear as expected when the outside area is clicked. Additionally, I am getting the error message "Line 2:8: 'Backdrop' is defined but never ...

How can we implement :focus-within styling on Material-UI Select when the input is clicked?

I am currently implementing a Select component inside a div element: <div className="custom-filter custom-filter-data"> <DateRangeIcon className="search-icon"/> <FormControl variant='standard& ...

Placing the video at the center of the background image

                    Can someone assist me with a design issue I'm facing? I have two divs within a section. The left div contains a heading and a button, while the right div contains a video. However, when I try to add a background image to ...

What is the best way to implement lazy loading for headless UI Dialog components in a React

Is there a way to implement lazy loading for the headless ui Dialog component while preserving transitions? Below is the current implementation that almost works: // Modal.js const Modal = ({ isOpen }) => { return ( <Transition show={isOpen ...

Error in MUI: Unable to access undefined properties (reading 'drawer')

Recently, I encountered an unexpected error in my project using MUI v5.0.2. Everything was working fine just a week ago with no errors, but now I'm facing this issue without any changes made to the code: Error: TypeError: Cannot read properties of un ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

The styles from the Next.js module are being overridden by the styles from mui/material

Currently, I am working with next.js and using scss modules for styling. However, I have encountered an interesting issue while using mui/material components. During development (npm run dev), everything functions as expected. The problem arises when I swi ...

Setting a CSS grid column to have a minimum width of `max-content` and a specific

I am trying to create a CSS grid where the column widths adjust based on the content, but with a minimum width of 100px. I attempted using minmax(max-content, 100px), however it did not produce the desired result. In the example below, the narrow column sh ...

Generating alternate list items

I'm attempting to design a list style that resembles the one depicted in the image linked below: https://i.stack.imgur.com/U7vMw.jpg My issue is that when I try to add borders, they end up applying to the entire structure. .styled-list { list-st ...

I must create a dropdown and lift up feature similar to the one provided in the example

I am looking to implement a drop-down navigation bar on the top right and a pop-up button/links on the top left. The drop-down menu should appear when the screen size reaches a certain pixel width. If you can't see the drop-down navigation (NAV), adju ...