Is it valid HTML to have two – Mentalist Dec 06 '19 at 04:24

3 Answers3

13

It's perfectly fine to have multiple style elements in between your <head> and </head> tags. You can have as many as you want, together with <link> or <script> elements.

knittl
  • 246,190
  • 53
  • 318
  • 364
  • does the top style tag has the higher preference? – Juke Sep 18 '19 at 13:57
  • 1
    @Juke all style tags will be concatenated together and then the standard rules of CSS regarding specificity and cascading apply. – knittl Sep 18 '19 at 16:30
11

None of the answers have cited any sources, so the OP cannot be sure. (Note that neither the "cascading" part of "CSS" nor the fact that browsers tolerate many STYLE elements proves anything; e.g. they tolerate (thank God, BTW!) STYLE elements in the BODY as well, even though that's considered heresy by the specs, unless the scoped attr. is used, which is in turn largely unsupported by browsers, despite being valid...)

Here's what the current HTML spec says today (2017-02-08):

Contexts in which this element can be used:

  • If the scoped attribute is absent: where metadata content is expected.
  • If the scoped attribute is absent: in a noscript element that is a child of a head element.
  • If the scoped attribute is present: where flow content is expected, but before any other flow content other than other style elements and inter-element whitespace.

Which effectively means the answer to the original question is, indeed: yes. :)

Sz.
  • 3,342
  • 1
  • 30
  • 43
  • 1
    Ehh-hehh, Google dropped me here, read this answer, and wanted to quickly flick an upvote for adding a source... But it failed... WTF, I read the error msg., which said: "You can't vote for your own post."... No shittin, I realized only then that this nice guy with the link was actually me ~2 years ago!... :-D – Sz. Nov 04 '18 at 15:27
  • 2
    Yeah, the web isn't all that big when you live in specific regions of it. I've come across my own past posts when searching too. Actually, since my memory is poor, I often post questions and answers for my future self, anticipating that I will forget how to do something. Nothing worse than having to re-trace steps through a problem you've already cracked before because you didn't document the solution. :-p – Mentalist Dec 06 '19 at 04:14
7

Yes, this is perfectly valid. CSS stands for "cascading style sheets" (as in, layered on top of each other.) Multiple <style> tags allow you to include multiple style sheets or multiple style declarations.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99