How to Change HTML Input’s Placeholder Color with CSS

Avatar

By squashlabs, Last Updated: October 6, 2023

How to Change HTML Input’s Placeholder Color with CSS

To change the color of an HTML input’s placeholder text using CSS, you can use the ::placeholder pseudo-element selector. Here are two possible ways to accomplish this:

Method 1: Using the color property

You can change the color of the placeholder text by applying the color property to the ::placeholder pseudo-element selector. Here’s an example:

input::placeholder {
  color: red;
}

In this example, the placeholder text of all input elements will be displayed in red.

Related Article: How to Hide Scroll Bar in CSS While Maintaining Scroll Functionality

Method 2: Using the ::placeholder selector with a specific input type

If you only want to change the color of the placeholder text for a specific input type, you can combine the ::placeholder pseudo-element selector with the attribute selector for that input type. Here’s an example:

input[type="text"]::placeholder {
  color: blue;
}

In this example, only the placeholder text of input elements with the type “text” will be displayed in blue.

It’s important to note that the ::placeholder pseudo-element selector is supported in modern browsers, but it may not work in older versions of Internet Explorer. To ensure compatibility, you can use vendor prefixes and the older ::-webkit-input-placeholder and :-ms-input-placeholder selectors. Here’s an example:

input::placeholder,
::-webkit-input-placeholder,
:-ms-input-placeholder {
  color: green;
}

In this example, the placeholder text of input elements will be displayed in green, with compatibility for different browser prefixes.

Best Practices

When changing the color of an HTML input’s placeholder text with CSS, it’s important to consider the following best practices:

1. Choose a color that provides sufficient contrast with the background color of the input element. This ensures that the placeholder text is easily readable.

2. Avoid using colors that may be associated with specific states (e.g., red for error messages) to prevent confusion between the placeholder text and actual input values.

3. Test your changes in different browsers to ensure compatibility and consistent rendering across platforms.

4. Consider using CSS preprocessors like Sass or Less to define variables for placeholder colors, making it easier to maintain and update your stylesheets.

Related Article: How to Fix CSS Text Overflow Ellipsis Issue

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

How to Disable Scrolling On Body Using CSS

Disabling scrolling on the body of a webpage can be achieved using CSS. This article provides two methods for accomplishing this: using the overflow:hidden property and... read more

How to Implement an Onclick Effect in CSS

Adding an onclick effect in CSS is a simple process that can enhance the interactivity of your web pages. This article provides a guide on how to implement this effect... read more

How to Style a Disabled Button with CSS

Styling disabled buttons using CSS can greatly enhance the user interface of your web application. This article provides a step-by-step guide on two different methods to... 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 on how to achieve... 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 provides a step-by-step... read more

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 achieve the desired... read more