Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the order of loading the CSS files in a HTML page?

Tags:

I want to know the order of loading the CSS files in a HTML page.

My actual requirement is like this: I have more than 10 CSS files in my application.

I am importing some 3 to 4 CSS files in each HTML page. The problem is I have duplicate classes that defined in some CSS files. That means I override some of the CSS classes in the CSS files. In some pages it behaves correctly. In some pages it behaves wrongly. I have inline styles defined for some of the DIVs in HTML page also. I am keeping CSS class for that DIVs also.

Can anyone know which one will take higher priority or which one loads first ?

like image 819
Puru Avatar asked Jun 18 '10 05:06

Puru


People also ask

What order are CSS files loaded?

Is it depends on size of the CSS file, name or any other parameter ? The external files are loaded the order they are included in the DOM, and imported stylesheets before the 'importer'.

What order should my CSS be?

CSS rules always prioritize from left to right, then from top to bottom.

What order are CSS classes applied?

The order of classes in the attribute is irrelevant. All the classes in the class attribute are applied equally to the element.

Does CSS load order matter?

The simple answer is yes.


2 Answers

Generally the last rule takes precedence. With that being said, there are "exceptions" in that inline styles take precedence over external stylesheets ( an inline !important is more important than an external !important, etc ), and more specific selectors override generic selectors.

Read all about it @ http://www.w3.org/TR/CSS2/cascade.html

like image 165
meder omuraliev Avatar answered Sep 23 '22 03:09

meder omuraliev


CSS files are loaded in the order that they appear in the page. If a class is redefined in a CSS file, it will override the previous class statements.

So
div.sample { background: none; width: 200px }
and
div.sample { color: #FFF; width: 400px }
will become
div.sample { background: none; color: #FFF; width: 400px }

You can also use the '!important' addin to make rules take precedence over other defined rules.

So
div.sample { background: none; width: 200px !important }
and
div.sample { color: #FFF; width: 400px }
will become
div.sample { background: none; color: #FFF; width: 200px !important }

Note: Many people will advise against using the '!important' addin in your CSS files. Personally, I see nothing wrong with it.

like image 23
animuson Avatar answered Sep 22 '22 03:09

animuson