0

I have the following style for my <li> tag inside a .css file:

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;
}

Now the problem is that I want to add an active class to the current selected <li> using className attribute :

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

And the active class style : (inside the same .css file)

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

But this doesn't override the original background-color. Any help ?

NOTE: The currentPage variable is working as expected. (tested it with background-color removed from li)

1 Answer 1

1

Try to give it a higher specifity:

li.active {
 background-color: #2f89fc;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I want this active class to be usable for more elements.
well, if it does work with the li, you could still use a selector like li.active, p.active, div.active { ... } (Depending on the type of elements to which you wanrt to add your class)
Yes, it works. But it works with "!important" at the end too. Which approach is better?
I think the combined selectors are better, without !important . !important should only ever be the last solution if nothing else works. You might want to overwrite that rule in some other situation later on, and then !important is not nice to handle...
Got it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.