How to Capitalize the First Letter of Each Word in CSS

CSS Capitalize First Letter In All Caps Word

Yes it is possible with just CSS, here's how:

.capitalize {

text-transform: lowercase;

display: inline-block;

}

.capitalize:first-letter {

text-transform: uppercase

}
<span class = "capitalize">TESTING</span>

How can I capitalize the first letter of each word in a string using JavaScript?

You are not assigning your changes to the array again, so all your efforts are in vain. Try this:

function titleCase(str) {

var splitStr = str.toLowerCase().split(' ');

for (var i = 0; i < splitStr.length; i++) {

// You do not need to check if i is larger than splitStr length, as your for does that for you

// Assign it back to the array

splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);

}

// Directly return the joined string

return splitStr.join(' ');

}

document.write(titleCase("I'm a little tea pot"));

Capitalize first letter of sentences CSS

You can capitalize the first letter of the .qcont element by using the pseudo-element :first-letter.

.qcont:first-letter{
text-transform: capitalize
}

This is the closest you're gonna get using only css. You could use javascript (in combination with jQuery) to wrap each letter which comes after a period (or comma, like you wish) in a span. You could add the same css as above to that span. Or do it in javascript all together.

Here's a snippet for the css approach:

.qcont:first-letter {

text-transform: capitalize

}
<p class="qcont">word, another word</p>

First letter Capitalize and other letters in lower case in css?

You could use text-transform in order to make each word of a paragraph capitalized, as follows:

p { text-transform: capitalize; }

It's supported in IE4+. Example Here.

16.5 Capitalization: the 'text-transform' property

This property controls capitalization effects of an element's text.

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


Making each word of an uppercase text, capitalized:

The following was under this assumption:

I want to make it look like: This Is Sometext

You have to wrap each word by a wrapper element like <span> and use :first-letter pseudo element in order to transform the first letter of each word:

<p>
<span>THIS</span> <span>IS</span> <span>SOMETEXT</span>
</p>
p { text-transform: lowercase; }    /* Make all letters lowercase */
p > span { display: inline-block; } /* :first-letter is applicable to blocks */

p > span:first-letter {
text-transform: uppercase; /* Make the first letters uppercase */
}

Example Here.

Alternatively, you could use JavaScript to wrap each word by a <span> element:

var words = $("p").text().split(" ");
$("p").empty();

$.each(words, function(i, v) {
$("p").append($("<span>").text(v)).append(" ");
});

Example Here.


Making the first letter of an uppercase text, capitalized:

This seems to be what you are really looking for, that's pretty simple, all you need to do is making all words lowercase and then transforming the first letter of the paragraph to uppercase:

p { text-transform: lowercase; }

p:first-letter {
text-transform: uppercase;
}

Example Here.

target first letter of each word in css

You should wrap every single word with a tag and use ::first-letter CSS selector .

Also, note that this selector does not work on inline elements. If you want to use it with an inline element, such a <span>, make sure you set display:inline-block (see here for more details: https://stackoverflow.com/a/7631782/11298742)

example :

p span { display: inline-block; font-family: 'Roboto', sans-serif }

p span::first-letter {

color: red;

font-weight: bold;

}
<p><span>Lorem</span> <span>ipsum</span> <span>dolor</span> <span>sit</span> <span>amet</span></p>


Related Topics



Leave a reply



Submit