×

注意!页面内容来自https://css-tricks.com/almanac/pseudo-selectors/p/placeholder/,本站不储存任何内容,为了更好的阅读体验进行在线解析,若有广告出现,请及时反馈。若您觉得侵犯了您的利益,请通知我们进行删除,然后访问 原网页

::placeholder

Chris Coyier on Updated on

The ::placeholder pseudo element (or a pseudo classin some casesdepending on the browser implementation) allows you to the placeholder text of a form element. As inthe text set with the placeholder attribute:

<input type="email" placeholder="[email protected]">

You can that text across most browsers with this smattering of vendor-prefixed selectors:

::-webkit-input-placeholder { /* Chrome/Opera/Safari */
  color: pink;
}
::-moz-placeholder { /* Firefox 19+ */
  color: pink;
}
:-ms-input-placeholder { /* IE 10+ */
  color: pink;
}
:-moz-placeholder { /* Firefox 18- */
  color: pink;
}

Important warning: this syntax is non-standardthus all the naming craziness. It doesn’t appear in the spec at all. :placeholder-shown is standardand even spec authors seem to think ::placeholder will be the standardized version.

Like any psuedoyou can scope it to specific elements as neededlike:

input[type="email"].big-dog::-webkit-input-placeholder {
  color: orange;
}

Demo

The difference between :placeholder-shown and ::placeholder

:placeholder-shown is for selecting the input itself when it’s placeholder text is being shown. As opposed to ::placeholder which s the placeholder text.

Here’s a diagram:

I found this highly confusing as:

  1. the specs only have :placeholder-shown and not ::placeholder
  2. :placeholder-shown can still affect the styling of the placeholder textsince it’s a parent element (e.g. font-size).

Note that :placeholder-shown is a pseudo class (it’s an element in a particular state) and ::placeholder is a pseudo element (a visible thing that isn’t really in the DOM). Distinguishable by single-versus-double colons.

Tab Atkins cleared it up for me via email:

:placeholder-shownbeing a pseudo-classhas to select an existing element – it selects the input whenever you’re in the placeholder-showing state. The ::placeholder pseudo-element wraps the actual placeholder text.

Element or class?

This functionality is not standardized. That means that every browser has a different idea on how it should work.

Firefox originally implemented this as a pseudo classbut changed it for a bunch of reasons. To make a long story shortyou can’t do as much with a pseudo class.

For instanceif you want to change the color of the text when the input is focused. You would use a selector like input:focus::placeholderwhich you wouldn’t be able to do with a pseudo class (they don’t stack the same way).

IE10 supports this as a pseudo classrather than an element. Everyone else has implemented a pseudo element.

Firefox placeholder color

You might notice how in Firefox the color of the placeholder looks faded when compared to other browsers. In the image belowFirefox 43 is shown on the left whilst Chrome 47 is shown on the right:

The Chrome version is ideally the that we’d like to see everywhere.

This is becauseby defaultall placeholders in Firefox have an opacity value applied to themso in order to fix this we need to reset that value:

::-moz-placeholder {
  opacity: 1;
}

You can see more by testing this demo out in Firefox.

Supported Styles

The pseudo element supports styling of these properties:

  • font properties
  • color
  • background properties
  • word-spacing
  • letter-spacing
  • text-decoration
  • vertical-align
  • text-transform
  • line-height
  • text-indent
  • opacity

The pseudo class supports most (if not all) of these properties as wellbut isn’t as flexible for the reasons outlined above.

Browser support

This browser support data is from Caniusewhich has more detail. A number indicates that browser supports the feature at that version and up.

Desktop

ChromeFirefoxIEEdgeSafari
5719*No7910.1

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
14314614310.3

Other resources