更新时间:2023-01-07 17:16:19
共有三种不同的实现:伪元素、伪类和无.
There are three different implementations: pseudo-elements, pseudo-classes, and nothing.
::-webkit-input-placeholder
.[参考]
:-moz-placeholder
(one 冒号).[参考]
::-moz-placeholder
,但旧的选择器仍然可以工作一段时间.[参考]
:-ms-input-placeholder
.[参考]
::placeholder
[Ref]
::-webkit-input-placeholder
. [Ref]
:-moz-placeholder
(one colon). [Ref]
::-moz-placeholder
, but the old selector will still work for a while. [Ref]
:-ms-input-placeholder
. [Ref]
::placeholder
[Ref]
Internet Explorer 9 及更低版本根本不支持 placeholder
属性,而 Opera 12 及更低版本不支持任何用于占位符的 CSS 选择器.
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.
关于***实现的讨论仍在继续.请注意,伪元素的行为类似于 Shadow 中的真实元素DOM.input
上的 padding
不会获得与伪元素相同的背景颜色.
The discussion about the best implementation is still going on. Note the pseudo-elements act like real elements in the Shadow DOM. A padding
on an input
will not get the same background color as the pseudo-element.
用户代理需要忽略带有未知选择器的规则.请参阅选择器级别 3:
User agents are required to ignore a rule with an unknown selector. See Selectors Level 3:
包含无效选择器的 组 选择器无效.
a group of selectors containing an invalid selector is invalid.
所以我们需要为每个浏览器制定单独的规则.否则整个组将被所有浏览器忽略.
So we need separate rules for each browser. Otherwise the whole group would be ignored by all browsers.
::-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 { /* Internet Explorer 10-11 */
color: #909;
}
::-ms-input-placeholder { /* Microsoft Edge */
color: #909;
}
::placeholder { /* Most modern browsers support this now. */
color: #909;
}
<input placeholder="Stack Snippets are awesome!">
opacity: 1
.em
中调整输入元素的大小,并使用大的最小字体大小设置来测试它们.不要忘记翻译:某些语言需要更多空间同样的话.placeholder
提供 HTML 支持但没有 CSS 支持的浏览器(如 Opera)也应该进行测试.input
类型(email
、search
)使用额外的默认 CSS.这些可能会以意想不到的方式影响渲染.使用 属性 -webkit-appearance
和 -moz-appearance
来改变它.示例:opacity: 1
here.em
and test them with big minimum font size settings. Don’t forget translations: some languages need more room for the same word. placeholder
but without CSS support for that (like Opera) should be tested too.input
types (email
, search
). These might affect the rendering in unexpected ways. Use the properties -webkit-appearance
and -moz-appearance
to change that. Example: [type="search"] {
-moz-appearance: textfield;
-webkit-appearance: textfield;
appearance: textfield;
}