2

Suppose we have 2 css files loaded into a web application, and both declare the .myStyle class with different, overlapping properties:

file 1

.myStyle{
    padding: 10px;
    background-color: rgba(211, 9, 58, 0.08);
    border: 1px solid #D3093A;
}

file 2

.myStyle{
    padding: 12px;
    background-color: rgba(255, 40, 15, 0.18);
    border: 2px solid #D3093A;
}

My question is, which class is going to be used by the browser and which one is going to be overridden? Is there a precedence in this case? My case is, I could declare 2 css files with the same class (but different properties like the examples I provided) on different declaration places, so I can't really be sure on wich one is going to be loaded/read first. Is there such a thing like "css file order" or something?

Thanks for any input.

4
  • 1
    The rule declared last will override anything declared above it for a class with the same name and precedence. You can easily test this yourself
    – j08691
    Commented Apr 28, 2020 at 16:22
  • It will follow the order in which the files are loaded. The properties in the declaration in the first file will be overridden by declarations of the same properties in the second file, if both selectors have the same specificity.
    – Sean
    Commented Apr 28, 2020 at 16:23
  • 1
    Does this answer your question? What is the order of precedence for CSS? - specifically #3 in the accepted answer.
    – disinfor
    Commented Apr 28, 2020 at 16:26
  • 1
    Its really close, but not at all. I updated the question to reflect better what I meant. Sorry!
    – Mauricio
    Commented Apr 28, 2020 at 16:41

1 Answer 1

1

the end style would be from whichever file comes last when linked in your HTML.

If these styles were both in the same stylesheet, the first style would be overridden by the second because it comes after it in the CSS. Because your first stylesheet would be read first, it will essentially do the same by overwriting it when the second stylesheet is loaded.

2
  • is there a way to tell the application to load a file before/after another? There could be a case where a the css files are read from different places within the application.
    – Mauricio
    Commented Apr 28, 2020 at 16:38
  • Can you tell me a bit more about the use case? I wasn't aware of a way of doing this but I did a little googling and it sounds like you can use something like loadCSS.js to do an async load of another stylesheet: css-tricks.com/precedence-css-order-css-matters/… Does that help?
    – St3ph3n92
    Commented Apr 29, 2020 at 18:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.