CSS CSS Basics (2014) Basic Selectors Reusing Classes

Virginia Ceban
Virginia Ceban
1,510 Points

what will happen if an element have 2 classes and they have the same properties, what then overrides?

I saw that if an element have a class and a id, and in there is the same property, the id property overrides the class... but what happen if an element have 2 classes and they have the same properties, what then overrides?

J S
J S
7,387 Points

Hey Virginia,

I believe it's whatever class is declared last in your CSS will take precedence. For example, in my code snippet below, the style in the "second" class will override "first" (no matter what order you put them in the HTML).

.first { background-color: red; }
.second { background-color: green; }

2 Answers

Stephanie van Doleweerd
Stephanie van Doleweerd
2,552 Points

Just to be pedantic; this is because CSS stands for "cascading" style sheet - the instructions cascade from the top of the CSS file to the bottom, and the instructions at the bottom of the cascade will take priority.