Add css property for individual components in form elements - Drupal

17k views Asked by At

I have a drupal form and I need to add separate classes to the components in a form element.

That is I need to add the attribute class='text' to the 'textbox (the actual box)' and class='title' to the 'textbox title' (#title').

How do I do this?

2

There are 2 answers

0
Nikit On BEST ANSWER

Someone already answer to your question Remove class=“form-item” from Drupal forms.
That function give possibility add class='title' to the title.
But for class='text' you should theme_textfield (or theme_textarea) to own template.php
Take codes from ./included/form.inc
But i recommend to do theming required custom form, not all form elements. Somewhere it can raise styling conflicts etc. Of course, if client doesn't demand change all form elements. In real case changing default elements doesn't need.

1
AudioBubble On

For the first case—adding the class to the textfield itself—use the #attributes property:

$form['foo'] = array(
  '#type' => 'textfield',
  '#title' => t('Textbox title'),
  '#attributes' => array(
    'class' => array('text'), // change to just 'text' for Drupal 6
  ),
);

For the second case——adding the class to the label—you are out of luck with the Forms API. Instead, you could target it using the wrapper class for the field and label:

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