How to Capitalize Words using CSS
Published: Sunday, May 15, 2022
Greetings, friends! In one of my previous articles, I discussed how to capitalize strings using JavaScript. In this article, we'll learn how to use CSS to capitalize words.
First, Create an index.html
file with the following contents:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Capitalization with CSS</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>capitalize me!</h1>
</body>
</html>
tip
If you don't feel like creating new files when following along this tutorial, you can use JS Bin instead!
When you open the index.html
file, you should see a page with the following text: "capitalize me!" Suppose we want to capitalize these words using CSS instead of JavaScript.
Create a style.css
file with the following contents:
css
h1 {
text-transform: capitalize;
}
If you refresh the page or reopen the index.html
file, you should see "Capitalize Me!" instead. Each word in the h1
element has been capitalized. Note that the text-transform
property will affect each word in the element.