I've just been going through a my current project and removing excess html and css where not needed, removing divs around abjects that didn't need a div for example and I was wondering, why do we need achors around everything:
<a href="Cake">
<div>
<p>This is not a lie.</p>
</div>
</a>
Why not allow simply allow:
<div href="Cake">
<p>This is not a lie.</p>
</div>
Yes, it's necessary.
The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!
Yes, it is valid to use the anchor tag without a href attribute. If the a element has no href attribute, then the element represents a placeholder for where a link might otherwise have been placed, if it had been relevant, consisting of just the element's contents.
The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address. Content within each <a> should indicate the link's destination.
I'm overly simplifying here, but it's about the concepts.
In HTML, every tag has a meaning and a function. Sometimes it's purely aesthetics (<b>
, bold text), sometimes it's semantics (<span>
, a new chunk of inline text), sometimes both (<h1>
, a header starts here). The point is to mark portions of text with semantics.
The <a>
tag is a hyperlink. Almost everything between <a>
and </a>
is supposed to behave like this: if clicked, jump to certain URL. Now that URL is a property of the <a>
tag and you specify it via href
.
This href
does not make any sense outside of the <a>
tag since back when HTML was born, CSS and JavaScript didn't exist so you couldn't do things like making a <span>
clickable like we can today. But even today, it doesn't make sense to allow elements to adopt functionality of other elements as it taints their semantics.
In your second example you have completely removed the <a>
tag, so it isn't a link any more. Even if the parser allows href
on the div
, it'd be wrong to suddenly make it behave like an a
tag since it isn't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With