What you are looking for is called Specificity. The linked article explains it very well.
Basically, which selector has precedence is based on how specific that selector is. As a somewhat intuitive example, .red.fruit
is more specific than just .fruit
since it has two class names. Therefore, in your CSS code, even if the .fruit
selector comes after .red.fruit
, the more specific one takes precedence.
There is more to it than just having multiple class names, though. As you can imagine, different types of selectors have different levels of precedence. For example, ID selectors are considered extremely specific since only a single element can (well, should) have the given ID. In general, this order goes like this, from most specific to least:
Inline Styles > ID Selectors > Class, attribute, and pseudo-class Selectors > Elements and pseudo-elements
Definitely read the article for more info.
Now, all that said, I think it's worth mentioning that as a good rule of thumb, you should aim to make your selectors as least specific as possible. In other words, try not to use ID Selectors or inline styles where you can avoid it, and use the least number of classnames necessary to select the elements you want.
Following this principle allows your CSS to be more flexible in the long run and easier to read.