Skip to content

35.页面导入样式时,使用link和@import有什么区别? #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
webVueBlog opened this issue Mar 14, 2020 · 2 comments
Open
Labels
HTML&CSS HTML&CSS

Comments

@webVueBlog
Copy link
Member

一:

区别1:link是XHTML标签,除了加载CSS外,还能用于定义RSS, 定义rel连接属性等作用;而@import是CSS提供的,只能用于加载CSS;

区别2:link引用CSS时,在页面载入时同时加载;@import需要页面网页完全载入以后加载。

区别3:link是XHTML标签,无兼容问题;@import是在CSS2.1提出的,低版本(IE5及以下)的浏览器不支持。

区别4:link支持使用Javascript控制DOM去改变样式;而@import不支持。

二:

本质上,这两种方式都是为了加载CSS文件,但还是存在着细微的差别

1 老祖宗的差别。link属于XHTML标签,而@import完全是CSS提供的一种方式。

link标签除了可以加载CSS外,还可以做很多其它的事情,比如定义RSS,定义rel连接属性等,@import就只能加载CSS了。

2 加载顺序的差别。当一个页面被加载的时候(就是被浏览者浏览的时候),link引用的CSS会同时被加载,而@import引用的CSS会等到页面全部被下载完再被加载。所以有时候浏览@import加载CSS的页面时开始会没有样式(就是闪烁),网速慢的时候还挺明显.

3 兼容性的差别。由于@import是CSS2.1提出的所以老的浏览器不支持,@import只有在IE5以上的才能识别,而link标签无此问题。

4 使用dom控制样式时的差别。当使用javascript控制dom去改变样式的时候,只能使用link标签,因为@import不是dom可以控制的。

@webVueBlog webVueBlog added the HTML&CSS HTML&CSS label Mar 14, 2020
@webVueBlog
Copy link
Member Author

A:

Difference 1: link is XHTML tag, in addition to loading CSS, can also be used to define RSS, define rel connection attributes, etc. @import is provided by CSS and can only be used to load CSS.

Difference 2: when link references CSS, the page is loaded at the same time; @import requires the page to be fully loaded and later loaded.

Difference 3: link is XHTML tag, no compatibility problem; @import was introduced in CSS2.1 and is not supported by browsers with lower versions (IE5 and below).

Difference 4: link supports using Javascript to control the DOM to change styles; @import is not supported.

2:

Essentially, both of these are for loading CSS files, but there are subtle differences

The difference between ancestors. Link belongs to the XHTML tag, and @import is entirely a way of providing CSS.

Link tags can do a lot of other things besides loading CSS, such as defining RSS, defining rel connection properties, etc. @import can only load CSS.

  1. Differences in loading order. When a page is loaded (that is, viewed by the viewer), the CSS referenced by link is loaded at the same time, while the CSS referenced by @import is loaded when the page is all downloaded. So sometimes when you're browsing a CSS page loaded with @import, you start off with no style (just flicker) and it's obvious when the network is slow.

3 compatibility differences. Since @import is proposed by CSS2.1, old browsers do not support it. Only those above IE5 can recognize @import, while the link tag does not have this problem.

4 use dom to control style differences. When using javascript to control the dom to change the style, you can only use the link tag, because @import is not something that the dom can control.

@webVueBlog
Copy link
Member Author

内联引用 CSS。可灵巧应用样式於各标签中。方便于编写代码时的使用。没有整 篇文件的“统一性”,在需要修改某样式的时候也变的比较困难。

内部引用 CSS 将样式规则写在<STYLE>...</STYLE>标签之中。

外部引用 link 标签引用 CSS 将样式规则写在.css 的样式文件中,再以标 签引入。这样引入该 css 样式表文件以后,就可以直接套用该样式档案中所制定的样 式了。

外部引用 @import 引用 CSS 跟 link 方法很像,但必须放在<STYLE>..</STYLE> 中: <STYLE TYPE="text/css"> </STYLE>可以灵活的引入 css 文件对 xhtml 元素进行控制。有时候也用来 编写某些 css hack。这种方法的缺点:在个别文件或元素的灵活度不足老祖宗的差 别。link 属于 XHTML 标签,而@import 完全是 CSS 提供的一种方式。

加载顺序的差别。当一个页面被加载的时候(就是被浏览者浏览的时候),link 引用的 CSS 会同时被加 载,而@import 引用的 CSS 会等到页面全部被下载完再被加 载。

兼容性的差别。由于@import 是 CSS2.1 提出的所以老的浏览器不支持,@import 只有在 IE5 以上的才能识别,而 link 标签无此问题。

使用 dom 控制样式时的差别。当使用 javascript 控制 dom 去改变样式的时 候,只能使用 link 标签,因为@import 不是 dom 可以控制的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HTML&CSS HTML&CSS
Projects
None yet
Development

No branches or pull requests

1 participant