Attempting to customize react-bootstrap styling within a NextJS application

I'm currently working on a project using Next.js and attempting to integrate Bootstrap as the CSS framework. After installing react-bootstrap into the project, I proceeded to create the navigation bar. My goal is to assign an '.active' class to one of the navigation links to visually indicate the active page. However, I am encountering difficulty when trying to override Bootstrap's styles with my own custom CSS.

In the _app.tsx file, I import the Bootstrap CSS like this:

import type { AppProps } from 'next/app'

import 'bootstrap/dist/css/bootstrap.min.css';

import Header from './layouts/Header';

function MyApp({ Component, pageProps }: AppProps) {

  return (
    <>
      <Header />
      <Component {...pageProps} />
    </>
  );

}
export default MyApp

The navigation component resides in a file named Header.js:

import Head from 'next/head';
import Image from 'next/image'
import Link from 'next/link';

import { Navbar, Nav, NavDropdown } from 'react-bootstrap';
import styles from '../../styles/Header.module.css';

import headerLogo from '../../public/img/header-logo.png';

const Header = () => {
    return (
        <div>
            <Head>
                <title>Test Project</title>
                <link rel='icon' href='/favicon.ico' />
            </Head>
            <Navbar collapseOnSelect expand="lg" bg="light" style={{ maxWidth: '1440px' }} >
                <Navbar.Brand href='/'>
                    <Image src={headerLogo} alt="logo" />
                </Navbar.Brand>
                <Navbar.Toggle aria-controls="responsive-nabar-nav" />
                <Navbar.Collapse id="responsive-navbar-nav">
                    <Nav className="me-auto">
                        <Link href="/"><Nav.Link>Home</Nav.Link></Link>
                        <Link href="/page1"><Nav.Link className={styles.active}>Active Page</Nav.Link></Link>
                        <Link href="/page2"><Nav.Link>Page 2</Nav.Link></Link>
                        <Link href="/page3"><Nav.Link>Page 3</Nav.Link></Link>
                        <Link href="/about"><Nav.Link>About Us</Nav.Link></Link>
                        <Link href="/contact"><Nav.Link>Contact Us</Nav.Link></Link>
                    </Nav>
                </Navbar.Collapse>
            </Navbar>
        </div >
    )
}

export default Header;

Here is the simple CSS code I'm using:

.active { color: red; }

Although I am importing the necessary Bootstrap components from react-bootstrap and constructing the navigation bar successfully, I encounter an issue while attempting to apply a class called '.active' from my CSS Module Header.module.css to the "Active Page" Nav.Link. Upon inspection using DevTools, I notice that Bootstrap's .nav-link property (color: rgba(0,0,0,.55)) overrides the custom style defined in my CSS module.

Regardless of the order of import statements, it seems that Bootstrap's CSS properties consistently take precedence over any modifications made in Header.module.css. Is there a correct way to import react-bootstrap? Are there specific considerations to be addressed since this is a Next.js project, not a standard React project?

Any assistance provided would be greatly appreciated,

Thank you

P.S. Here is a screenshot from DevTools for reference:

https://i.sstatic.net/ZsjNC.png

Answer ā„–1

For added emphasis, consider including !important after the color property like this -> color: #0d6efd !important;

Answer ā„–2

The rationale behind using bootstrap lies in its ability to provide a more specific selector. By incorporating classNames for multiple parent elements, you can enhance the specificity of your selector. Additionally, utilizing !import will also help in achieving this goal.

Update: In the screenshot provided, there is a rule for

.navbar-light .navbar-nav .nav-link
, indicating the need for creating a similar rule with your customized selector. To achieve this, it is recommended to switch to global scope as detailed in this resource: https://github.com/css-modules/css-modules#exceptions

An example rule in Header.module.css could appear as follows:

:global(.navbar-light) :global(.navbar-nav) :global(.nav-link).active {
  color: red;
}

Answer ā„–3

I resolved the issue by making these changes in my scss file and using the NextJS App router:

  1. Added a new app.scss file within the /app directory
  2. Inserted
    @import '~bootstrap/scss/bootstrap';
    into the /app/app.scss file
  3. Integrated import './app.scss' into the /app/layout.js file

Despite my efforts, I have yet to uncover the root cause of this problem.

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

Automatic scrolling feature in JavaFX

I am currently working on developing a chat box using JavaFX. My goal is to implement an auto-scroll feature that will scroll down the page when it gets filled up. However, I am facing some issues with this functionality. import javafx.application.Applica ...

Leveraging API data within React: A step-by-step guide

I have a React application where I am making API calls. To manage these calls, I created a separate file called dataService.js to contain all the functions for interacting with the API. However, when trying to log the data in my component, I am getting a ...

What is the best way to position my Jchartfx area graph below my gridview?

When my page loads, the graph appears like this. It consistently shows up in the top left corner even though it should be positioned underneath the grid view as intended. var chart1; function createGraph(mpy) { if (mpy == undefined) mpy = 12.00; ch ...

Why isn't my onScroll event triggering in my React.js application? What mistake am I making?

I am facing an issue with my onScroll event in react js. My goal is to implement infinite scrolling in react js, but unfortunately, the onScroll event is not triggering as expected. The process involves fetching posts from an API and passing them to the ...

A useful tip for jQuery users: learn how to prevent the context menu from appearing when a text box is

Is there a way to prevent the context menu from appearing in jQuery when the text box is disabled? The right-click disable functionality works well in all browsers except for Firefox. Please note that the right-click disable functionality works when the t ...

Selected Angular Radio Button

Back in the good ole days of basic HTML and CSS, I was able to achieve the following: input:checked+label { background-color: #f00; } <div class="col-xs-6"> <input type="radio" id="template-1" name="template" value="template1" checked> ...

Is it achievable to have a background image cover size with a responsive rollover effect?

Iā€™m currently facing a unique challenge where I want to have an image as the background of my website, with the size set to cover the entire screen. On this background image, there are elements like buildings that I want to interact with when hovered ove ...

Facing Issue with NextJS 13 App: Images Fail to Load When Accessed via GitHub Pages Route

Attempting to host a basic NextJS 13 website on GitHub pages has revealed some strange behavior that appears to only affect Safari users (both iOS and MacOS). Upon initial loading, the images appear correctly. However, as I navigate between routes, I enco ...

Step-by-step guide on implementing a border-bottom for an active link

Is there a way to apply a border-bottom to btn_articles and btn_posts when one of them is clicked? I attempted to use the active class but it did not have the desired effect. Any suggestions or solutions would be greatly appreciated. let btn_articles = ...

The placement of buttons in a responsive web design

Need help with adjusting button positions in mobile mode relative to text. Tried using different position settings like absolute and relative, but buttons are not staying aligned with the text on mobile devices. Can anyone assist? .button { display: b ...

Global Declaration of Third-Party Modules for Seamless Use without Imports in Webpack5 with TypeScript

Within my React project utilizing Webpack, I have opted to declare certain modules as global entities to eliminate the need for importing them every time they are needed. In my Webpack configuration file: plugins: [ new webpack.ProvidePlugin({ ...

Creating a square shape in Twitter Bootstrap to frame an item on a webpage

I've been working on creating a webpage that looks similar to the image provided. I've managed to get about 90% of it done, but there are a couple of issues I'm facing: How can I create a square with a triangle at the bottom as shown in th ...

What's the trick to inserting a "dot" beneath a DatePicker?

Could someone assist me in adding a small "dot" below certain dates on MUIX DatePicker, similar to the example shown here? Thank you. ...

Create a responsive layout with Bootstrap that features five columns of equal width starting from the "md" breakpoint

I've implemented this code multiple times in Bootstrap 4 where columns have full width on mobile and equal width from "md" breakpoint: <div class="container"> <div class="row"> <div class="col-12 col-md&qu ...

Integrating Next.js with Wordpress for Seamless Connectivity

Recently, I set up a brand new WordPress site on InMotion. My goal is to restrict WordPress to the backend for storing content and serving it through its API. I plan on using Next.JS/React for the frontend. However, I'm facing some uncertainty on how ...

Is it possible to customize the Angular Kendo Grid to be non-scrollable and automatically adjust its height to fit the row contents?

I'm trying to create a Kendo Grid in Angular that is Non-Scrollable and adjusts its height to fit the row contents. However, my current implementation is not working as expected, as it still gives me a fixed height. <kendo-grid [data]="propertyVie ...

Need jQuery solution for changing CSS in numerous locations upon hover

Currently, I am working on a WordPress website and I am trying to figure out how to change the CSS color of a side navigation element when a remote image is hovered over. In a typical scenario, I would accomplish this using CSS by assigning a hover class ...

The recharts error message displays: "There is no overload that matches this call."

I am currently working on developing a chart in react using the recharts library. To guide me, I am referencing an example provided in their documentation available at this link: https://codesandbox.io/s/zen-ellis-30cdb?file=/src/App.tsx While the project ...

Explore nested arrays and retrieve objects that have corresponding categories

Working with react+ nextJS and fetching static objects, specifically "posts". The aim is to develop a "related posts" feature for each post that retrieves three posts containing at least one of the categories. Below is an excerpt simplified for clarity: co ...

Break down the organization of CSS

While exploring a website, I came across this CSS code that sets the font family to "Open Sans," Helvetica, Arial, and sans-serif. However, I am having trouble understanding this structure. Any assistance would be greatly appreciated. Thank you in advanc ...