【问题标题】:load external css file in body tag [duplicate]在body标签中加载外部css文件[重复]
【发布时间】:2014-04-17 07:48:01
【问题描述】:

可能重复:
What's the difference if I put css file inside <head> or <body>?

通常,外部css文件加载代码放在html的头部。

<head>

...
<link href="/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
...

</head>

但是,有时我应该将我的 css 加载代码放在 body 标记中, 我的意思是..

<body>
...
<link href="/uploadify/uploadify.css" type="text/css" rel="stylesheet" />
...
</body>

所以我的问题是, 可以将我的 css 加载代码放在 body 标记中吗?

【问题讨论】:

  • 不是有效的 HTML;它通常有效,但这不是一个好习惯。 (有关更深入的信息,请参阅骗子)
  • 要验证 HTML,当在 body 标签内时,将这个:&lt;link href="/uploadify/uploadify.css" type="text/css" rel="stylesheet" /&gt; 改为这个:&lt;style type="text/css"&gt; @import url("/uploadify/uploadify.css"); &lt;/style&gt;
  • @LeopoldoSanczyk - 显然,不,“body”中的“style”无效。

标签: html css


【解决方案1】:

不,不能在 body 标记中放置 link 元素。请参阅specification(链接到 HTML4.01 规范,但我相信所有版本的 HTML 都是如此):

“这个元素定义了一个链接。与A 不同,它可能只出现在文档的HEAD 部分,尽管它可能出现多次。”

【讨论】:

  • 在 HTML5 中,将 &lt;link&gt;&lt;style&gt; 放入 &lt;body&gt; 是完全可以的。事实上,您甚至可以将&lt;style&gt;s 限定为仅适用于您的文档的一部分!
  • @Vilx - 好奇:你能提供一个链接到规范说可以的地方吗?
  • @Vilx- 在 HTML5 中,link 只能在正文中使用,如果它使用 itemprop 属性而不是 relSpec Reference
  • 直接引用:If the rel attribute is used, the element is restricted to the head element. When used with the itemprop attribute, the element can be used both in the head element and in the body of the page, subject to the constraints of the microdata model.
  • @Pacerier 你可以直接在&lt;body&gt; 标签中使用&lt;link rel="stylesheet" /&gt;,因为stylesheet 类型是body-ok。你可以阅读developer.mozilla.org/en-US/docs/Web/HTML/Element/…。或html.spec.whatwg.org/multipage/links.html#body-ok
猜你喜欢
  • 2021-02-18
  • 2020-05-18
  • 2015-12-17
  • 2016-01-25
  • 2011-01-07
  • 2011-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多