Order of tags in <head></head>

2020-02-17 08:51发布

does it matter at all what order the <link> or <script> or <meta> tags are in in the <head></head>?

(daft question but one of those things i've never given any thought to until now.)

标签: html xhtml
14条回答
做个烂人
2楼-- · 2020-02-17 09:00

The accepted answer is kind of wrong, depending on the encoding of the document. If no encoding is sent by in the HTTP header, the browser has to determine the encoding from the document itself.

If the document uses a <meta http-equiv="Content-Type" … declaration to declare its encoding, then any ASCII-valued character (character code < 128) occurring before this statement must be an ASCII value, as per HTML 4 spec. Therefore, it's important that this meta declaration occurs before any other element that may contain non-ASCII characters.

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-17 09:00

I recently was having a problem with a draggable jquery ui element. It was behaving properly in Firefox, but not Safari. After a ton of trial and error, the fix was to move my css links above the javascript links in the head. Very odd, but will now become my standard practice.

查看更多
冷血范
4楼-- · 2020-02-17 09:01

One important thing to note: if you're using the Internet Explorer meta X-UA-Compatible tag to switch rendering modes for IE, it needs to be the first thing in the HEAD:

<head>
  <meta http-equiv="X-UA-Compatible" content="IE=7" />
  <title>Page title</title>
  ...etc
</head>
查看更多
时光不老,我们不散
5楼-- · 2020-02-17 09:02

As already pointed out meta describing content charset should be the first otherwise it could actually be a security hole in a certain situation. (sorry i dont remember that situation well enought to describe here but it was demostrated to me at web security training course)

查看更多
不美不萌又怎样
6楼-- · 2020-02-17 09:04

If you declare the charset in a meta element, you should do it before any other element.

查看更多
虎瘦雄心在
7楼-- · 2020-02-17 09:05

Not a daft question at all.
CSS above Script tags for reasons already mentioned.

CSS is applied in the order you place the tags - the more specific the stylesheet, the lower down the order it should be.

Same goes for scripts - scripts that use functions declared in other files should be loaded after the dependency is loaded.

查看更多
登录 后发表回答