Change an HTML5 input's placeholder color with CSS

后端 未结 30 3170
猫巷女王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 05:06

    Use the new ::placeholder if you use autoprefixer.

    Note that the .placeholder mixin from Bootstrap is deprecated in favor of this.

    Example:

    input::placeholder { color: black; }
    

    When using autoprefixer the above will be converted to the correct code for all browsers.

    0 讨论(0)
  • 2020-11-21 05:06

    Now we have a standard way to apply CSS to an input's placeholder : ::placeholder pseudo-element from this CSS Module Level 4 Draft.

    0 讨论(0)
  • 2020-11-21 05:06

    The easiest way would be:

    #yourInput::placeholder {
        color: red;/*As an example*/
    }
    /* if that would not work, you can always try styling the attribute itself: */
    #myInput[placeholder] {
        color: red;
    }
    
    0 讨论(0)
  • 2020-11-21 05:07

    For Sass users:

    // Create placeholder mixin
    @mixin placeholder($color, $size:"") {
      &::-webkit-input-placeholder {
        color: $color;
        @if $size != "" {
          font-size: $size;
        }
      }
      &:-moz-placeholder {
        color: $color;
        @if $size != "" {
          font-size: $size;
        }
      }
      &::-moz-placeholder {
        color: $color;
        @if $size != "" {
          font-size: $size;
        }
      }
      &:-ms-input-placeholder {
        color: $color;
        @if $size != "" {
          font-size: $size;
        }
      }
    }
    
    // Use placeholder mixin (the size parameter is optional)
    [placeholder] {
      @include placeholder(red, 10px);
    }
    
    0 讨论(0)
  • 2020-11-21 05:07

    Here is one more example:

    .form-control::-webkit-input-placeholder {
      color: red;
      width: 250px;
    }
    h1 {
      color: red;
    }
    <div class="col-sm-4">
      <input class="form-control" placeholder="Enter text here.." ng-model="Email" required/>
    </div>

    0 讨论(0)
  • 2020-11-21 05:08

    Compass has a mixin for this out of the box.

    Take your example:

    <input type="text" placeholder="Value">
    

    And in SCSS using compass:

    input[type='text'] {
      @include input-placeholder {
        color: #616161;
      }
    }
    

    See docs for the input-placeholder mixin.

    0 讨论(0)
提交回复
热议问题