The way JavaScript works is we can do scripts as an inline block:
<script>
let foo = "bar";
</script>
Or, if the script should be fetched from the network…
<script src="/js/global.js"></script>
With CSS, we can do an inline block of styles:
<style>
.foo { color: red; }
</style>
So why not <style src=""></style>
? Instead, we have <link href="">
.
Harry Roberts asked about that the other day on Twitter:
Can any W3 historians tell us why it’s `<link rel="stylesheet" />` and not `<style src="…">`?
— Harry Roberts (@csswizardry) November 29, 2018
There is lots of speculation in that thread, but Bruce has a pretty clear answer:
AFAIK, <foo src=""> tells the browser to get something and insert it here – eg <img src="">, "<script src="">. Stylesheets aren't 'inserted', they are related to the current doc, but typically style more than 1 page. <style></style> declares a block of rules for this page only
— Bruce Lawson (@brucel) November 29, 2018
I sort of get that. The location in the document matters with src
, but not with <link>
— that relates to the entire document instead. I guess the crack in that reasoning is that the order of stylesheets does matter for order-specificity, but I take the point.
The W3C chimed to confirm that logic:
(2/2) and the original <script> element in HTML 3.2 didn't have a src attribute. the REC for HTML 3.2 mentions <link> as a way to link to scripts, but it doesn't define any keywords for rel=.
— W3C (@w3c) November 29, 2018
There we have it: <style src=""></style>
wasn’t even considered.
That was one of the questions, I always wondered. Thank you.
You forgot to include the second Tweet by the w3c account in that thread. Link here.
it goes onto to say:
So basically, from what I can gather… it was never really proposed as script vs link/style, they were thought of independently and time and use has just semented it into the standards.
I’d quite like having
<style src=""></style>
, it looks more uniformed when used in conjunction with script tags. Plus, having different methods of doing the same thing (including an external file on a page) seems silly.