31

I have an example text and it all Uppercase. I want to make it capitalized text, but css text-transform doesn't work. How can i do this?

span,
a,
h2 {
  text-transform: capitalize !important;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<span class="ml-5 mt-5 d-block">IT IS AN EXAMPLE TEXT</span>

<h2><a class="ml-5 mt-5 d-block" href="#">IT IS AN EXAMPLE TEXT IN HREF</a></h2>

And you can check in JSFiddle

I want first letter Upper, others lower for all words. Like this:

It Is An Example Text
wpuzman
  • 343
  • 1
  • 3
  • 9
  • So you want what? The text - except for the initial letter (or the first letter) - to be lowercase? "...doesn't work" is not an adequate description of the problem; what exactly did you expect, what happened instead? – David Thomas Sep 04 '18 at 11:46
  • Edited question. Sory. – wpuzman Sep 04 '18 at 11:50
  • 1
    This isn't something CSS can do - since the text is all uppercase already, so capitalising the first letter of each word makes no visual difference (this is probably by design, but it honestly feels like a bug), and there's no `:word` pseudo-class - is JavaScript a possibility, or does it have to be CSS only? – David Thomas Sep 04 '18 at 11:53
  • No it can be with javascript and php. @DavidThomas – wpuzman Sep 04 '18 at 12:03

6 Answers6

37

Capitalize only affects the first letters of words. It will not change the case of the rest of the letters in a word. For example, if you capitalize a word that's in all capital letters already, the other letters in the word won't switch to lowercase. This is nice when your text includes an acronym or abbreviation that shouldn't include any lowercase letters.

If you change your provided text to lowercase, you will see it work just fine!

But to be answer complete, i will try to find a way to capitalize with the desired way no matter the input.

Seems it is not possible with CSS only, for something beside a single word text if you are not wrapping every word in a different element.

Single word snippet

span,
a,
h2 {
   text-transform:lowercase
}

span:first-letter,
a:first-letter,
h2:first-letter {
  text-transform: capitalize;
}
vfle
  • 1,355
  • 7
  • 18
22

CSS text-transform: capitalize will only affect the first character.

According to the specs:

capitalize Puts the first character of each word in uppercase; other characters are unaffected.

I strongly suggest to just have the content edited. Remember that at the end of the day, technologies such as css is just 1 way to solve the problem. The better solution, again, would be to edit that content and capitalize it, whether it be stored in the database or just static content in the markup.


As a hack (if you really want to use a code-level solution), you can use JavaScript to transform all letters first into lowercase. Then use /\b(\w)/g to match the instances of first letters, then use toUpperCase() on each

document.querySelector("a").innerText = document.querySelector("a").innerText.toLowerCase().replace(/\b(\w)/g, x => x.toUpperCase());
<h2><a class="ml-5 mt-5 d-block" href="#">IT IS AN EXAMPLE TEXT IN HREF</a></h2>
95faf8e76605e973
  • 13,643
  • 3
  • 24
  • 51
5

span,
a,
h2 {
  text-transform: lowercase !important;
}
span:first-letter,
a:first-letter,
h2:first-letter {
  text-transform: capitalize !important;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<span class="ml-5 mt-5 d-block">IT IS AN EXAMPLE TEXT</span>

<h2><a class="ml-5 mt-5 d-block" href="#">IT IS AN EXAMPLE TEXT IN HREF</a></h2>
yjs
  • 692
  • 3
  • 11
5

I edited their answer to apply capitalization to all the first letter of the first line.

span,
a,
h2 {
  text-transform: lowercase;
}
span:first-line,
a:first-line,
h2:first-line {
  text-transform: capitalize;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<span class="ml-5 mt-5 d-block">IT IS AN EXAMPLE TEXT</span>

<h2><a class="ml-5 mt-5 d-block" href="#">IT IS AN EXAMPLE TEXT IN HREF</a></h2>
Mystery
  • 1,031
  • 8
  • 15
2

My solution is to use JavaScript and CSS together.

CSS :

#text { text-transform: capitalize} 

Javascript:

document.getElementById("text").innerHtml = document.getElementById("text").innerHtml.toLowerCase()

The JavaScript will first of all force everything to lowercase, then the CSS will apply the text-capitalize.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Benito
  • 181
  • 1
  • 9
1

To expand upon @Mystery's answer. In reference to Why is ::first-line not working for span like p / div tags?

As per MDN:

A first line has only meaning in a block-container box, therefore the ::first-line pseudo-element has only an effect on elements with a display value of block, inline-block, table-cell or table-caption. In all other cases, ::first-line has no effect.

Below is the extract from the W3C Spec: (Section 7.1.1 First formatted line definition in CSS)

In CSS, the ::first-line pseudo-element can only have an effect when attached to a block-like container such as a block box, inline-block, table-caption, or table-cell.

Since span elements are display: inline by default the ::first-line selector has no effect on it. If we change the display for the span to inline-block or block, it will work.

span,
a,
h2 {
    display: block;
    text-transform: lowercase;
}
span:first-line,
a:first-line,
h2:first-line {
    display: block;
    text-transform: capitalize;
}
tralawar
  • 506
  • 6
  • 12