Is it possible to have a 'permanent' placeholder?

好久不见. 提交于 2019-11-30 21:09:30
Gillian Lo Wong

By wrapping the input inside a special div you can add a block of text and absolute position it above the input field.

.input-container {
  position: relative;
  width: 150px;
}

.input-container input {
  width: 100%;
}

.input-container .unit {
  position: absolute;
  display: block;
  top: 3px;
  right: -3px;
  background-color: grey;
  color: #ffffff;
  padding-left: 5px;
  width: 45px;
}
<div class="input-container">
  <input type="text" value="102.95" name="" />
  <span class="unit">Volts</span>
</div>
<div class="input-container">
  <input type="text" value="30" name="" />
  <span class="unit">Kilos</span>
</div>
<div class="input-container">
  <input type="text" value="64" name="" />
  <span class="unit">km/h</span>
</div>

Demo: http://jsfiddle.net/mgmBE/3/

This possible solution has nothing to do with the html5 placeholder but if you adapt the code accordingly it should work how you want it to:

http://attardi.org/labels2/#demo

Look at the fourth input field, the 'placeholder' is hidden once you type something, but you can easily change that in the javascript code.

You would however need to write something that moves the 'placeholder' when something is typed in, if that's what you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!