How to Capitalize first letter only using CSS in each case
Asked Answered
H

5

17

I want to Capitalize first letter only and other should be small using CSS

String is:

SOMETHING BETTER 
sOMETHING bETTER
Something better

but the result should be

Something Better

Is this possible using CSS? To Capitalize first letter I am using

text-transform: capitalize;

But not able to capitalize in each case. "I want to use CSS because in my application it has written every where hard coded but a class has been called everywhere."

Haemostatic answered 3/7, 2013 at 14:12 Comment(4)
Not possible with CSS alone. Even trying to cascade a text-transform: lowercase into a text-transform: capitalize won't work. If you want to use javascript, here's a link to an answer for that: #197472Frediafredie
In PHP - ucwords("UPPERCASE WORD HERE");, and the result : Uppercase Word Here" - php.net/manual/en/function.ucwords.php You can't do it using just CSS, because you can't do lowercase and then capitalize.Kew
possible duplicate of make the first character uppercase in cssRowles
@yoeriboven: I don't think it's the same.Levison
C
40

you should be able to use the :first-letter pseudo element:

.fl {
 display: inline-block;
}

.fl:first-letter {
 text-transform:uppercase;
}

<p>
 <span class="fl">something</span> <span class="fl">better</span>
</p>

yields:

Something Better

Cyclopentane answered 3/7, 2013 at 14:28 Comment(2)
This is correct, but means each word needs to be wrapped in a span, which is uglyRigmarole
Added some javascript magic ^_^ @CyclopentaneGastrotomy
P
8

It is not possible with CSS alone but you can do it with Javascript or PHP for example.

In PHP

ucwords()

And in Javascript

function toTitleCase(str){
    return str.replace(/\w\S*/g, function(txt){
        return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

Extracted from Convert string to title case with JavaScript

Polyptych answered 3/7, 2013 at 14:20 Comment(0)
G
7

You can try a combination of this answer and some javascript (using jQuery)

HTML:

<div class='capitalize'>
    SOMETHING BETTER 
    SOMETHING BETTER 
    SOMETHING BETTER 
</div>

JAVASCRIPT:

$('.capitalize').each(function(){
    var text = this.innerText;
    var words = text.split(" ");
    var spans = [];
    var _this = $(this);
    this.innerHTML = "";
    words.forEach(function(word, index){
        _this.append($('<span>', {text: word}));
    });
});

CSS:

.capitalize {
    text-transform: lowercase;
}

.capitalize span {
    display: inline-block;
    padding-right: 1em  
}

.capitalize span:first-letter {
    text-transform: uppercase !important;
}

Demo: http://jsfiddle.net/maniator/ZHhqj/

Gastrotomy answered 3/7, 2013 at 14:42 Comment(0)
L
4

Why dont you just use the :first-letter pseudo element in css?

h2:first-letter{
text-transform: uppercase;

}

h2{

*your general code for h2 goes here;*

}
Lornalorne answered 11/8, 2015 at 8:50 Comment(0)
G
-1

Yes, CSS is no help here. Welcome to the world of JavaScript, where anything is possible.

window.onload = function(){
  var elements = document.getElementsByClassName("each-word")
  for (var i=0; i<elements.length; i++){
    elements[i].innerHTML = elements[i].innerHTML.replace(/\\b([a-z])([a-z]+)?\\b/gim, "<span class='first-letter'>$1</span>$2")
  }
}



  .first-letter {
    color: red;
  }



<p class="each-word">First letter of every word is now red!</p>
Guntar answered 1/12, 2015 at 7:53 Comment(1)
Wrong, it's completely possible in CSS. Javascript is overkill.Sordid

© 2022 - 2024 — McMap. All rights reserved.