1

Size attributes just get in the way of responsive forms, and I realize most form fields can be overridden by CSS, but field types like input[type="file"] still do not behave well if a size attribute is set.

See this JSFiddle example.

Since field elements are classed out well, I'd much rather purely rely on CSS for field sizes. So, how can size attributes be removed from all fields?

5

For text fields the size attribute is forcibly added in theme_textfield(), so the only way would be to override that function in your theme and remove that bit of code:

function mytheme_textfield($variables) {
  $element = $variables['element'];
  $element['#attributes']['type'] = 'text';

  // 'size' removed from this call
  element_set_attributes($element, array('id', 'name', 'value', 'maxlength'));

  // add the rest of the function...

Exactly the same thing goes for the file element (see theme_file()), and most likely for any other element type you need to do do this for.

3
  • this removes my input fields completely skitch.com/krisbulman/epnd5/admin-d7.kgbdev.local
    – krisbulman
    Aug 16 '12 at 18:31
  • Did you copy the rest of the function across? All the way down to return $output . $extra;?
    – Clive
    Aug 16 '12 at 18:31
  • my bad, I see you have to use the whole function now.. and it can be isolated on any field type. works like a charm.
    – krisbulman
    Aug 16 '12 at 18:37
2

You could do it with jQuery on the browser side like this, too:

$('input').removeAttr('size');
0

input[type="file"] does not have size attribute, see http://www.w3schools.com/tags/att_input_size.asp

For D6, just add next lines to template.php (copied from includes/form.inc and removed size):

function phptemplate_file($element) {
    _form_set_class($element, array('form-file'));
    return theme('form_element', $element, '<input type="file" name="'. $element['#name'] 
        .'"'. ($element['#attributes'] ? ' '. drupal_attributes($element['#attributes']) : '')
        .' id="'. $element['#id'] ."\" />\n");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.