如何更改焦点上的占位符颜色?

时间:2022-11-21 15:14:24

How to change the color of placeholder when focus the input field? I use this css to set the default color, but how to change it on focus?

如何在聚焦输入字段时更改占位符的颜色?我用这个css设置默认颜色,但是如何在焦点上更改它?

::-webkit-input-placeholder { color: #999; }

/* Firefox < 19 */
:-moz-placeholder { color: #999; }

/* Firefox > 19 */
::-moz-placeholder { color: #999; }

/* Internet Explorer 10 */
:-ms-input-placeholder { color: #999; }

8 个解决方案

#1


91  

Try this, this should work :

试试这个,这应该工作:

input::-webkit-input-placeholder {
    color: #999;
}
input:focus::-webkit-input-placeholder {
    color: red;
}

/* Firefox < 19 */
input:-moz-placeholder {
    color: #999;
}
input:focus:-moz-placeholder {
    color: red;
}

/* Firefox > 19 */
input::-moz-placeholder {
    color: #999;
}
input:focus::-moz-placeholder {
    color: red;
}

/* Internet Explorer 10 */
input:-ms-input-placeholder {
    color: #999;
}
input:focus:-ms-input-placeholder {
    color: red;
}

Here is an example : http://jsfiddle.net/XDutj/27/

这是一个例子:http://jsfiddle.net/XDutj/27/

#2


4  

In addition to Pranav answer I refined the code with textarea compatibility:

除了Pranav的答案,我还使用textarea兼容性改进了代码:

::-webkit-input-placeholder { color: #999; }
:-moz-placeholder { color: #999; }

:focus::-webkit-input-placeholder { color: #ccc; }
:focus:-moz-placeholder { color: #ccc; }​

#3


1  

Try this:

尝试这个:

HTML

HTML

<input type='text' placeholder='Enter text' />

CSS

CSS

input[placeholder]:focus { color: red; }

#4


1  

I've found this solution with JQuery:

我用JQuery找到了这个解决方案:

 $('input[type="text"]').each(function(){

    $(this).focus(function(){
      $(this).addClass('input-focus');
    });

    $(this).blur(function(){
      $(this).removeClass('input-focus');
    });

  });

with this css:

用这个css:

.input-focus::-webkit-input-placeholder { color: #f00; }    
.input-focus:-moz-placeholder { color: #f00; }
.input-focus:-ms-input-placeholder { color: #f00; }

#5


1  

Use star * to select everything

使用星号*来选择所有内容

*::-webkit-input-placeholder { color: #999; }


*:-moz-placeholder { color: #999; }


*::-moz-placeholder { color: #999; }


*:-ms-input-placeholder { color: #999; }

#6


1  

The following worked for me:

以下对我有用:

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

#7


0  

From Firefox 19: The :-moz-placeholder pseudo-class that matches form elements with the placeholder attribute has been removed, and the ::-moz-placeholder pseudo-element has been added instead.

从Firefox 19:删除了匹配带有占位符属性的表单元素的-moz-placeholder伪类,并添加了:: - moz-placeholder伪元素。

input:focus::-moz-placeholder { color: transparent; }

#8


-1  

You can create a material design animated placeholder that shrinks on top when input field is focused.

您可以创建一个材质设计动画占位符,当输入字段被聚焦时,该占位符会在顶部缩小。

<div class="field">
 <input type="text" name="user" required><br>
 <label>Enter Username</label>
 </div>

Basically the label field is going to act like a placeholder. We can do this only using css. Explained here http://www.voidtricks.com/create-material-design-animated-placeholder/

基本上,标签字段将像占位符一样。我们只能使用css来做到这一点。在这里解释http://www.voidtricks.com/create-material-design-animated-placeholder/

#1


91  

Try this, this should work :

试试这个,这应该工作:

input::-webkit-input-placeholder {
    color: #999;
}
input:focus::-webkit-input-placeholder {
    color: red;
}

/* Firefox < 19 */
input:-moz-placeholder {
    color: #999;
}
input:focus:-moz-placeholder {
    color: red;
}

/* Firefox > 19 */
input::-moz-placeholder {
    color: #999;
}
input:focus::-moz-placeholder {
    color: red;
}

/* Internet Explorer 10 */
input:-ms-input-placeholder {
    color: #999;
}
input:focus:-ms-input-placeholder {
    color: red;
}

Here is an example : http://jsfiddle.net/XDutj/27/

这是一个例子:http://jsfiddle.net/XDutj/27/

#2


4  

In addition to Pranav answer I refined the code with textarea compatibility:

除了Pranav的答案,我还使用textarea兼容性改进了代码:

::-webkit-input-placeholder { color: #999; }
:-moz-placeholder { color: #999; }

:focus::-webkit-input-placeholder { color: #ccc; }
:focus:-moz-placeholder { color: #ccc; }​

#3


1  

Try this:

尝试这个:

HTML

HTML

<input type='text' placeholder='Enter text' />

CSS

CSS

input[placeholder]:focus { color: red; }

#4


1  

I've found this solution with JQuery:

我用JQuery找到了这个解决方案:

 $('input[type="text"]').each(function(){

    $(this).focus(function(){
      $(this).addClass('input-focus');
    });

    $(this).blur(function(){
      $(this).removeClass('input-focus');
    });

  });

with this css:

用这个css:

.input-focus::-webkit-input-placeholder { color: #f00; }    
.input-focus:-moz-placeholder { color: #f00; }
.input-focus:-ms-input-placeholder { color: #f00; }

#5


1  

Use star * to select everything

使用星号*来选择所有内容

*::-webkit-input-placeholder { color: #999; }


*:-moz-placeholder { color: #999; }


*::-moz-placeholder { color: #999; }


*:-ms-input-placeholder { color: #999; }

#6


1  

The following worked for me:

以下对我有用:

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

#7


0  

From Firefox 19: The :-moz-placeholder pseudo-class that matches form elements with the placeholder attribute has been removed, and the ::-moz-placeholder pseudo-element has been added instead.

从Firefox 19:删除了匹配带有占位符属性的表单元素的-moz-placeholder伪类,并添加了:: - moz-placeholder伪元素。

input:focus::-moz-placeholder { color: transparent; }

#8


-1  

You can create a material design animated placeholder that shrinks on top when input field is focused.

您可以创建一个材质设计动画占位符,当输入字段被聚焦时,该占位符会在顶部缩小。

<div class="field">
 <input type="text" name="user" required><br>
 <label>Enter Username</label>
 </div>

Basically the label field is going to act like a placeholder. We can do this only using css. Explained here http://www.voidtricks.com/create-material-design-animated-placeholder/

基本上,标签字段将像占位符一样。我们只能使用css来做到这一点。在这里解释http://www.voidtricks.com/create-material-design-animated-placeholder/