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

1

7

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>
Blurb answered 25/8, 2014 at 14:57 Comment(16)
In a word? Semantics.Melleta
You don't need to wrap the a around the whole div. If you're making a link, you can just do this: <p><a href="cake">This is not a lie.</a></p>.Hildegard
@ialarmedalien: Those do very different things…Issue
@Melleta Is that necessary? I can clearly see where a DIV begins and ends.Blurb
@AirCombat: Do you actually understand what the a tag and it's href property do? They create a hyperlink and you'd throw it away.Issue
Yes, it's necessary. Otherwise you could extend your argument to the extreme and say why not have just one element <element> and allow every attribute and property on it?Melleta
@Melleta That would make writing a validator so much easier! ;)Hildegard
I don't agree. P, H1, ASIDE, FOOTER all describe the content symantically, <A> does not, it is like width:100px;, it describes a property surely?Blurb
possible duplicate of Is putting a div inside an anchor ever correct?Ulna
As j08691 why should you use P, H1, ASIDE ? and not simply <ELEMENT text-group="P"> or <ELEMENT text-level="H1">. Because it wouldn't be readable. You see "A" you know it is a hyperlink, you see H1 you know is HeaderMelone
No, it doesn't describe a property, because only a elements, i.e. hyperlinks, are interpreted by browsers as being links.Hildegard
@MohammadAreebSiddiqui I'm not asking if it's incorrent, we know this. Im just interested in the logic behind why that decision was takenBlurb
@AirCombat You might email someone from the w3c staff and get your answer.Ulna
Tim Berners-Lee formulated the original HTML spec (see w3.org/History/19921103-hypertext/hypertext/WWW/MarkUp/…), so ask him.Hildegard
So I shouldn't ask questions on Stack Overflow when I can contact a 3rd party?Blurb
StackOverflow is specifically for programming problems; this isn't a programming problem--it's a conceptual one.Hildegard
I
7

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.

Issue answered 25/8, 2014 at 15:14 Comment(2)
A lot of our customers like to right click links and open in a new tab/window in a second monitor. We also like entire rows to be clickable to open a record (especially if a custom column layout omits columns with any anchor tags). A great solution would be to support the href attribute on other tags such as TR or DIV. But in order to support right click open and clickable rows simultaneously, you have to jump through a lot of hoops. Especially when you want to change those links on the fly.Pregnant
Did you find a solution for your case?Hydria

© 2022 - 2024 — McMap. All rights reserved.