Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple classes with the same name? [duplicate]

Tags:

css

class

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>
like image 401
devotchkah Avatar asked Feb 21 '13 13:02

devotchkah


People also ask

Can we have multiple classes with same name?

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.

Can you have multiple classes with same name CSS?

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.

Can 2 classes have same name in C++?

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.

Can Java have two classes with 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.


1 Answers

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

like image 187
RB. Avatar answered Oct 21 '22 10:10

RB.