If you’re reading this you probably noticed that the CSS property text-transform:capitalize; does not convert THIS into This. Instead the, non-initial characters remain capitalized, so the transformation has no effect in this case. So how can we achieve this result?
I’ve seen this asked often and most answers are quick to promote using javascript to accomplish this. This will work, but it is unnecessary if you are writing or customizing a template/theme for a PHP CMS like WordPress, Drupal, or Joomla.
The bad news is that there is no such thing as
text-transform : title-casewhich would guarantee the result to be title cased. The good news is that there IS a way to do it, which doesn’t require javascript (as is often suggested for this situation). If you are writing a theme for a CMS you can use strtolower() anducwords()to convert the relevant text to title case.BEFORE (THIS DOESN’T WORK):
AFTER:
If you are writing a theme, you’ll probably be working with variables instead of text strings, but the function and the concept work the same way. Here’s an example using the native WordPress function get_the_title() to return the page title as a variable:
Hope this helps someone. Happy coding.