@import vs link

后端 未结 1 722
Happy的楠姐
Happy的楠姐 2020-11-27 03:14

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

相关标签:
1条回答
  • 2020-11-27 03:38

    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> 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/

    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.


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

    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.

    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.

    0 讨论(0)
提交回复
热议问题