Home > Web Front-end > CSS Tutorial > How Can I Use `text-transform: capitalize` Without Affecting All-Caps Words?

How Can I Use `text-transform: capitalize` Without Affecting All-Caps Words?

Susan Sarandon
Release: 2024-11-17 16:21:01
Original
709 people have browsed it

How Can I Use `text-transform: capitalize` Without Affecting All-Caps Words?

Overriding CSS text-transform: Capitalize for All Caps Text

Problem:

Utilizing CSS's text-transform: capitalize; rule to convert all-caps text to sentence case renders problematic when the initial letters of such words inherently require uppercase, such as proper nouns or acronyms.

As demonstrated in the following code snippet, the desired output of "Small Caps & All Caps" is not achieved:

HTML:

<a href="#" class="link">small caps</a> &amp; 
<a href="#" class="link">ALL CAPS</a>
Copy after login

CSS:

.link {text-transform: capitalize;}
Copy after login

Result:

Small Caps & ALL CAPS
Copy after login

Solution:

To achieve the desired capitalization behavior while maintaining the text-transform: capitalize; rule, the following CSS can be implemented:

.link {
  text-transform: lowercase;
}
.link:first-letter,
.link:first-line {
  text-transform: uppercase;
}
Copy after login

This modification transforms all text to lowercase initially, then re-capitalizes the first letter of each word and the first line of text.

Result:

Small Caps
All Caps
Copy after login

The above is the detailed content of How Can I Use `text-transform: capitalize` Without Affecting All-Caps Words?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template