Change an HTML5 input's placeholder color with CSS

后端 未结 30 3166
猫巷女王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: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);
    }
    

提交回复
热议问题