How do I shorten the length of all form input text fields? I'm using Drupal 7, for which the standard length of an input text field is 60. Is there any way to shorten this for ALL fields at once? I want all the forms on my site to be no wider than 40.

I know that hook_form_alter() can be used to do this one-by-one, but I have a rather large number of fields on my site, and I would like to edit their lengths all at once if possible.
There are a couple of things you can do depending on whether you want to do this on a theme level or site-wide level (i.e. independent of theme).

On the theme level you can override theme_textfield() and set the size there:

function MYTHEME_textfield($variables) {
  $new_size = 40;
  $variables['element']['#attributes']['size'] = $new_size;

  // EDITED per kiamlaluno's comment regarding infinite loops
  return theme_textfield(array('element' =&gt; $variables['element']));
}


On a site-wide level you can write a small module and implement hook_element_info_alter() to change the default that's provided by system_element_info():

function MYMODULE_element_info_alter(&amp;$type) {
  $new_size = 40;
  $type['textfield']['#size'] = $new_size;
}


You'll want to clear Drupal's cache once you've implemented either of these methods to clear the relevant registry.

It's worth noting that if you use the first method any specific sizes for textfields set by other modules (e.g. size 10 for numeric fields) will be overwritten with your new size. If you use the second method that will become the default but other modules will still be able to provide a specific size for specific textfields.

EDIT

I've just had another thought on this, probably the best way is to do it is in your theme function and check the existing length. That would match your statement "I want all the forms on my site to be no wider than 40" a bit better:

function MYTHEME_textfield($variables) {
  $new_size = 40;
  if (isset($variables['element']['#attributes']['size']) &amp;&amp; $variables['element']['#attributes']['size'] &gt; $new_size) {
    $variables['element']['#attributes']['size'] = $new_size;
  }

  // EDITED per kiamlaluno's comment regarding infinite loops
  return theme_textfield(array('element' =&gt; $variables['element']));
}