Home >Web Front-end >CSS Tutorial >Can CSS and JavaScript Capitalize Text After Converting it to Lowercase?
Text Transformation in CSS and JavaScript
Can CSS facilitate the task of first converting text to lowercase and then capitalizing it?
Solution in CSS
Certainly, using CSS's text-transform property, you can achieve this transformation:
<code class="css">.className { text-transform: capitalize; }</code>
This will convert all the text within elements with the className class to lowercase and capitalize the first letter of each word.
Solution in JavaScript
JavaScript also offers a solution through the capitalize() function, which you can define as follows:
<code class="javascript">function capitalize(s){ return s.toLowerCase().replace( /\b./g, function(a){ return a.toUpperCase(); } ); };</code>
When applied to a string, such as capitalize('this IS THE wOrst string eVeR'), this function converts it to this Is The WoRsT String EveR.
The above is the detailed content of Can CSS and JavaScript Capitalize Text After Converting it to Lowercase?. For more information, please follow other related articles on the PHP Chinese website!