-5

I came across a interesting code. If we run the below code snippet, green class will be applied for both the divs. Can anyone explain why?

.orange {
    color: orange;
}
.green {
    color: green;
}
<div class="orange green">Div 1</div>
<div class="green orange">Div 2</div>   
Vinit Desai
  • 520
  • 1
  • 4
  • 20

2 Answers2

3

The reason is because CSS is Cascading, so rules are applied according to how far down the file they are. If you switched .orange to the bottom, you would have both <div>s with orange text, and therefore with the class taking priority being orange.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

However, you jumble the class names in html code, but the code(class) which you have written lastly in the CSS file will execute first. As CSS is Cascading and it overrides the content defined earlier.