Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad practice to add extra attributes to HTML elements?

Sometimes I add an attribute to some of my controls. Like:

<a href id="myLlink" isClimber="True">Chris Sharma</a>

I know it is not a valid html. But it helps me in some cases.

Is this considered as a bad practice? A friend of mine says that it is ok for Intranet environment but on internet it might not be find friendly by search engines.

If it is not a good practice, what are the best practicess?

Thanks

like image 632
pencilCake Avatar asked Apr 30 '10 12:04

pencilCake


People also ask

Can I add any attributes to HTML elements?

If you want to define your own custom attributes in HTML, you can implement them through data-* format. * can be replaced by any of your names to specify specific data to an element and target it in CSS, JavaScript, or jQuery.

Can a HTML element have multiple data attributes?

You can have multiple data attributes on an element and be used on any HTML element.

Can elements have more than one attribute?

The multiple attribute in HTML allows user to enter more than one value. It is a Boolean attribute and can be used on <input> as well as <select> element, To allow multiple file uploads in HTML forms, use the multiple attribute. The multiple attribute works with email and file input types.


2 Answers

If you are using HTML5 doctype then you can add data attrbutes which are valid.

So something like the following will be valid

<a href id="myLlink" data-isClimber="True">Chris Sharma</a>
like image 118
rahul Avatar answered Oct 19 '22 13:10

rahul


Yes. It is considered a bad practice. Your HTML (if it's 4.0) won't validate successfully. Instead, add a class like so:

<a href id="myLlink" class="climber" >...</a>

Remember that you can have multiple classes:

<a href id="myLlink" class="climber girl pretty" >...</a>

And you can use CSS or JQuery to select out stuff based on these classes, and selectively override style based on the combinations:

a.climber             { color: brown; }
a.climber.girl        { color: red; }
a.climber.girl.pretty { color: pink; }
like image 22
Dave Markle Avatar answered Oct 19 '22 13:10

Dave Markle