16

I had studied earlier that embedded CSS always overrides external css. But I found that whichever comes last in the code, those styles prevail.

Please see the following code, considering that I have used color:green; in external CSS for h3.

<head>
<link rel=stylesheet href="style.css">
<style>
h3{
color:red;
}
</style>
</head>

Output of the above code will show me any text I write inside h3 in red color.

But if I write the above code like this:-

<head>
    <style>
h3{
color:red;
}
</style>
<link rel=stylesheet href="style.css">
</head>

In the above case, I get the color of text inside h3 as "green" (since assuming I have given "green" as font-color in external CSS ).

This is because I have written link tag after style tag.

So which means that external css is not always over-ridden by embedded css.

Or is it a rule to write the link tag always before style tag in head.

Please explain this point.

d219
  • 2,707
  • 5
  • 31
  • 36
A_J
  • 977
  • 4
  • 16
  • 44
  • 1
    Where and how did you study that "embedded css always overrides external css"? –  Jul 12 '15 at 16:35
  • 1
    I'm sure what he read as "embedded" referred to inline styles via the universal style ***attribute*** - and he confused this with the style ***tag***. – connexo Jul 12 '15 at 16:48
  • @torazaburo, I read about it here: http://www.boogiejack.com/CSS_2.html – A_J Jul 12 '15 at 16:58
  • and also here: http://www.boogiejack.com/CSS_4.html – A_J Jul 12 '15 at 17:01
  • *Embedded styles override external styles.* This website is wrong. I suggest you stop consulting it. There are many better tutorials and introductory materials. –  Jul 12 '15 at 17:04

4 Answers4

16

It doesn't matter if your stylesheet is within <style>-tags or externally and linked with <link />. The last one has always precedence, they could even be in the same external file, really just the order of the selectors and their specificities matter.

However, inline CSS using the style=".." attribute always has precedence, because it's most specific. To override that, you would have to use !important. Properties in style=".." using !important cannot be overridden.

kelunik
  • 6,750
  • 2
  • 41
  • 70
  • 3
    Inline CSS only gets precedence because it's more specific, not because of the order. In HTML5 you can have `style` tags inside the body in some cases, but placing that after the element it applies to doesn't give it precedence over inline CSS. – Guffa Jul 12 '15 at 16:33
5

Which CSS rules are applied depends on the specificity of the CSS rule, where that rule is placed, and the presence of !important. If two contradictory rules are placed, the rule declared later will overwrite the previous rule. If two contradictory rules are declared with selectors of varying specificity, the more specific styles will win, regardless of placement. If a rule is marked as !important e.g.

h1 {
  color: green !important;
}

the !important rule will always win.

For reference the list of specificity of CSS selectors goes like this (from most specific to least):

  1. Style attributes
  2. ID
  3. Class, pseudo class, attribute
  4. Elements
Tim
  • 2,123
  • 4
  • 27
  • 44
  • Sure, but this question was about neither specificity nor important. –  Jul 12 '15 at 16:35
  • But it was about CSS hierarchy; which rules take precedence over the others. I was just trying to expand my answer to be a complete explanation of what factors are considered when contradictory CSS rules are declared. – Tim Jul 13 '15 at 02:35
  • But you didn't say anything about embedded and external css priority which was the main question – A_J Jul 26 '15 at 06:51
2

It does not matter if it is embedded or not. styles are applied according to Cascading order

Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
1

After all the rules of css, if there are 2 with the same specificity, the last one defined will take over.

For example, writing:

div { 
    background: green;
}


div {
    background: red;
}

Will turn it red regardless of the source.

PiniH
  • 1,901
  • 13
  • 20