views:

73

answers:

4

Here is my HTML:

<a href="#" class="link">small caps</a> & 
<a href="#" class="link">ALL CAPS</a>

Here is my css:

.link {text-transform: capitalize;}

The output is:

Small Caps & ALL CAPS

and I want the output to be:

Small Caps & All Caps

Any ideas?

A: 

Sorry, pretty sure it's impossible to get what you want without JavaScript.

Ryan Kinal
+1  A: 

captialize only effects the first letter of the word. http://www.w3.org/TR/CSS21/text.html#propdef-text-transform

prodigitalson
+1  A: 

Interesting question!

capitalize transforms every first letter of a word to uppercase, but it does not transform the other letters to lowercase. Not even the :first-letter pseudo-class will cut it (because it applies to the first letter of each element, not each word), and I can't see a way of combining lowercase and capitalize to get the desired outcome.

So as far as I can see, this is indeed impossible to do with CSS.

@Harmen shows good-looking PHP and jQuery workarounds in his answer.

Pekka
Strange, I expected adding 'a {text-transform:lowercase}' would work. But it doesn't.
Kwebble
+3  A: 

There is no way to do this with CSS, you could use PHP or Javascript for this.

PHP example:

$text = "ALL CAPS";
$text = ucwords(strtolower($text)); // All Caps

jQuery example (it's a plugin now!):

// Uppercase every first letter of a word
jQuery.fn.ucwords = function() {
  return this.each(function(){
    var val = $(this).text(), newVal = '';
    val = val.split(' ');

    for(var c=0; c < val.length; c++) {
      newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + (c+1==val.length ? '' : ' ');
    }
    $(this).text(newVal);
  });
}

$('a.link').ucwords();​
Harmen
Yup, probably the only way(s) to do this.
Pekka
Maybe its better with regular expressions instead of this loop + substrings -- But how can you uppercase a letter in Javascript's regular expressions?
Harmen
If you're going to do it server-side, you could lowercase the whole string then let CSS do the capitalize. Just a thought.
Stephen P
I ended up doing a .toLowerCase() on the whole string, then using CSS to capitalize. Thanks for the advice!
Khan