0

I know that inline has more specificity than external but does internal have more than inline? Or is it the other way around?

Internal:

<style>
p{
  color: red;
}
</style>

Inline:

<p style="color:blue">

External:

p{
  color:green;
}
Jakub Muda
  • 4,356
  • 10
  • 32
  • 45
ghost2092000
  • 157
  • 1
  • 2
  • 10
  • That would depend entirely on the order of the CSS loading. For equal CSS styles, whichever is loaded last will take precedent – Martin Dec 20 '19 at 18:54
  • https://www.hungred.com/useful-information/inline-internal-external-css-style-print/ --OR-- https://www.w3schools.com/css/css_howto.asp at the end of the page – Soroush Falahati Dec 20 '19 at 18:56
  • 2
    Specificity is determined by an algorithm, detailed in [Selectors Level 3](https://www.w3.org/TR/selectors-3/#specificity). – Heretic Monkey Dec 20 '19 at 19:11
  • Does this answer your question? [Understanding CSS selector priority / specificity](https://stackoverflow.com/questions/4072365/understanding-css-selector-priority-specificity) – disinfor Dec 21 '19 at 04:03

1 Answers1

3

No, internal does not take over inline. Inline styles are always the highest priority. From Mozilla Docs:

Inline styles added to an element (e.g., style="font-weight: bold;") always overwrite any styles in external stylesheets, and thus can be thought of as having the highest specificity.

These "external stylesheets" also include style tags in the head or body. See for yourself:

p {
  color: red;
}
<style>
  p {
    color: red;
  }
</style>

<p style="color: blue">
  Hello!
</p>

<style>
  p {
    color: red;
  }
</style>

Whether it is before or after, the only thing that will override an inline style is !important, which you should shy away from using. Refer to the MDN link above.


Finally, be careful when you say,

I know that Inline has more specificity than external [...]

since specificity is a special CSS concept. Yes, inline styles will override external styles (when not using !important), but don't confuse specificity with precedence. CSS rules are ranked in two ways:

  1. Specificity
  2. and Order.

This includes order across separate files. If you put your style tag before your link tag, then the external styles will overwrite the internal ones (if they are of the same specificity).

Again, this doesn't apply much to internal styles, and this is mostly a semantics clarification for "specificity," but it doesn't hurt to point out.

matthew-e-brown
  • 2,039
  • 1
  • 5
  • 21
  • 2
    This question about `!important` is worth reading too: https://stackoverflow.com/questions/5805040/relationship-between-important-and-css-specificity – Jakub Muda Dec 21 '19 at 03:52