:placeholder-shown
Experimental
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
:placeholder-shown
CSS 伪类 在 <input>
或 <textarea>
元素显示 placeholder text 时生效.
/* 选择所有显示占位符(placeholder)的元素 */
:placeholder-shown {
border: 2px solid silver;
}
参数
:placeholder-shown
样例
基础样例
HTML
<input placeholder="Type something here!">
CSS
input {
border: 2px solid black;
padding: 3px;
}
input:placeholder-shown {
border-color: silver;
}
结果
超出文本
在分辨率较小的设备上, 输入框或者其他表单控件可能会变的很窄. 这个选择器可以使得占位符文本缩短. 这个选择器经常和 text-overflow
一起使用.
HTML
<input placeholder="Enter something into this field, if you please!">
CSS
input:placeholder-shown {
text-overflow: ellipsis;
}
结果
规范
Specification | Status | Comment |
---|---|---|
Selectors Level 4 :placeholder-shown |
Working Draft | Initial definition. |
浏览器兼容性
BCD tables only load in the browser
参见
- The
::placeholder
pseudo-element styles the placeholder itself. - Related HTML elements:
<input>
,<textarea>
:-moz-placeholder
,::-moz-placeholder
- HTML forms