Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two css files defining same class

Tags:

css

If I have two css files:

File 1:

.colorme {    background-color:Red; } 

File 2:

.colorme {    background-color:Green; } 

And have included them in a page, which one will take priority? I'm guessing the one that is loaded last? If so is there anyway to ensure which one css file is loaded last?

like image 723
Haroon Avatar asked Mar 10 '10 13:03

Haroon


People also ask

Can I use 2 CSS files?

Yes, It is possible to include one CSS file in another and it can be done multiple times. Also, import multiple CSS files in the main HTML file or in the main CSS file.

How do I use two CSS files in HTML?

Yes, you can apply more than one stylesheet to an HTML file. For each stylesheet you link to a page, you would just need to add an additional <link> element. Like so, <link href="style1.

Can an element have 2 CSS classes?

HTML elements can be assigned multiple classes by listing the classes in the class attribute, with a blank space to separate them.

How do I combine multiple CSS files into one?

To combine external CSS files, you can simply copy / paste all of your CSS code into one main file. Therefore all of the content from within the other CSS files will now reside within the main file allowing the browser to only make one request for a CSS file instead of multiple.


1 Answers

The one loaded last (or as David points out, more accurately included last) wins in this case. Note that it's per-property though, if you load 2 definitions with different properties, the result will be the combination. If a property is in both the first and second, the last wins on that property.

The only way to ensure which is used last/wins is including the <link> elements in the order you want in the page.

For the property, here's an example:

.class1 { color: red; border: solid 1px blue; padding: 4px; } //First .css .class1 { color: blue; margin: 2px; } //Second .css 

is equivalent to:

.class1 { color: blue; border: solid 1px blue; padding: 4px; margin: 2px; } 
like image 60
Nick Craver Avatar answered Oct 25 '22 13:10

Nick Craver