简体   繁体   中英

What is the <link> tag's default MIME type?

When you set a link tag like the following:

<link rel="stylesheet" href="style/global.css">
<link rel="shortcut icon" href="favicon.ico">

Does it get a default MIME type due to its rel attribute without any custom httpd.conf settings (ie, the stylesheet link relationship hints it's a text/css file)?

If yes, what are the "default" MIME types for stylesheet , alternate and shortcut icon ?

For HTML5:

In general, the type attribute gives the MIME type. But an external resource link type (which can be specified with the rel attribute) can define a default MIME type, which gets used if no MIME type is specified explicitly:

If there is no type metadata, but the external resource link type has a default type defined, then the user agent must assume that the resource is of that type.

This is the case for the stylesheet link type , which has the default MIME type text/css :

The default type for resources given by the stylesheet keyword is text/css .

No default type for the icon link type (but image/ has to be assumed):

There is no default type for resources given by the icon keyword. However, for the purposes of determining the type of the resource, user agents must expect the resource to be an image.

The alternate link type can't have a default MIME type, because it's a hyperlink link type, not an external resource link type.

rel specifies how the document relates to the webpage and does not affect MIME-Type.

href is the source file to be loaded and the MIME-Type is determined based on your web server settings (for instance, my Apache 2 server associates *.php files with the application/x-httpd-php type but a non-configured server will see this as whatever its default MIME-Type is (usually text/plain ).

type will override whatever the server returns from it's diagnosis on href and is set manually (not a required field).

For <script> resources, the same process takes place but modern browsers have deprecated the type and language fields unless special cases are present (like using VBScript instead of Javascript - whoever the hell does that anymore).

EDIT:

To address your last comment - Apache does not default to an octet stream unless you specify this in apache2.conf (or httpd.conf/other .conf files). The only reason it would default to this is if you were primarily serving binary files and wanted to avoid browsers parsing the files as plain text in a browser when they are clicked on.

Take this example provided by Windows:

    <LINK REL = "stylesheet"
TYPE = "<code><b class="cfe">mime/type</b></mark>"
HREF = "<code><b class="cfe">url</b></mark>"> 

where the value REL= "stylesheet" indicates that the target source is a style sheet, mime/type is the MIME type for the style sheet, and url is the address of the style sheet file.

Each stylesheet has it's own MIME type and Cascading style sheets (CSS) have the default MIME type text/css .

When adding a favicon to your website, you add something like this within your web page

<link rel="shortcut icon" type="image/vnd.Microsoft.icon" href="/some-folder/some-image.ico">

The "href" is simply the location on your web server where you stored the icon. For example, you would type " /images/favicon-logo.ico " if your favicon is stored in the images folder and is named " favicon-logo.ico. "

There also some "Unofficial MIME Types": Although the unofficial MIME types will work with most web browsers, they do not work with Internet Explorer. Therefore, avoid using the following MIME Types if you want all of your visitors to view the favicon: "image/png," "image/gif," image/jpeg," or any other variation.

The MDN Docs say:

rel
This attribute names a relationship of the linked document to the current document. The attribute must be a space-separated list of the link types values. The most common use of this attribute is to specify a link to an external style sheet: the rel attribute is set to stylesheet, and the href attribute is set to the URL of an external style sheet to format the page. WebTV also supports the use of the value next for rel to preload the next page in a document series.

This is not telling the default value of the ref attribute. But: rel names a relationship of the linked document to the current document, so this attribute must be set to the desired relationship stylesheet in order to tell the browser what to interpret from this link.

The new HTML5 Doctype makes the script tags type attribute superfluous. So only use <script>...</script> (for inline scripts) or <script src="<SOURCE>">...</script> (for includes) in your html if you're using the HTML5 Doctype, else add the type attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM