4
From what I’ve been reading, HREF
serves to point to something external(outside the page) that the user requests, or that the page containing it needs to use it (in the case of style sheets).
That is, in the case of style sheets, when pointed with HREF
the browser does not pause the DOM rendering or loading to apply the stylesheet if it is not used at any time.
Already the SRC
it pauses the rendering to dump the file contents called.
About that I commented above, I ask you:
- If I have a function that gives a
append
of an HTML structure of a Newsletter for example, it would be more efficient to call the stylesheet that makes the "design" of this Newsletter forHREF
?
Since at the first moment of the user’s entry on the site, there is no use of this sheet, which will only be used later if the user is requested(click on a button/link to open then the Newsletter), so.. There would be no need to pause the rendering to dump the CSS which at first won’t be used..
Got a little confused?! If I’m confusing something, tell me!
I saw this discussion here: https://stackoverflow.com/questions/3395359/difference-between-src-and-href
And until then I was unaware that the style sheets could be called with the attribute HREF, I always used SRC and never knew the difference of application for both.
From what little I understand of the linked question, it is the parse (reading, parsing) the page that is interrupted when a script uses
src
, while with a link usinghref
that does not happen. The processing itself can stop yes. Also, the elementimg
also uses thesrc
, but in that case neither the Parsing nor does processing stop (i.e. the page is displayed first without the image, and then with). I do not think there is a general rule for all cases. Or do you refer specifically to style sheets?– mgibsonbr
"More efficient" in relation to what? To an inline CSS that would come with append?
– bfavaretto
S[ó a comment that HREF also serves for internal links to the very page that defined it, assigning to it an ID of some existing element in the DOM.
– Bruno Augusto
-Brunoaugusto, in the case of anchors right? @bfavaretto, the idea is to be more efficient in question performance, user experience(page be faster in loading). Because, as the stylesheet of the Newsletter(news.css for example) will only be used if/if the user actually opened the link/button of the Newsletter (which in turn the script would give an append with the HTML structure)At first you wouldn’t need to have the page dump the CSS into the page, so it wouldn’t be in the way of the page’s Parsing. For small applications I see minimal benefits,.. But for large, just testing?
– Ale
Is that a bad practice? less secure, can compromise not the performance but something more important, as some function of the page, by some bottleneck that may occur?
– Ale
It’s just that I don’t understand the question. You say you’ve always used
src
... To load CSS? As I already told Elias in his reply, src link is not valid.– bfavaretto
Understood, then
<style>
if using the HREF attribute instead of SRC (no scenario exists for this one), already images make use of SRC, as well as<script>
makes use of this. This is because it is necessary to dump the content into the HTML, in case the image loads it into the document, and in case the script is similar to dump the code into the tags<script></script>
.. Logo, HREF does a relation of something external, outside the document, with something from the document, and with that, when requested, it ends up loading the styles (in the case of .css) and rather pausing the rendering for application of these.– Ale
Therefore, HREF does not interfere with Parsing, only in rendering if requested (in the case of style sheets). So if I call the style sheet by
@import
, which, from what I read, would be similar to pouring the content into the tags styles(actually import), will cause the pause in the page Parsing to apply the styles, that’s right what I’m saying?– Ale
That is, @import will cause a delay in Parsing, for the loading of the stylesheet, which was imported, even if it was not requested by DOM? Excuse me if I’m traveling in any concept!!
– Ale
@Alexandrec.Caus I think the
@import
causes a synchronous request. So I would take the<link href...>
in the main document, as Elias has already suggested.– bfavaretto
I found the title ambiguous because it doesn’t say it’s about css style sheets.. It implies that it refers to the general as <a> and <img>..
– Daniel Omine