Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do comma separated placeholder rules not get applied in css?

If I apply the following rule to an input element with id #one then the placeholder color will change,

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

But if I use comma separater to combine placeholder rules of different browsers then the color doesn't apply, e.g.

#two::-webkit-input-placeholder,
#two::-moz-placeholder{
  color: red;
}

Working example:

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

#two::-webkit-input-placeholder,
#two::-moz-placeholder{
  color: red;
}
<input id="one" type="text" placeholder="one">
<input id="two" type="text" placeholder="two">

Why does the #two placeholder not change its color to red?

like image 851
user31782 Avatar asked Jul 07 '17 12:07

user31782


People also ask

Can you set placeholder text in CSS?

CSS ::placeholder SelectorThe ::placeholder selector selects form elements with placeholder text, and let you style the placeholder text. The placeholder text is set with the placeholder attribute, which specifies a hint that describes the expected value of an input field.

How do you reference a placeholder in CSS?

Use the ::placeholder pseudo-element to style your placeholder text in an <input> or <textarea> form element. Most modern browsers support this, but for older browsers, vendor prefixes will be required.

How do I override placeholder in CSS?

Step 2) Add CSS: In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector. Note that Firefox adds a lower opacity to the placeholder, so we use opacity: 1 to fix this.


1 Answers

This is because a browser will only apply a rule form a selector it can fully interpret.
For a webkit type browser -webkit-input-placeholder is valid but -moz-placeholder is not, so it trashes the entire selector, and vise-versa for a geeko based browser.
The solution is to separate browser specific selectors.

#two::-webkit-input-placeholder{
  color: red;
}
#two::-moz-placeholder{
  color: red;
}
like image 131
Musa Avatar answered Oct 07 '22 16:10

Musa