How to change placeholder color on focus?

后端 未结 9 913
有刺的猬
有刺的猬 2020-12-04 11:05

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?

::-webkit-input-pl         


        
相关标签:
9条回答
  • 2020-12-04 11:54

    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/

    0 讨论(0)
  • 2020-12-04 11:57

    I've found this solution with JQuery:

     $('input[type="text"]').each(function(){
    
        $(this).focus(function(){
          $(this).addClass('input-focus');
        });
    
        $(this).blur(function(){
          $(this).removeClass('input-focus');
        });
    
      });
    

    with this css:

    .input-focus::-webkit-input-placeholder { color: #f00; }    
    .input-focus:-moz-placeholder { color: #f00; }
    .input-focus:-ms-input-placeholder { color: #f00; }
    
    0 讨论(0)
  • 2020-12-04 12:00

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

    ::-webkit-input-placeholder { color: #999; }
    :-moz-placeholder { color: #999; }
    
    :focus::-webkit-input-placeholder { color: #ccc; }
    :focus:-moz-placeholder { color: #ccc; }​
    
    0 讨论(0)
提交回复
热议问题