[英]How to change the placeholder of an input in css?
我的输入格式如下所示: <input type="text" placeholder="My placeholder" class="form-control DataTable--global-filter" value="">
我希望能够更改css 中的占位符内容,从"My placeholder"
到其他内容。 我设法使用以下 css 片段更改了颜色和背景以及其他属性,但占位符的内容我无法更改。 我应该放什么属性而不是content
?
::placeholder {
font-style: italic;
font-size: 1em;
color: mintcream;
background: thistle;
padding: 5px;
content : "some other placeholder" !important;
}
您无法更改CSS
中属性的值,但如果您的输入位于form
内并且您可以包装input
,则可以使用一个棘手的解决方法。
简而言之:
<span>
元素包装输入span::before
伪元素并应用您为:placeholder
伪类选择的样式:placeholder
的颜色和背景,使其不再可见span::before
的pointer-events
设置为none
,这样每次点击该元素都会被忽略并从底层input
元素中截取。input
使用必需的属性form
为:valid
时隐藏伪元素 span::before, ::placeholder { font-family: arial; font-style: italic; font-size: .75rem; padding: 5px; line-height: 1; box-sizing: border-box; } ::placeholder { color: transparent; background: transparent; } span { position: relative; } span::before { content : "some other placeholder very long" ; position: absolute; inset: 0 5px; z-index: 1; color: mintcream; background: thistle; width: calc(100% - 10px); white-space: nowrap; overflow: hidden; pointer-events: none; } form:valid span::before { display: none; }
<form> <span><input type="text" placeholder="my placeholder" required /></span> </form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.