@import vs链接 [英] @import vs link

查看:129
本文介绍了@import vs链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道标题说这是一个重复的问题,此处此处在这里。我在这个话题上阅读的一切都是两年多了。在那段时间里有很多变化,所以同样的想法仍然可取吗?

First I know the title says this is a duplicate question as asked here, here, and here. Everything I read on this topic is over two years old. A lot has changed in that time period so are the same thoughts still advisable?

下面是一个例子,我在样式表中使用@import来引入我的重置CSS和一些其他样式。我应该从 @import 更改为< link> 吗?根据此文章,我应该使用链接。所以我问其他开发者,什么是真正的最好的方式,到目前为止(2011年8月25日)

Here is an example, I use @import inside a stylesheet to bring in my reset CSS and a couple other styles. Should I change that from @import to <link>? According to this article I should use link. So I ask other developers, what is truly the best way as to date (August 25, 2011)

推荐答案

在过去一两年内发生了变化,我们仍然在处理很多相同的浏览器,所以你不应该改变你的做法。

Not much if anything has changed in the past year or two, and we're still dealing with a lot of the same browsers from then, so you should not change your practice.

< link> 在所有情况下优先于 @import ,因为后者会阻止并行下载,

<link> is preferred in all cases over @import, because the latter blocks parallel downloads, meaning that the browser will wait for the imported file to finish downloading before it starts downloading the rest of the content.

您可以在这里非常详细地看到这个:

You can see this in great detail here:

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

因此, @import 方便,这是所有它提供。如果你真的想利用快速加载时间,使用最小数量的样式表(在大多数情况下可能是一个),使用高效的选择器(通常的东西)写良好的CSS,缩小它,并使用 < link> 标签。

So, while @import may be convenient, that's all it offers. If you really want to take advantage of fast loading times, use the minimum number of stylesheets (probably one in most cases), write good CSS with efficient selectors (the usual stuff), minify it, and use a <link> tag.

这将是一个注释, long:

This was going to be a comment but it got too long:

而不是 @import (我知道非常方便)您应该在您的网站上线时将这些文件合并为一个文件。你不应该在这一点上调整,有很多工具来帮助缩小它。就个人而言,使用PHP,我有一个配置文件,其中我定义所有的CSS文件写入一个单独的CSS文件(我将引用< link> 标签),则如果缓存的版本是旧的(手动或自动确定),则它组合/缩小它们并将内容写入缓存文件,并且返回时间戳查询字符串以附加到CSS文件名,以强制新鲜下载。

Instead of @import (I know it is very convenient), you should combine the files into one when your site goes live. You shouldn't be tweaking at that point anyways, and there are a number of tools to help minify it. Personally, using PHP, I have a config file where I define all the CSS files that are written to a separate CSS file (the one I will reference in the <link> tag), then if the cached version is old (either determined manually or automatically), it combines/minifies them and writes the content to the "cache" file, and returns a timestamp query string to append to the CSS file name to force a fresh download.

如果您也使用PHP,我强烈建议您 cssmin ,它可以解析 @import 的样式表,并将内容拖入一个文件,以及处理缩小的所有方面。

If you are using PHP as well, I highly recommend cssmin, it can parse stylesheets for @import and pull the content into one file, as well as handle all aspects of minification.

这篇关于@import vs链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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