Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML "data-attribute" vs simple "custom attribute"

I usually saw html data-attribute(s) to add specific values/parameters to html element like Bootstrap that uses them to "link" buttons to modal dialog to be opened, etc.

<button type="button" class="close" data-dismiss="modal" aria-label="Close">

Now, I see that an almost famous CSS framework, Kube, in its new version extensively uses simple custom attribute such as in following:

<column cols="4">4</column> 

<span class="label label-black" outline bold>Black</span>

Other in-action examples are visibile for example, here.

I did not know that was possible to use simple custom attributes so I tried to search some source about this, and I found only this old similar question in which are noted almost only (possible) compatiblity issues.

I'm surprised that a CSS framework like Kube could use a similar solution if browser support could be so "fragile"....

So my question are:

  1. How good (=cross-compatible) is Kube's approach?
  2. Can I safely replace my data-attribute with simple custom ones if for example I have to pass only true/false values?

This last question is better described by an example, so replace <span class="foo" data-boo='true'>Black</span> with <span class="foo" boo>Black</span>

like image 418
Luca Detomi Avatar asked Oct 15 '15 14:10

Luca Detomi


People also ask

What are the 3 types of attribute in HTML?

HTML attributes are generally classified as required attributes, optional attributes, standard attributes, and event attributes: Usually the required and optional attributes modify specific HTML elements.

What are custom data attributes in HTML?

Custom attributes are attributes that are not part of the standard HTML5 attributes but are explicitly created. They allow us to add our own information to HTML tags. These are not specific and can be used with all the HTML elements.

Can you use custom HTML attributes?

You can add custom HTML attributes to pages and page elements, which are rendered on the HTML tag of the page element. For example, this is useful when working with third-party frameworks that render page elements differently based on certain attributes.

What does custom attribute mean?

Custom attributes. A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.


1 Answers

Use data attributes. They are:

  • standard
  • won't make a validator report them as errors (it's hard to spot unintentional errors in a validation report when they are surrounded with a pile of errors that you've made intentionally)
  • won't conflict with attributes that might be added to the standard in the future

… and you don't have to give them values if you just want to check to see if they exist with a CSS attribute selector … but if you want that then you should probably just be using additional classes instead.

like image 193
Quentin Avatar answered Oct 08 '22 09:10

Quentin