Do you really need to specify the type attribute? [duplicate] Do you really need to specify the type attribute? [duplicate] javascript javascript

Do you really need to specify the type attribute? [duplicate]


Most people are used to HTML 4/XHTML and before, where the type attribute is required for these elements.

In regards to HTML 5, these are indeed optional and the spec gives a default, depending on the element.

For the script tag, this defaults to text/javascript:

If the language is not that described by "text/javascript", then the type attribute must be present

For the style tag, this defaults to text/css:

The default value for the type attribute, which is used if the attribute is absent, is "text/css".

So, not needed, as you stated. However, browser support and server setups can't always be relied on - being explicit is a good idea as it avoids such problems.

And of course, not all browsers out there support HTML 5 - those that don't will use an earlier version where the attribute is required and your javascript/css might not get parsed in such browsers, meaning you end up with no CSS or javascript on older browsers, when a simple solution for backwards compatibility is to add the attribute.


The type attribute may not be required for HTML5 but it is required for other HTML Doc Types such as HTML 4.01 Strict. I'd also say that anything making the code/document clearer for the developer is really only ever a good thing.

If that means being explicit about the type of script being used or the type of style, I'd use it.


That's not good. In XHTML, the type attribute is strictly required. Although browsers may be lenient, that's no reason to break convention.