How to convert UPPERCASE text to Title Case using CSS


Tags: css

Problem :

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.



Solution :

The bad news is that there is no such thing as text-transform : title-case which 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() and ucwords() to convert the relevant text to title case.

BEFORE (THIS DOESN'T WORK):

<style>
.title-case{ text-transform:capitalize; }
</style>
<span class="title-case">ORIGINAL TEXT</span>

AFTER:

<?php echo ucwords( strtolower('ORIGINAL TEXT') ); ?>

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:

<?php
$title = get_the_title();
$title = strtolower($title);
$title = ucwords($title); 
<h1>
<?php echo $title;
</h1> 
?>

Hope this helps someone. Happy coding.


    CSS Howto..

    how to create a css attribute swapper between two elements?

    How to keep two divs and images inside them to be side by side on browser resize?

    How to get 2 fixed and 1 dynamic column in CSS

    How to use CSS with JS [duplicate]

    How to count the number of correct answers using Javascript?

    How to reserve a DIV width with “left:100%” CSS rule

    How to generate unique css selector for DOM element?

    How to change a css element based on other element

    How to create offer tag with css and pass value to it

    how to implement “blog” page in HTML/CSS? [closed]

    How to disable flow around elements in table (CSS)

    How did Google make these 3d buttons? [closed]

    How to make scrollbars appear when DIV doesn't have enough room but keep DIV centered?

    How to manage z-index of navigation menu?

    How to inherit photo when hover on links under navigation bar?

    How to add offset border to image of unknown size?

    How to scale content for displays keeping the ratio [closed]

    How to implement a drag and swap effect in CSS and JS [closed]

    CSS how to use pseudo-class :not with :nth-child

    How to create a vertical column in a table using css

    CSS frustration: How to center floating images of different widths inside LI tags?

    How to change border of div containing radio button when checked

    How do I get this CSS/jQuery menu to open only on click, rather than hover?

    How to split page and reveal stuff underneath?

    How can I stay organized when writing CSS? [duplicate]

    How to hide a column in the Webgrid in aspasp.net MVC?

    How to make first letter lowercase in span with css?

    How can I stop my submenu from disappearing while hovering between spaced submenu items?

    How do I keep a CSS hover rule applied when the mouse moves to the child element?

    How to set up a correct cascade with label statement