href vs. src
I’ve always been struggling to remember a lot of the HTML attributes for various tags, but probably the one I get most frequently wrong is
<link src="styles.css" />. Or was is
<link href="styles.css" />? 😉
When I saw this post on dev.to, I realised I never really thought about if there was a system or pattern behind it, explaining the usage of
hrefvs
src.
But it turns out there is at least some logic behind it.
To sum it up, both are references to resources, but the
srcattribute is used for resources that are automatically loaded, while the
hrefattribute is used for resources that are dynamically loaded through a trigger, e.g. a user click in case of the anchor tag.
But, I know what you think. The
linktag “statically” references a resource but uses the
hrefattribute, so what’s up with that? That’s where the inconsistency comes in 😅
So, considering all I’m not sure how useful this little piece of knowledge will be, but it generally always helps me to understand the reasoning behind why certain things are the way they are 🤓