214

I was wondering whether or not it is necessary to use <link rel="stylesheet" type="text/css" href=...> over <link rel="stylesheet" href=...>. The rel="stylesheet" marks the information that it is a stylesheet - so text/css doesn't actually add anything as far as I'm concerned.

The only stylesheet format used by HTML is CSS anyway, so what does text/css 'say' to the browser? Some websites seem to add the type="text/css" attribute (http://www.jquery.com/), whilst other ones don't (http://www.youtube.com/).

So, what is the use of type="text/css" in a <link rel="stylesheet"> element, and is it necessary to include it?

3
  • 1
    It may have been implemented to act as a fallback for servers which don't return the correct MIME-type on .css files, but it seems it wasn't ever necessary. I use it just for consistency's sake.
    – drudge
    Mar 23, 2011 at 17:41
  • Something I just ran into, don't specify type="" or IE will not load your stylesheet.
    – mhenry1384
    Jul 28, 2016 at 14:03
  • Nowadays jquery.com site does not use type="text/css", but the Stackoverflow site is still using it. Dec 23, 2018 at 19:35

6 Answers 6

169

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.

3
  • 27
    it may be required in the HTML4 spec, but no browsers actually enforce it.
    – Spudley
    Mar 23, 2011 at 17:29
  • 4
    well, there is a type for "image/ico"
    – maxisam
    Jul 9, 2013 at 14:29
  • 2
    Actually, the HTML4.01 spec does not appear to require the type attribute. Nov 22, 2014 at 15:54
35

It's not required, no.

The part of the HTML Living Standard you're interested in is The link element, which states:

A link element must have either a rel attribute or an itemprop attribute, but not both.

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

For external resource links, the type attribute is used as a hint to user agents...

2
  • 1
    What does "as a hint to user agents..." mean? What are "user agents"? Feb 14, 2020 at 19:25
  • 2
    @ScottyBlades Good question! A user agent is anything that can act on behalf of a user to download (in this case) HTML. Most of the time that's a web browser, but it could also be a terminal client like curl.
    – alexmuller
    Feb 16, 2020 at 8:04
8

HTML4.01 does not require the type attribute on a <link> tag. According to the specification, the type attribute is a hint to the browser. If the browser does not support the hinted content type, then it can skip fetching it.

type = content-type [CI]

This attribute gives an advisory hint as to the content type of the content available at the link target address. It allows user agents to opt to use a fallback mechanism rather than fetch the content if they are advised that they will get content in a content type they do not support.

Authors who use this attribute take responsibility to manage the risk that it may become inconsistent with the content available at the link target address.

  • NOTE: <link> defers to <a>'s definition for the type attribute.
1
  • 1
    This is the most helpful answer => basically: only use type attribute if the browser may not support the type => useless for web standard files like CSS.
    – jave.web
    Oct 13, 2019 at 18:29
5

My understanding is that it's to allow the specification of stylesheets in formats other than text/css.

While that has become the dominant (and standard) delivery format for stylesheets for (X)HTML documents, the specification is actually wide enough to allow a variety of different MIME types to be passed, it's just that standard browsers don't implement them.

2

Not relevant to most people, but some systems (at least Microsoft SharePoint 2013) will use this type attribute when working out how to process HTML (e.g. when creating a .master page file).

-1

No it just stands for MIME type, it has been deprecated in HTML, We can drop using it without any browser compatibility.

Not the answer you're looking for? Browse other questions tagged or ask your own question.