admin管理员组文章数量:1333435
I have a new and intriguing requirement. Instead of the placeholder disappearing when the user starts typing, it should remain visible and shift over to the right hand side of the input box.
Is this even possible with a standard HTML placeholder? How might I achieve this?
Thanks
I have a new and intriguing requirement. Instead of the placeholder disappearing when the user starts typing, it should remain visible and shift over to the right hand side of the input box.
Is this even possible with a standard HTML placeholder? How might I achieve this?
Thanks
Share asked Oct 22, 2013 at 11:13 DavidDavid 16.1k23 gold badges95 silver badges154 bronze badges1 Answer
Reset to default 4This is not possible with a standard HTML placeholder due to native behavior of it disappearing. However, you might want to consider adding an element to pose as the placeholder, and add a CSS selector to sense when the user starts typing (:placeholder-shown
) and move it right.
Here's how it might be done:
.wrapper {
position: relative;
font-family: sans-serif;
font-size: 14px;
width: max-content;
}
.placeholder {
position: absolute;
right: 4px;
top: 2px;
pointer-events: none;
opacity: .5;
}
.input:placeholder-shown + .placeholder {
/* if real placeholder is shown - hide fake placeholder */
opacity: 0;
}
<div class="wrapper">
<input type="text" class="input" placeholder="Test">
<div class="placeholder">Test</div>
</div>
本文标签: javascriptHow to keep input placeholder visible when user is typingStack Overflow
版权声明:本文标题:javascript - How to keep input placeholder visible when user is typing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742292951a2448154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论