Every label of every element of every list has trailing space, and it is annoying and makes styling sometimes hard or in certain ways impossible. I was trying to add :before pseudo element to label with content: "\0020";, to make trailing spaces symmetrical at least, but it doesn't work. How to get rid of them?


Yeah, this is a known issue. The form.inc file from /includes/form.inc which themes form labels outputs the following for labels:

$t('!title !required', array('!title' => $title, '!required' => $required))

The offending space is the space between '!title' and '!required', where !title will contain the actual label text and !required will contain a '*' for fields that are required to be filled in the form...

In any case, this is all in core, and there is an initiative to fix it for Drupal 8: https://drupal.org/node/1623574. Definitely follow that or add your voice to the mix! Any fix will not be back-ported to Drupal 7, however, because lots of themes rely on that extra space and will be messed up if it is removed.

What you can do in your own case, though, is override the core hook_form_element_label theme function.

So, in your custom theme, just create the necessary function in your template.php file:

function YOURTHEMENAME_form_element_label($variables)

Now just copy the entire function from /includes/form.inc and just change the very last return statement to not include the space unless there is a required star ('*') to go after the label. Your new and improved return should be:

return ' <label' . drupal_attributes($attributes) . '>' . $t('!title!required', array('!title' => $title, '!required' => $required ? ' ' . $required : '')) . "</label>\n";

That should do it!! However, make sure you click around your site and check whether this messes anything else up before going in production. You may see some labels mashed into other stuff without the space, again because some themes have relied on it! And make sure to clear cache so the new function is picked up.

Let us know if it works...

  • That works! thank you very much and I'm certainly following this initiative for Drupal 8. :) – Łukasz Zaroda May 15 '13 at 9:29

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.