AlexisWbr
AlexisWbr

Reputation: 451

Can I create my own HTML5 attribute?

I have created a navbar with Jquery and HTML5, and for usability I decided to get a variable directly in the HTML like that

<nav class="mainNav" breakpoint="768">

I saw it is needed to add "data-" before my custom attribute. So the question is, is it valid to process like this, and is there a problem with XHTML/XML ? Is it a problem if the XHTML/XML is not valid ?

Thanks.

Upvotes: 1

Views: 65

Answers (2)

Cyril Duchon-Doris
Cyril Duchon-Doris

Reputation: 13949

Well, you "can" go this way. It will make your HTML invalid, but in practice, it's not really a big deal, only purists will complain, and most browser will just render the page normally. Also it's not really clear what you want. Do you just want it to be HTML valid, or also XHTML valid ? XHTML is probably the biggest failure of W3C, so if you just need to make a classic webpage, html5 compliance is more than enough.

However, the "data-" is now in the HTML specifications. So I encourage you to use this. Not only your page will become truly valid, but you'll also be able to use convenience methods like document.getElementById('myID').dataset.breakpoint !

Link to MDN

EDIT Whatever you do, you can use The W3C Validator to check error/ get tips

Upvotes: -1

Blago Eres
Blago Eres

Reputation: 1348

No it's not. You can use like data-breakpoint="768" but no like breakpoint="768"

NOTE: It is not only about standards and rules, HTML parser will need to work much harder if he deals with something that is not standards compliant.

Upvotes: 4

Related Questions