How to Make the First Character Uppercase in CSS

Avatar

By squashlabs, Last Updated: Oct. 6, 2023

How to Make the First Character Uppercase in CSS

To make the first character of a text string uppercase in CSS, you can use the ::first-letter pseudo-element. This pseudo-element allows you to select and style the first letter of an element.

Here are two possible approaches to achieve this:

Approach 1: Using the ::first-letter Pseudo-element

You can use the ::first-letter pseudo-element to select and style the first letter of an element. Here's an example of how to use it to make the first character uppercase:

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

In this example, the ::first-letter pseudo-element is applied to the <p> element. The text-transform: uppercase; property is used to transform the text to uppercase.

Related Article: How to Vertically Center Text with CSS

Approach 2: Using the text-transform Property

Another approach is to use the text-transform property with the value of capitalize. This property can be applied directly to the element or to a specific class or ID selector. Here's an example:

p {
  text-transform: capitalize;
}

In this example, the text-transform: capitalize; property is applied to the <p> element. This property capitalizes the first character of each word in the text.

Additional Suggestions and Best Practices

Related Article: How to Implement an Onclick Effect in CSS

- The ::first-letter pseudo-element is more specific and allows you to apply more specific styles to the first letter, such as color, font size, or text decoration.

- If you want to apply the same uppercase style to multiple elements, you can use a class or ID selector instead of the element selector. For example:

  .uppercase {
    text-transform: uppercase;
  }

Then, apply the class to the desired elements:

  <p class="uppercase">This is an example.</p>
  <h1 class="uppercase">Heading</h1>

- Keep in mind that the text-transform property only affects the visual rendering of the text and does not change the underlying text content. If you need to transform the actual text content, you may need to use JavaScript or server-side scripting.

More Articles from the Cascading Style Sheets (CSS) Guide series:

How To Set the CSS Background Opacity Property

Adjusting the opacity of CSS backgrounds is made easy with these two methods. Learn how to use RGBA color values or the CSS opacity property to achie… read more

How to Horizontally Center a Div in CSS

Aligning a div to the middle width of a webpage using CSS can be achieved in a few different ways. One method is to use the "margin: auto;" property,… read more

How to Center a Div in a Bootstrap Responsive Page

Centering a div in a Bootstrap responsive page can be achieved through various methods. This article provides a concise guide on how to accomplish th… read more

How To Troubleshoot CSS Position Sticky Not Working

CSS position sticky is a powerful property that allows elements to stick to a specific position as the user scrolls. However, it may not always work … read more

How to Center an Image in CSS

Learn how to center an image in CSS using easy-to-follow steps. This tutorial covers the basics of CSS centering elements. From understanding the box… read more

How to Change HTML Input's Placeholder Color with CSS

Learn how to change the color of an HTML input's placeholder text using CSS. Discover two methods: using the color property and using the ::placehold… read more

How to Center a Div Vertically and Horizontally on a Page

Learn the simplest method to center a div both vertically and horizontally using CSS. This article covers different techniques such as Flexbox, Absol… read more

How to Set Distance Between Flexbox Items in CSS

Setting the distance between Flexbox items in CSS is made easy with two methods: using the gap property or using margin or padding. This article prov… read more

How to Implement a Beating Heart Loader in Pure CSS

The code below generates a beating heart that can be used as a CSS loader. Use it in web pages and web apps to add a visually appealing loading anima… read more

How to Set the Transparent CSS Background Color

Setting a transparent background color in CSS is a simple process that can enhance the visual appeal of your website. This article provides a guide o… read more