Is Type="Text/Css" Necessary in a ≪Link≫ Tag

Is type= text/css necessary in a link tag?

It's not required with the HTML5 spec, but for older versions of HTML is it required.

Html 4 W3.org spec

http://www.w3.org/TR/html40/struct/links.html#edef-LINK
http://www.w3.org/TR/html40/present/styles.html

Type stands for The MIME type of the style sheet. The only supported value I have ever seen is Text/CSS, which is probably why HTML5 has dropped it. I imagine they had it for earlier versions to allow future expansion possibilities which never happened.

Using HTML5 and not specifying the type, I have run so far into no problems with compatibility even when testing older versions of IE.

Do we need type= text/css for link in HTML5

The HTML5 spec says that the type attribute is purely advisory and explains in detail how browsers should act if it's omitted (too much to quote here). It doesn't explicitly say that an omitted type attribute is either valid or invalid, but you can safely omit it knowing that browsers will still react as you expect.

Is type= text/css mandatory on the link element?

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]

Is it bad practice to not include type= text/css in style tags?

From the W3.org wiki :

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

So no, you don't need it, as it was always the default value and it's now explicitly optional.

Difference between type= text/css and type= text/stylesheet ?

text/stylesheet is not a valid type for the link element.

You should use text/css.

You are probably seeing a difference in browsers because some browsers have taken into account people might incorrectly use type="text/stylesheet" instead of type="text/css".

Why people continue to use text/css ?

Nothing changed, it is used for backward compability and will probably appear more rarely with time, as more installed browser will support HTML5.

link rel= stylesheet type= text/css href= ../styles.css not working

If you use document relative links you are asking for a headache. I always make my links root relative so

/css/styles.css

or whatever your path relative to root is instead of

../styles.css

I have encountered so many errors in my career due to things being moved and document relative links.

Rails 4 stylesheet_link_tag missing text/css in HTML

You can add type option in your stylesheet_link_tag helper:

= stylesheet_link_tag 'application', type: 'text/css'


Related Topics



Leave a reply



Submit