How can I add a class to a label? I need to add a class name to certain labels created by Drupal's form API like this:

$form['name'] =&gt; array(
  '#type' =&gt; 'textfield',
  '#title' =&gt; 'Prénom'
);


If I use the following, the &lt;textarea&gt; gets a class, but not the label.

$form['name']['#attributes']['class'] = array('myClass');


I'm looking for something similar that will add a class to the &lt;label&gt;.
This post on StackOverflow says that the Drupal Form API does not support adding classes to labels, but it does contain a decent workaround:


  Instead, you could target it using the wrapper class for the field and label:

.form-item-field-foo label {
  /* CSS here */
}



The API documentation for theme_form_element_label() shows why labels aren't possible (at least not out-of-the-box): the attributes array is initialized in the theme function and populated with just two optional classes ('option' and 'element-invisible').

So, is it completely impossible? In fact, I think it can be done, but I must add that I haven't tried this myself. You could try to:


Add a new #label_attributes property to some (or all) form elements using hook_element_info_alter() in a custom module;
Override theme_form_element_label() and make it merge the $attributes array with the value of $element['#label_attributes'].


If you're going to give this a try, please let us know if it worked.