I want to add css class to a field inside hook_field_widget_form_alter, but it doesn't work! in this link
https://api.drupal.org/api/drupal/modules!field!field.api.php/function/hook_field_widget_form_alter/7

it describes that we can add css class to an element in a form via this code

function hook_field_widget_form_alter(&amp;$element, &amp;$form_state, $context) {
  // Add a css class to widget form elements for all fields of type mytype.
  if ($context['field']['type'] == 'mytype') {
    // Be sure not to overwrite existing attributes.
    $element['#attributes']['class'][] = 'myclass';
  }
}


I tried to use it but it seems $element['#attributes']['class'][] = 'myclass'; doesn't add any css class to form after rendering.

So how can I add css class to a form element inside hook_field_widget_form_alter() ?
Old issue but it might help future viewers.
Try this:
$element['value']['#attributes']['class'] = 'myclass';

See field_multiple_value_form() which creates the render array.
Then main theme function for rendering fields in forms is theme_field_multiple_value_form() and the parent theme function is theme_form_element()