Multiple classes with the same name? I know it works but is it valid?
.color {color:orange;}
.first .color {color:blue;}
.second .color {color:red;}
<div class="color">
some text here
</div>
<div class="first">
some <span class="color"> text here</span>
</div>
<div class="second">
some <span class="color"> text here</span>
</div>
Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements. You'll have to fully qualify one of the class names if you really need to reference both of them.
If you have two CSS files containing the same class name, then the properties of both will be applied in a sort of combination. If both class declarations share the same property, then the one for the file that was linked last is applied. Any properties that are declared in only one of the CSS files will be applied.
CPP. In each scope, a name can only represent one entity. So, there cannot be two variables with the same name in the same scope. Using namespaces, we can create two variables or member functions having the same name.
For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.
That's perfectly valid, and perfectly readable. Note particularly that the order the classes are declared in is not important. CSS works on the principle that the most-specific selector wins.
.color {color:orange;}
Defines that any element with class color is orange.
.first .color {color:blue;}
Defines that any element with class color that is a descendent of an element with class first is blue
.second .color {color:red;}
Defines that any element with class color that is an descendent of an element with class second is red
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With