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

2

6

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?

Malacca answered 7/7, 2017 at 12:42 Comment(0)
N
7

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;
}
Noreen answered 7/7, 2017 at 12:53 Comment(1)
Would you be able to give reference from w3c or mdn for this behaviour please?Malacca
S
0

I know it is now a complete answer, but you could add different classes for each input

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



#two::-webkit-input-placeholder{
  color: red;
}
#two::-moz-placeholder{
  color: red;
}
<input id="one" type="text" placeholder="one">
<input id="two" type="text" placeholder="two">
Scutter answered 7/7, 2017 at 12:56 Comment(2)
Why should I do this?Malacca
Musa has the correct answer and explanation..I inspected the shadow dom and everything is working as expectedScutter

© 2022 - 2024 — McMap. All rights reserved.