7

I heard (from Crockford) what type attributes on LINK and SCRIPT elements are superfluous when those elements are used to load external resources. (Because the HTTP response determines the content-type of the resource.)

<link rel="Stylesheet" href="foo.css">

<script src="foo.js"></script>

But what about the case when non-HTML code is placed inline inside the STYLE and SCRIPT elements?

<style>
    /* inline CSS rules */
</style>

<script>
    // inline JavaScript code
</script>

Is setting the type attribute in those cases recommended? Are there any issues when we choose to omit the type attribute?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385

1 Answers1

11

For HTML 4, the answer is simple: The type attribute is required for both <script> and <style>.

Authors must supply a value for this attribute; there is no default value for this attribute.

As far as I know, the default fallback in all browsers in its absence is text/javascript and text/css, respectively. It's widespread (though invalid) practice to not use the type attribute. I would still specify it to avoid browser problems.

HTML 5 accepts reality and declares these values as official defaults for <style> and <script>. I'm pretty sure this makes it okay to leave them off for inline content as well, which will be parsed using the correct content type automatically.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088