Change an HTML5 input's placeholder color with CSS

后端 未结 30 3167
猫巷女王i
猫巷女王i 2020-11-21 04:36

Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).

But the following CSS doesn\'t do anything

相关标签:
30条回答
  • 2020-11-21 04:41

    I just realize something for Mozilla Firefox 19+ that the browser gives an opacity value for the placeholder, so the color will not be what you really want.

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
        color: #eee; opacity:1;
    }
    input:-moz-placeholder, textarea:-moz-placeholder {
        color: #eee; opacity:1;
    }
    input::-moz-placeholder, textarea::-moz-placeholder {
        color: #eee; opacity:1;
    }
    input:-ms-input-placeholder, textarea:-ms-input-placeholder {
        color: #eee; opacity:1;
    }
    

    I overwrite the opacity for 1, so it will be good to go.

    0 讨论(0)
  • 2020-11-21 04:41

    This short and clean code:

    ::-webkit-input-placeholder {color: red;}
    :-moz-placeholder           {color: red; /* For Firefox 18- */}
    ::-moz-placeholder          {color: red; /* For Firefox 19+ */}
    :-ms-input-placeholder      {color: red;}
    
    0 讨论(0)
  • 2020-11-21 04:42

    For Bootstrap and Less users, there is a mixin .placeholder:

    // Placeholder text
    // -------------------------
    .placeholder(@color: @placeholderText) {
      &:-moz-placeholder {
        color: @color;
      }
      &:-ms-input-placeholder {
        color: @color;
      }
      &::-webkit-input-placeholder {
        color: @color;
      }
    }
    
    0 讨论(0)
  • 2020-11-21 04:43

    Cross-browser solution:

    /* all elements */
    ::-webkit-input-placeholder { color:#f00; }
    ::-moz-placeholder { color:#f00; } /* firefox 19+ */
    :-ms-input-placeholder { color:#f00; } /* ie */
    input:-moz-placeholder { color:#f00; }
    
    /* individual elements: webkit */
    #field2::-webkit-input-placeholder { color:#00f; }
    #field3::-webkit-input-placeholder { color:#090; background:lightgreen; text-transform:uppercase; }
    #field4::-webkit-input-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; }
    
    /* individual elements: mozilla */
    #field2::-moz-placeholder { color:#00f; }
    #field3::-moz-placeholder { color:#090; background:lightgreen; text-transform:uppercase; }
    #field4::-moz-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; }
    

    Credit: David Walsh

    0 讨论(0)
  • 2020-11-21 04:45

    I have tried every combination here to change the color, on my mobile platform, and eventually it was:

    -webkit-text-fill-color: red;
    

    which did the trick.

    0 讨论(0)
  • 2020-11-21 04:46

    You may also want to style textareas:

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder {
      color: #636363;
    }
    input:-moz-placeholder, textarea:-moz-placeholder {
      color: #636363;
    }
    
    0 讨论(0)
提交回复
热议问题