Change an HTML5 input's placeholder color with CSS

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

    Here is the solution with CSS selectors

    ::-webkit-input-placeholder { /* WebKit, Blink, Edge */
        color:    #909;
    }
    
    :-moz-placeholder { /* Mozilla Firefox 4 to 18 */
       color:    #909;
       opacity:  1;
    }
    
    ::-moz-placeholder { /* Mozilla Firefox 19+ */
       color:    #909;
       opacity:  1;
    }
    
    ::-ms-input-placeholder { /* Microsoft Edge */
       color:    #909;
    }
    
    :-ms-input-placeholder { /* Internet Explorer 10-11 */
       color:    #909;
    }
    
    • WebKit, Blink (Safari, Google Chrome, Opera 15+) and Microsoft Edge are using a pseudo-element:
      ::-webkit-input-placeholder.
    • Mozilla Firefox 4 to 18 is using a pseudo-class:
      :-moz-placeholder (one colon).
      Mozilla Firefox 19+ is using a pseudo-element:
      ::-moz-placeholder, but the old selector will still work for a while.
    • Internet Explorer 10 and 11 are using a pseudo-class:
      :-ms-input-placeholder.
    • Internet Explorer 9 and lower does not support the placeholder attribute at all, while Opera 12 and lower do not support any CSS selector for placeholders.
    0 讨论(0)
  • 2020-11-21 04:57

    Adding an actual very nice and simple possibility: css filters!

    It will style everything, including the placeholder.

    The following will set both input elements on the same palette, using the hue filter for color changes. It render very well now in browsers (except ie...)

    input {
      filter: sepia(100%) saturate(400%) grayscale(0) contrast(200%) hue-rotate(68deg) invert(18%);
    }
    <input placeholder="Hello world!" />
    <input type="date" /><br>
    <input type="range" />
    <input type="color" />

    To allow users to change it dynamically, using an input type color for changes, or to find nuances, check out this snippet:

    From: https://codepen.io/Nico_KraZhtest/pen/bWExEB

    function stylElem() {
      stylo.dataset.hue = ((parseInt(stylo.value.substring(1), 16))/46666).toFixed(0)
      Array.from(document.querySelectorAll('input, audio, video')).forEach(function(e){
          e.style.cssText += ";filter:sepia(100%) saturate(400%)grayscale(0)contrast(200%)hue-rotate("+ stylo.dataset.hue+"deg)invert("+(stylo.dataset.hue/3.6)+"%)"
      out.innerText = e.style.cssText
    })()}
    
    stylElem()
    body {background: black; color: white}
    Choose a color!
    <input type="color" id="stylo" oninput="stylElem()">
    <br>
    <div id="out"></div> <p>
      <input placeholder="Hello world!" />
      <input type="date" /><br>
      <input type="range" />
     <br>
    <audio controls src="#"></audio> <br><br> 
    <video controls src="#"></video>

    Css filters docs: https://developer.mozilla.org/en-US/docs/Web/CSS/filter

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

    For Bootstrap users, if you are using class="form-control", there may be a CSS specificity issue. You should get a higher priority:

    .form-control::-webkit-input-placeholder {
        color: red;
    }
    //.. and other browsers
    

    Or if you are using Less:

    .form-control{
        .placeholder(red);
    }
    
    0 讨论(0)
  • 2020-11-21 05:03

    You can use this for input and focus style:

    input::-webkit-input-placeholder  { color:#666;}
    input:-moz-placeholder  { color:#666;}
    input::-moz-placeholder { color:#666;}
    input:-ms-input-placeholder  { color:#666;}
    /* focus */
    input:focus::-webkit-input-placeholder { color:#eee; }
    input:focus:-moz-placeholder { color:#eee } /* FF 4-18 */
    input:focus::-moz-placeholder { color:#eee } /* FF 19+ */
    input:focus:-ms-input-placeholder { color:#eee } /* IE 10+ */
    
    0 讨论(0)
  • 2020-11-21 05:03

    A part of HTML:

     <form action="www.anything.com">
     <input type="text" name="name"
      placeholder="Enter sentence"/>
      </form>
    

    I gonna show how to change color of expression of 'Enter sentence' by CSS:

      ::placeholder{
      color:blue;
       }
    
    0 讨论(0)
  • 2020-11-21 05:05

    In addition to toscho's answer I've noticed some webkit inconsistencies between Chrome 9-10 and Safari 5 with the CSS properties supported that are worth noting.

    Specifically Chrome 9 and 10 do not support background-color, border, text-decoration and text-transform when styling the placeholder.

    The full cross-browser comparison is here.

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