JSFiddle - React, Tailwind, and code Playground
HTML
<label class="input">
<span>Hello, world!</span>
<input type="text">
</label>
CSS
.input {
margin: 50px;
background: white;
float: left;
clear: both;
}
.input span {
position: absolute;
padding: 5px;
margin-left: 3px;
color: #999;
}
.input input, .input textarea, .input select {
position: relative;
margin: 0;
border-width: 1px;
padding: 6px;
background: transparent;
font: inherit;
}
/* Hack to remove Safari's extra padding. Remove if you don't care about pixel-perfection. */
@media screen and (-webkit-min-device-pixel-ratio:0) {
.input input, .input textarea, .input select { padding: 4px; }
}
JavaScript
(function($) {
function toggleLabel() {
var input = $(this);
setTimeout(function() {
var def = input.attr('title');
if (!input.val() || (input.val() == def)) {
input.prev('span').css('visibility', '');
if (def) {
var dummy = $('<label></label>').text(def).css('visibility','hidden').appendTo('body');
input.prev('span').css('margin-left', dummy.width() + 3 + 'px');
dummy.remove();
}
} else {
input.prev('span').css('visibility', 'hidden');
}
}, 0);
};
function resetField() {
var def = $(this).attr('title');
if (!$(this).val() || ($(this).val() == def)) {
$(this).val(def);
$(this).prev('span').css('visibility', '');
}
};
$('input, textarea').live('keydown', toggleLabel);
$('input, textarea').live('paste', toggleLabel);
$('select').live('change', toggleLabel);
$('input, textarea').live('focusin', function() {
$(this).prev('span').css('color', '#ccc');
});
$('input, textarea').live('focusout', function() {
$(this).prev('span').css('color', '#999');
});
$(function() {
$('input, textarea').each(function() { toggleLabel.call(this); });
});
})(jQuery);