Why does css stylesheets link link to a link?

If CSS styles are defined in the <style> , then why do they link to <link> ?

JavaScript uses the <script> to define inline scripts and reference them using <script src=""> , so why not use <style src=""> for CSS? Is there a special function that the <link> can perform using <style> cannot?

+4
source share
1 answer

The root of this asymmetry between style and source comes from the differences in how HTML handles scripts and resources. Javascript (surprise!) A script, while style sheets are considered a resource.

As we know, scripts can be written inline or retrieved from other pages, as with the same element: script . This is how the specifications tell us that it should work .

But also in spec, a style element determines the style of your page and all that it can do. It cannot reference a style from any other documents, such as a script tag. What for? I can not say more.

On the other hand, a link element links resources to your page, which can also be styles and other things. Therefore, when you use outsourcing, you do not use <style> , but instead you <link> it.

It is true that you can imagine that you can write something like <style src=''> , where you specify the source of your style, but, alas, this attribute does not exist. Therefore we cannot!

Now I understand that saying “Because the specifications say that” may be the most satisfactory answer. But this is the best I can do for you right now.

Source: specifications for styles and references .

+3
source

All Articles