Change an HTML5 input's placeholder color with CSS

后端 未结 30 3058
猫巷女王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:46

    If you are using Bootstrap and couldn't get this working then probably you missed the fact that Bootstrap itself adds these selectors. This is Bootstrap v3.3 we are talking about.

    If you are trying to change the placeholder inside a .form-control CSS class then you should override it like this:

    .form-control::-webkit-input-placeholder { /* WebKit, Blink, Edge */
        color:    #777;
    }
    .form-control:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
        color:    #777;
        opacity:  1;
    }
    .form-control::-moz-placeholder { /* Mozilla Firefox 19+ */
        color:    #777;
        opacity:  1;
    }
    .form-control:-ms-input-placeholder { /* Internet Explorer 10-11 */
        color:    #777;
    }
    
    0 讨论(0)
  • 2020-11-21 04:48

    How about this

    <input type="text" value="placeholder text" onfocus="this.style.color='#000'; 
        this.value='';" style="color: #f00;" />

    No CSS or placeholder, but you get the same functionality.

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

    OK, placeholders behave differently in different browsers, so you need using browser prefix in your CSS to make them identical, for example Firefox gives a transparency to placeholder by default, so need to add opacity 1 to your css, plus the color, it's not a big concern most of the times, but good to have them consistent:

    *::-webkit-input-placeholder { /* WebKit browsers */
        color:    #ccc;
    }
    *:-moz-placeholder { /* Mozilla Firefox <18 */
        color:    #ccc;
        opacity:  1;
    }
    *::-moz-placeholder { /* Mozilla Firefox 19+ */
        color:    #ccc;
        opacity:  1;
    }
    *:-ms-input-placeholder { /* Internet Explorer 10-11 */
        color:    #ccc;
    }
    
    0 讨论(0)
  • 2020-11-21 04:48

    You can change an HTML5 input's placeholder color with CSS. If by chance, your CSS conflict, this code note working , you can use (!important) like below.

    ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
        color:#909 !important;
    }
    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
       color:#909 !important;
       opacity:1 !important;
    }
    ::-moz-placeholder { /* Mozilla Firefox 19+ */
       color:#909 !important;
       opacity:1 !important;
    }
    :-ms-input-placeholder { /* Internet Explorer 10-11 */
       color:#909 !important;
    }
    ::-ms-input-placeholder { /* Microsoft Edge */
       color:#909 !important;
    }
    
    <input placeholder="Stack Snippets are awesome!">
    

    Hope this will help.

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

    For SASS/SCSS user using Bourbon, it has a built-in function.

    //main.scss
    @import 'bourbon';
    
    input {
      width: 300px;
    
      @include placeholder {
        color: red;
      }
    }
    

    CSS Output, you can also grab this portion and paste into your code.

    //main.css
    
    input {
      width: 300px;
    }
    
    input::-webkit-input-placeholder {
      color: red;
    }
    input:-moz-placeholder {
      color: red;
    }
    input::-moz-placeholder {
      color: red;
    }
    input:-ms-input-placeholder {
      color: red;
    }
    
    0 讨论(0)
  • 2020-11-21 04:50

    I think this code will work because a placeholder is needed only for input type text. So this one line CSS will be enough for your need:

    input[type="text"]::-webkit-input-placeholder {
        color: red;
    }
    
    0 讨论(0)
提交回复
热议问题