Implementing the Float Label Form Pattern
http://webdesign.tutsplus.com/tutorials/ux-tutorials/implementing-the-float-label-form-pattern/?utm_source=CSS-Weekly&utm_campaign=Issue-83&utm_medium=email
by kazu69
HTML
<script src="https://getfirebug.com/firebug-lite.js"></script>
<section class="container">
<h1 class="title">Contact Me</h1>
<form id="form" class="form" action="#" method="get">
<ul>
<li>
<label for="name">Your Name:</label>
<input type="text" placeholder="Your Name" id="name" name="name" tabindex="1" />
</li>
<li>
<label for="email">Your Email:</label>
<input type="email" placeholder="Your Email" id="email" name="email" tabindex="2" />
</li>
<li>
<label for="message">Message:</label>
<textarea placeholder="Message…" id="message" name="message" tabindex="3"></textarea>
</li>
</ul>
<input type="submit" value="Send Message" id="submit"/>
</form>
</section>
CSS
/*
* CSS Reset
* http://meyerweb.com/eric/tools/css/reset/
* v2.0 | 20110126
* License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/*
* Our Custom Styles
*
*/
/* General
==================================== */
*, *:before, *:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
body {
background: #eaedf1;
}
body, input, textarea {
font: 1em/1.5 Arial, Helvetica, sans-serif;
}
.container {
max-width: 25em;
margin: 2em auto;
width: 95%;
}
.title {
font-size: 1.5em;
padding: .5em 0;
text-align: center;
background: #323a45;
color: white;
border-radius: 5px 5px 0 0;
}
/* Form
==================================== */
.form ul {
background: white;
margin-bottom: 1em;
}
.form li {
border: 1px solid #ccc;
border-bottom: 0;
margin-bottom: 0px;
position: relative;
}
.form li:first-child {
border-top: 0;
}
.form li:last-child {
border-bottom: 1px solid #ccc;
}
label {
font-size: .8125em; /* 13/16 */
position: absolute;
top: 1.23em;
left: 1.23em;
color: #f1773b;
opacity:...
JavaScript
/*
* Interaction design based on:
* http://dribbble.com/shots/1254439--GIF-Mobile-Form-Interaction?list=users
*/
$(document).ready(function(){
// Test for placeholder support
$.support.placeholder = (function(){
var i = document.createElement('input');
return 'placeholder' in i;
})();
// Hide labels by default if placeholders are supported
if($.support.placeholder) {
$('.form li').each(function(){
$(this).addClass('js-hide-label');
});
// Code for adding/removing classes here
$('.form li').find('input, textarea').on('keyup blur focus', function(e){
// Cache our selectors
var $this = $(this),
$parent = $this.parent();
if (e.type == 'keyup') {
if( $this.val() == '' ) {
$parent.addClass('js-hide-label');
} else {
$parent.removeClass('js-hide-label');
}
}
else if (e.type == 'blur') {
if( $this.val() == '' ) {
$parent.addClass('js-hide-label');
}
else {
$parent.removeClass('js-hide-label').addClass('js-unhighlight-label');
}
}
else if (e.type == 'focus') {
if( $this.val() !== '' ) {
$parent.removeClass('js-unhighlight-label');
}
}
});
}
});