Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text input placeholder color in reactjs

When using ordinary CSS if you want to style your place holder you use these css selectors :

::-webkit-input-placeholder {
    color: red;
}

But I can't figure out how to apply these type of styles in react inline styles.

like image 330
Mohammad Razeghi Avatar asked Aug 12 '16 16:08

Mohammad Razeghi


People also ask

How do you change the color of the placeholder in an input?

<input type="text" placeholder="A red placeholder text..">

How do I change placeholder text in React?

Use the placeholder prop to set a placeholder on an input field in React, e.g. <input type="text" placeholder="Your first name" /> . The placeholder text of an input field is shown until the user types something in the field. Copied!

What is placeholder default color?

Note: In most browsers, the appearance of placeholder text is a translucent or light gray color by default.

How do you add color in React JS?

To change background color on click in React:Set the onClick prop on the element. When the element is clicked, set the active state. Use a ternary operator to conditionally set the background color based on the state variable.


2 Answers

Simply give your "input" tag an "id" or a "class" and put your css style in App.css inside src. e.g

//App.css or external stylesheet

#inputID::placeholder {
    color: #ff0000;
    opacity: 1;
}

//your jsx code
<input type="text" id="inputID" placeholder="Your text here" />

That actually worked for me.

like image 148
Olawale Oladiran Avatar answered Oct 07 '22 17:10

Olawale Oladiran


You can't use ::-webkit-inline-placeholder inline.

It is a pseudo-element that (much like e.g. :hover) can only be used in your stylesheet:

The non-standard proprietary ::-webkit-input-placeholder pseudo-element represents the placeholder text of a form element.

Source

Instead, assign a class to the React component via the className property and apply the style to this class.

like image 9
TimoStaudinger Avatar answered Oct 07 '22 18:10

TimoStaudinger