0

I have a strange problem

I'm trying to style a SemanticUI button, but the button's internal CSS is applied on top of my CSS class, and so none of my stylings are applied.

Note the top style is from semantic.min.css, while the style under it (being overridden) is defined in my own style tag

Style orderings applied to button

Also note: The ordering of the CSS in the header appears to be 'correct', or at least, my custom CSS comes after the SemanticUI import

Header CSS import & local definitions

I'm using SemanticUI React with CSS Modules, if that makes a difference.

How can I get my class applied over the top of semanticUI's button class?

Notice that all my styles from custom class are overridden

4
  • In the <head> section, your custom css link should be after semantic ui css. Also you can make use of !important on your custom css. E.g. color: green !important; hope this works. Commented Dec 27, 2018 at 14:57
  • @MicSel The important tag works (and is new to me - thanks), but it's not what I would chose - I have to apply it to every declaration. I've added in a screenshot of the CSS import/declaration order in my 'head'. From what I understand, this -should- give my local stylings precedence over the default style, but thats not what's happening. Commented Dec 27, 2018 at 15:15
  • What is the selector that overides your css? Commented Dec 27, 2018 at 15:17
  • @MarkBaijens See the edits - the ui.button is from the SemanticUI css, and the custom class is defined in the style tag applied later in the head section, yet somehow the styles are applied in opposite order to the element Commented Dec 27, 2018 at 15:26

2 Answers 2

2

It seems Semantic UI's selector has a higher specificity than yours, as it's selector is .ui.button it has two classes being selected, in contrast your selector is only one class, thus, less specific than the one from Semantic UI's stylesheet, try adding another class, id, or tag being more specific to that element so that your style applies correctly.

So for example, if semantic ui says .ui.button { } you can try .ui.button._31HHf.... { }

[edit]

For React with CSS Modules, we can combine classes with the following:

import styles from './index.module.css';
import cx from 'classnames';
...

      <Button className={cx(
        styles.ui,
        styles.button,
        styles.mainButton
      )}>LEARN MORE</Button>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this was it. I didn't realize that nested classes had higher specificity irregardless of import order. That's annoying and a bit confusing, but we can deal with it
You can also use id as well, and that has higher specificity than any class, although its better practice to just stick with classes for CSS
Yeah, I considered that but that would limit to a single element per rule, which wouldn't be very efficient
1

based on image you have provided you can use

#app .container .(your highlighted selector)

so it will be for ex

#app .container ._31HHF..{
  color: red;
}

2 Comments

The style is being applied, it's just applied below the default SemanticUI styling, and so is overridden by the default stylings
@FrozenKiwi Probably by a more specific selector. What selector overrides it?

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.