Tips for setting the style of child components in a ReactJS project with CSS modules, classNames, and JSX syntax

Currently in the process of setting up a project using React JS and I am interested in incorporating auth0 for authentication purposes, along with potentially integrating some serverless functions.

auth0 conveniently offers a pre-made "seed" project at https://github.com/auth0-samples/auth0-react-sample that requires minimal setup to get started.

The main container's code utilizes JSX and pulls in CSS styles from a module file. This module file contains the className classes necessary for styling.

I am augmenting the bootstrap layout with React-Bootstrap components and have successfully styled certain components using classNames syntax.

As an example, I am adjusting the background color of a Navbar like so:

Adding this to the css module file

/01-Login/src/views/Main/styles.module.css

.mainNavBar{
        background-color: rgb(45, 140, 210);                                                    
        border-color: rgb(45, 140, 210);                                                        
} 

Then within the JSX file

01-Login/src/views/Main/Container.js

<Navbar className={styles.mainNavBar} fixedTop>
...
</Navbar>

However, when attempting to style other inner elements, such as text color,

I tried the following:

.mainMenuItem { 
        color: #fff;
}

And also:

 <NavItem className={styles.mainMenuItem} eventKey={1} href="#">Link</NavItem>

Unfortunately, it did not produce the desired result. It appears that I may be struggling to target the children of NavItem.

I experimented with variations like:

.mainMenuItem ul > li > a { 
        color: #fff;
} 

But it seems to be ineffective and doesn't apply the styles correctly (although an 'li' item seemed to inherit the color upon inspection through the browser, but not the 'a' ...).

As a last resort, I attempted inline styles (referenced a related question here Add inline style in reactjs without using JSX) but encountered similar issues with inheritance.

If you have any guidance or pointers on the correct approach, I would greatly appreciate it as I find myself entangled in confusion.

Edit 19/07/2016

It appears that NavItem is not responsive to the style changes...

I have raised an issue with react-bootstrap: https://github.com/react-bootstrap/react-bootstrap/issues/2070

Answer №1

Instead of using the standard CSS through the "Classname" attribute, I opted for inline styles written with JavaScript syntax utilizing Radium.

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

How can I convert the stylesheet link from fonts.google.com into a CSS class and include it in my file.css as an internal style instead of an external one?

Looking to incorporate a font from fonts.google.com? Here's how you can do it: <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@200;300 ...

modify the color of text in a row within a jquery ajax table

Is it possible to change the font color of values in a row based on a condition inside a function? Specifically, if the TotalStudent count exceeds the room capacity, can we add student information to the table with red font color? Below is my attempt using ...

Animated Gradient Header with CSS

I've been attempting to add an animated gradient to my header class, but so far I haven't been successful. I want the gradient to apply only to the header, not the body. Here's the link I'm using for reference: Can anyone offer some i ...

Positioning elements next to each other in jQuery on mouse over - while also addressing scrolling issues in a div

After tinkering with this interesting concept of mouseover combined with absolute positioning divs on a jsFiddle, I encountered some unexpected results. The code was inspired by a stackoverflow thread on positioning one element relative to another using j ...

Swap out a component for another using a higher order component (HOC perhaps?)

I have noticed that certain libraries like "framer-motion" in react utilize this syntax, for instance to include an animated H1: <motion.h1> where the h1 tag is enhanced with animations specified in the component's props. What confuses me is ho ...

Placing a div with absolute positioning inside another div with absolute positioning raises the question of how it functions when the outer div is not set to position: relative

Trying to grasp the concept, I experimented with the following setup: <div id="Outer"> <div id="Inner"></div> </div> In this scenario, the outer div has a relative position and the inner div has an absolute position. This allo ...

In the Vercel production environment, when building Next.js getStaticPaths with URL parameters, the slashes are represented as %

I've encountered an issue while implementing a nextjs dynamic route for my static documentation page. Everything works perfectly in my local environment, and the code compiles successfully. However, when I try to access the production URL, it doesn&ap ...

An error has occured with Redux showing "Cannot read properties of undefined (reading 'items')" for the export constant selectItems, which is defined as (state) => state.basket.items

I am encountering an issue with implementing the add to basket functionality in my e-commerce website built with Next.js using Redux. The error message "Cannot read properties of undefined (reading 'items'): export const selectItems = (state) => ...

Is it possible to incorporate Google icons within Freemarker?

https://i.stack.imgur.com/Pv2T4.pngI'm having trouble using Google icons in my project. Can anyone help me figure out how to fix this? UPDATE: Here is a snippet of my HTML template: <?xml version="1.0" encoding="UTF-8"?> < ...

Preventing a user from accessing the login page if they are already logged in using Reactjs

I need assistance with implementing a "Login Logout" module in Reactjs using the nextjs framework. My goal is to redirect users to the "dashboard" page if they are logged in (email set in cookie). However, I am encountering an error with the following co ...

Setting up initial values for a React hook form with the help of Redux state

Struggling to set a default value for a React hook form using a Redux object in my project. I am working with react-hook-form, Redux toolkit, and materialui. The issue seems to be related to the TextField component of materialui. Troubleshooting const dis ...

WordPress CSS Styling Issue: HTML Element Display Problem

I recently converted my static HTML site to WordPress, but I'm facing an issue where the site is always aligned to the left. Despite trying different solutions like the one below: body{ margin: auto; } The original HTML page was centered perfectly ...

"Exploring the versatility of Material UI's dropdown feature with multiple columns in React JS

Is there a way to implement a Material UI dropdown box like the one in this image using only Material UI React JS (JavaScript)? What is the best approach to utilize the backend for displaying data from both the first and second columns? Requirements: Use ...

When trying to implement formik-material-ui for material-ui styling, validation needs to happen before onSubmit

Looking to incorporate material-ui into my formik app by utilizing the formik-material-ui library. However, I am facing an issue where validation is triggering before submission when using material-ui textfield components within the fields. I suspect it ma ...

What is the process for encoding images in HTML code?

Is there a method to embed a background image directly in the HTML code? For instance: background-image: url(data:image/gif;base64,XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX); If I have an image file, can I encode its data and t ...

The React snackbar is mysteriously peeking out from behind the popup

While using the react-notifications-component snack bar, I encountered an issue where my snack bar was appearing behind the pop-up. Is there a way to fix this with z-index? I tried using <ReactNotification style={{ zIndex: 10000 }}/>, but it didn&ap ...

How can I use CSS to conceal the controls for an HTML5 video?

Seeking advice on how to hide and show controls on an HTML5 video section. I currently have a setup where clicking on the video div opens a modal that plays the video in a larger size. The code for opening the modal and playing the video is working fine. ...

Utilizing a forwardRef component in TypeScript to handle child elements

Working with @types/react version 16.8.2 and TypeScript version 3.3.1. This forward refs example was taken directly from the React documentation with added type parameters: const FancyButton = React.forwardRef<HTMLButtonElement>((props, ref) => ...

Exploring how to use React with a select component featuring objects

Hello, I am new to working with React and I have a question regarding the select component of Material UI. Here's my situation: I am creating functionality for creating and editing a User object. This User object has a primary key and some data, incl ...

Creating additional pages

Recently starting with Next js, I decided to create a new page. First step was creating a folder named pages inside of src/app. Inside this folder, I added a file called dashboard.tsx. However, when attempting to access the page via http://localhost:3000/d ...