我想将占位符和键入的输入都放在输入字段的中心,但是当没有输入时,我希望光标保持左对齐。例如,如果我使用:
input {
text-align: center;
width: 60px;
}
::placeholder {
text-align: center;
}
<input type="text" placeholder="hello">
这将成功地将占位符和我输入的所有输入对齐到中心,但会使光标在占位符的中心而不是向左闪烁
我希望将光标置于输入中的占位符“ hello”中的h之前,但是当我键入任何输入时,功能应保持不变,即输入应居中。仅当没有输入时,光标才应保持对齐。
您可以轻松考虑这一点:placeholder-shown
。不需要JS
input {
text-align: center;
width: 60px;
}
::placeholder {
text-align: center;
}
input:placeholder-shown {
text-align: left;
}
<input type="text" placeholder="hello">
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句