Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it poor form to use CLASS attributes with no corresponding CSS rule? [duplicate]

For example if I wanted to select some elements with a certain class using jQuery, and for that reason only, is it always expected that those classes SHOULD be defined in the css?.

<div class="xyz">
something
</div>

<div class="xyz">
something else
</div>

//with example jQuery
$(".xyz").hide();

//is it wrong no element 'xyz' is defined in css?
like image 836
CRice Avatar asked May 14 '10 05:05

CRice


2 Answers

This is perfectly acceptable. As client side processing is becoming more common place (via jQuery, ExtJS, Prototype, etc), it makes sense to create efficient code that can use the powerful selector support provided by these libraries.

like image 166
Mike Trpcic Avatar answered Oct 13 '22 19:10

Mike Trpcic


Using CSS classes and - depending on the case - IDs (unique!!) is perfectly fine and often the only solution to keep your HTML code valid while giving additional attributes used by scripts.

While using non-standard attributes often works fine, too, it will prevent your html code from validating and might cause issues in the future.

However, for some cases there might be attriutes which can be used, too - like rel on links which is often used for scripts which modify link behaviour.

http://www.w3.org/TR/html401/struct/global.html#h-7.5.2 also mentions that class is general-purpose:

The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:

  • As a style sheet selector (when an author wishes to assign style information to a set of elements).
  • For general purpose processing by user agents.
like image 23
ThiefMaster Avatar answered Oct 13 '22 19:10

ThiefMaster