0

Might be off topic I hope it isn't. I was just browsing the questions when I noticed one with something I didn't think was right.

section class="someclass" href="index.html"

The person asking got his answer but none of the people answering said anything about this line so I got interested but couldn't find anything.

Can you use the href attribute in section...? I thought you can use it on a, area, base and link. If you can use it on , what does it do? I really couldn't find anything about it...

Thanks.

the WRB
  • 13
  • 1
  • 7
  • Why not just trying it instead of asking? plain html, it takes less time to try than to ask, and less lines. http://stackoverflow.com/questions/4465923/a-href-link-for-entire-div-in-html-css – Lynob Dec 10 '16 at 09:05
  • Yep. Tried it. Nothing happened and the validator said href isn't allowed on section. My question was if you can (not meaning allowed but possible) use it and if you can (still meaning possible) what you can (possibly) use it for. Sorry if I couldn't phrase my question properly, English is not my native language. – the WRB Dec 10 '16 at 09:19
  • you can't, that's why nothing happened, a section is like a div, refer to the link above – Lynob Dec 10 '16 at 09:25

2 Answers2

1

No, it's not valid. The section element can only have the global attributes: accesskey, class, contenteditable, contextmenu, dir, draggable, dropzone, hidden, id, lang, spellcheck, style, tabindex, title, translate.

Oriol
  • 274,082
  • 63
  • 437
  • 513
1

The href attribute is indeed only a valid attribute for <a>, <area>, <base> and <link> (convenient attribute + element reference).

Now that the validity is clear, we can talk about how this could have been (ab)used (still not making it valid).

Using javascript, one could give any attribute meaning, even the invalid ones.

var section = document.querySelector('section[href]'),
    href = section.getAttribute('href');

section.innerHTML = 'Could have loaded ' + href + ' here';
<section href="index.html">
  default content
</section>

This was a rather common practise before HTML5, abusing valid attributes aswel as introducing invalid ones, simply to implement behaviour with script.

This is why as of HTML5 the W3C/WhatWG (the people who kindly write these specifications) have provided the data- attribute prefix, which allow to make up attributes which have no meaning (ever) in HTML and are always considered valid.

So, the href attribute is not valid on a <section>-element, but it could have been given some meaning using javascript, which is invalid and should have been data-href (or a name describing its purpose better).

Rogier Spieker
  • 4,087
  • 2
  • 22
  • 25