Šime Vidas
Šime Vidas

Reputation: 185873

The type attribute of SCRIPT and STYLE elements in HTML?

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?

Upvotes: 7

Views: 2300

Answers (1)

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 11

Related Questions