-1

Suppose I have two classes in a div like in the example below

.class1 {
      background-color: blue;
      border: solid black;
    }

.class2 {
      background-color: red;
      border: solid black;

    }
<div>
  <div class="class1 class2">
    This is red
  </div>
  <br>
  <div class="class2 class1">
    This should have been blue but its red
  </div>
</div>

There doesn't seem to be any rule regarding which class takes the precedence.

Why both the boxes has a red background in the output?

Riwaj Chalise
  • 637
  • 1
  • 13
  • 27

2 Answers2

1

There are certain rules CSS bases on to apply conflicting styles.

In your case, the last class to be interpreted by the browser (class2 in the first <div> and class1 on the second) is the one being applied.

There are some exceptions to this rule, based on certain use cases. You can read more about CSS specificity by clicking here.

1

i think is in which one you defined later in css file and if you use !important. You can also see this Is it possible to give one CSS class priority over another?