Is type="text/css" mandatory on the <link> element?
Asked Answered
R

2

7

I'm trying to use Google Web Fonts, and on the official site it recommends using a <link> tag including the type attribute, as following:

<link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700' rel='stylesheet' type='text/css'>`

In the official HTML5 boilerplate site they ommit the type attribute

<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400,700">

According to this rather old answer, in HTML5 the type attribute is optional on the <style> tag and mandatory on the <link> tag.

However, the version without the type attribule validates fine on the W3C validator.

So, is the type attribute mandatory or not?

Retinite answered 9/7, 2013 at 17:35 Comment(3)
possible duplicate of Is there a style type besides text/css?Bukavu
The approved answer in the old question is wrong. The way to deal with this is to have this fixed, rather than spawning new copies of the same question and leaving the old incorrect answers around.Bukavu
possible duplicate of style type="text/css" ... what else is there?Unaccountedfor
R
7

I found the answer on the official W3C HTML5 draft:

The type attribute gives the MIME type of the linked resource. It is purely advisory. The value must be a valid MIME type.

For external resource links, the type attribute is used as a hint to user agents so that they can avoid fetching resources they do not support. If the attribute is present, then the user agent must assume that the resource is of the given type (even if that is not a valid MIME type, e.g. the empty string). If the attribute is omitted, but the external resource link type has a default type defined, then the user agent must assume that the resource is of that type. (...)

User agents must not consider the type attribute authoritative — upon fetching the resource, user agents must not use the type attribute to determine its actual type. Only the actual type (...).

The stylesheet link type defines rules for processing the resource's Content-Type metadata. (...)

If a document contains style sheet links labeled as follows:

 <link rel="stylesheet" href="A" type="text/plain">
 <link rel="stylesheet" href="B" type="text/css">
 <link rel="stylesheet" href="C">

...then a compliant UA that supported only CSS style sheets would fetch the B and C files, and skip the A file (since text/plain is not the MIME type for CSS style sheets).

For files B and C, it would then check the actual types returned by the server. For those that are sent as text/css, it would apply the styles, but for those labeled as text/plain, or any other type, it would not.

If one of the two files was returned without a Content-Type metadata, or with a syntactically incorrect type like Content-Type: "null", then the default type for stylesheet links would kick in. Since that default type is text/css, the style sheet would nonetheless be applied.

For the <style> attribute, the same document states:

The type attribute gives the styling language. If the attribute is present, its value must be a valid MIME type that designates a styling language. The charset parameter must not be specified. The default value for the type attribute, which is used if the attribute is absent, is "text/css". [RFC2318]

Retinite answered 9/7, 2013 at 17:35 Comment(0)
B
0

I think it is not mandatory, I have successfully used <style> and also <style type="text/css">.. Anything u use will be working such as <script> and <script type="text/javascript"> work the same way :)

Botnick answered 9/7, 2013 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.