Is type=“text/css” mandatory on the <link> e

2019-06-27 04:20发布

问题:

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?

回答1:

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]



回答2:

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 :)