Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why not allow the href attribute in tags other than the anchor? - EG: <h1 href=""></h1>

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>
like image 323
ZZ9 Avatar asked Aug 25 '14 14:08

ZZ9


People also ask

Can I use href In other tags?

Yes, it's necessary.

What happens if you do not give the href attribute in a tag?

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!

Can we use href without anchor tag?

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.

Why is the href attribute necessary in an a tag?

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.


1 Answers

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.

like image 121
DarkDust Avatar answered Sep 18 '22 14:09

DarkDust