Is it possible to supersede CSS styling using a class name in React?

My <li> tag in my .css file has the following style:

li {
 padding: 15px;
 background-color: #32383d;
 transition: 0.4s;
 border-bottom: 1.5px solid #464e53;
 color: #6b6d6d;
 font-family: "Poppins", Arial, sans-serif;
 cursor: pointer;
}

The issue arises when I try to add an active class to the currently selected <li> using the className attribute:

filteredItem[0].items.forEach((menuItem, index) => {
    menuItems.push(<li key={index} className={`${index === currentPage ? 'active' : ''}`}><span
        className={menuItem.css}/>{menuItem.value}</li>)
});

This is the style for the active class (located in the same .css file):

.active {
 background-color: #2f89fc;
}

However, the new background-color doesn't override the original one. Any advice on how to resolve this issue?

NOTE: The currentPage variable is functioning properly. (verified by removing background-color from li)

Answer №1

Enhance its specificity:

li.active {
  background-color: #2f89fc;
}

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

children blocking clicks on their parents' divs

I previously asked a question about a parent div not responding to click events because its children were blocking it. Unfortunately, I couldn't recreate the issue without sharing a lot of code, which I didn't want to do. However, since I am stil ...

What CSS attribute determines the distinction in visual presentation between two elements that share the same CSS configurations?

In the HTML code below, there is a button and a div with the same class and content: <div class="root"><!-- --><button class="outer"><div class="middle"><div class="inner">label</div></div></button><!-- - ...

Is there a way to modify the color of a specific section of a font icon?

Currently, I am in the process of implementing an upvote button using fa fa signs. However, I have encountered an issue where the background color of the up vote button is bleeding outside of the sign (possibly due to padding on the icon), and I am strivin ...

Using @material-ui/core/useScrollTrigger in a Next.js application: a step-by-step guide

I have been researching the Material-UI documentation for useScrollTrigger and attempting to implement it in Next.js to replicate the Elevate App Bar. https://material-ui.com/components/app-bar/#usescrolltrigger-options-trigger import React from "react"; ...

Updating items within a nested list in Redux can be achieved by carefully managing the state and implementing actions to add or remove

My current state is set up like this: Fruits: { 34: { FruitsID: 34, FruitsList:{apple, pineapple, banana} } } I want to update my fruit list by adding 'peach' and 'pear', while also removing 'apple&apos ...

What is the method to update an element when any of the items in an array is marked as "Confirmed"?

{submissions && submissions.files_set.map((el) => { if (el.confirmed_status === null) { return <DataToConfirm />; } else if (el.confirmed_status === "CONFIRMED") { return <DataSent />; } })} This ...

Traversing a multidimensional array with jQuery

In my quest to iterate through a multidimensional array, I encountered the need to remove the class "list" from the divs within the array. However, there could be multiple divs with this class on my site. My initial approach involved using two for-loops, ...

Tips for creating a breakpoint in Sass specifically for a 414px iPhone screen size

For my latest project, I am utilizing sass and looking to incorporate a sass breakpoint or media query. My goal is to have the code execute only if the screen size is 414px or less. Below is my existing code: @include media-breakpoint-down(sm) { .sub-men ...

Unable to view indicators and slides are not transitioning in Bootstrap carousel

I am in the process of developing a website with bootstrap, but I am encountering issues with the carousel feature. Despite copying the code directly from their official page, the carousel remains non-functional. The indicators are missing, and the slides ...

Styling the background of a navigation menu specifically for mobile devices using CSS

Currently, my focus is on developing the website I am using Elementskit nav builder and I aim to change the background color of the navigation menu on mobile and tablet devices to black The existing code snippet is as follows: margin-top: 6px; } @med ...

What causes the "branch is not a function at Condition.fn" error when YUP conditional validation is applied?

I am currently working with a MUIRadioGroup that has options for Low, Medium, and High, as well as a multi-select MUIAutocomplete component on my form. <MUIRadioGroup name="requestDetail.riskAssesmentDetail.riskClassification" ...

Disable the clear button on input fields for Internet Explorer version 8

Struggling with a form issue in IE8 where the input fields display a clear button, as seen below: My attempt to fix this by adding the following CSS rule was unsuccessful: .someinput::-ms-clear { display: none; } Unfortunately, this solution does no ...

Difficulty in properly updating React state within setInterval functions

I have a button in the user interface and when the OnClick event handler is triggered, a setInterval timer needs to run. Within this interval timer, I am checking for a specific condition. If the condition is met, I update the state accordingly. However, I ...

The second parameter within the ReactJS function is not defined

Exploring the world of ReactJS through a small project has been an enjoyable experience for me so far. I encountered a few challenges along the way, but this community has been helpful in resolving them. Currently, my focus is on modifying the links in th ...

MERN stack does not have a defined onClick function

I am developing a website for college registration, and I encountered an issue while trying to create a feature that allows students to select a major and view all the associated courses. I made a list of majors with buttons next to them, but whenever I at ...

Tips for evenly spacing Navbar items in Bootstrap 4

Designing a navigation system using Bootstrap for my website. I'm struggling to figure out the best way to space out the navigation link elements on my navbar so that it looks good on both desktop and mobile screens. This is my current navbar code: ...

Updates made to CSS are not appearing on the Joomla site

My website is based on Joomla and has been functioning well since it was created. However, recently I have noticed that any CSS changes made in the files are not showing up on the site. I have tried clearing the cache and viewing it from different ISPs, ...

Encounter an issue with NextJS and CKEditor5 integration when using the CodeBlock plugin due to a global CSS import

I'm currently working on implementing CKEditor5 in NextJS. Below are the versions of the packages I am using: "@ckeditor/ckeditor5-build-classic": "^22.0.0", "@ckeditor/ckeditor5-code-block": "^22.0.0", "@c ...

Tips for implementing defaultValue within the select tag on a functional component

I am working on a functional component that includes a select tag with various options. My goal is to have "cars" as the default value for the select tag. Initially, I tried adding selected to the car's option but React warned me to use defaultVa ...

It appears that the HTML links are non-responsive and appear to be arranged in an

I have a fairly straightforward website set up. It utilizes php to scan for images and then displays them one by one with navigation buttons at the top. These buttons allow users to switch back and forth between the images. At the top of the page, there ar ...