CSS导入或带有"media"属性的<link rel ...>

Ale*_*lex 7 html css stylesheet

在页面中包含CSS的最佳方法是什么?为什么?

例如:

<style type="text/css">
  @import "style.css" screen, tv;
  @import "print.css" print;
  @import "iphone.css" iphone;
</style>
Run Code Online (Sandbox Code Playgroud)

要么

<LINK rel="stylesheet" media="screen" href="style.css" type="text/css" />
<LINK rel="stylesheet" media="print" href="print.css" type="text/css" />
<LINK rel="stylesheet" media="iphone" href="iphone.css" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

据我所知@import,在古代浏览器中不起作用,这可能是一个优势,因为这些浏览器只显示文本而不是不可读的CSS混乱(使用链接时).

lep*_*epe 7

已多次讨论过,您可以在这里阅读更多内容:

http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

CSS中的@import和链接之间的区别

http://webdesign.about.com/od/beginningcss/f/css_import_link.htm

提一些......

就个人而言,我从不使用@import作为性能影响.

  • 不幸的是,IE可以做些什么来使web开发更有趣.请参阅此文章(http://developer.yahoo.com/performance/rules.html#csslink),其中指出"在IE中@import的行为与在页面底部使用<link>相同,因此最好不要使用它." 这就是为什么许多网站建议在页面上最后加载脚本以及为什么在DOM准备好之后只运行JS是一种很好的做法. (2认同)