在< head>之后包含CSS的正确方法 [英] Correct way to include CSS after <head>

查看:163
本文介绍了在< head>之后包含CSS的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,在文档正文中添加< link rel =stylesheet... 被W3C标准视为不良做法。在主体中添加< style> 块也是如此...

Apparently adding <link rel="stylesheet" ... in the document body is considered a bad practice by W3C standards. The same for adding <style> blocks in the body...

解决方案在< head> 标签之外添加CSS?

So are there any standard-compliant solutions to add CSS outside of the <head> tag? Like at the end of the document.

推荐答案

如果您只想在特定事件中包含CSS样式,你这样做的头:

If you only want to include your CSS styles on a specific events, there's nothing stopping you from doing so at the head:

var linkElement = document.createElement("link");
linkElement.rel = "stylesheet";
linkElement.href = "path/to/file.css"; //Replace here

document.head.appendChild(linkElement);

这增加了以异步方式添加样式表的额外好处,从下载任何其他内容。

This has the added benefit of adding your stylesheet in an async way, which doesn't block the browser from downloading anything else.

这篇关于在&lt; head&gt;之后包含CSS的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆