JSFiddle - React, Tailwind, and code Playground
Floating Labels Form
by Jennifer Perrin
HTML
<div class='container'>
<header>
<h1>
Mailing Address
</h1>
</header>
<div class='fields'>
<div class="field">
<input id="f" type="text" class='f' />
<label for="f">First Name</label>
</div>
<div class="field">
<input id="l" type="text" class='f' />
<label for="l">Last Name</label>
</div>
<div class="field">
<input id="a" type="text" class='f' />
<label for="a">Mailing Address</label>
</div>
<div class="field city">
<input id="c" type="text" class='f' />
<label for="c">City</label>
</div>
<div class="field state">
<input id="s" type="text" class='f' />
<label for="s">State</label>
</div>
<div class="field clear">
<input id="n" type="text" class='f' /> <label for="n">Zip</label>
</div>
</div>
<footer>
<button class='submit'>Submit</button>
</footer>
</div>
CSS
@import url(http://fonts.googleapis.com/css?family=Raleway:300);
* {
box-sizing: border-box;
-moz-box-sizing: border-box;
font-family: "Raleway", sans-serif;
font-weight: 300;
}
body {
background: #f2f2f2;
}
.clear {
clear: both;
}
.container {
width: 500px;
margin: 25px auto;
background: white;
border-radius: 3px;
box-shadow: 0 0 0 8px rgba(68, 68, 68, 0.05)
}
header, footer {
border-radius: 3px 3px 0 0;
background: #fafafc;
padding: 10px 10px 7px 10px;
border-bottom: solid 1px #d8d8d8;
}
header h1, footer h1 {
color: #3b9dc4;
margin: 0;
font-size: 1.4em;
font-weight: 300;
}
footer {
margin-top: 5px;
padding: 10px;
border-top: solid 1px #d8d8d8;
border-bottom: none;
border-radius: 0 0 3px 3px;
}
.fields {
padding: 0 10px;
}
.field {
position: relative;
height: 45px;
width: 100%;
border-bottom: solid 1px #bfbfbf;
}
.field:last-of-type {
border-bottom: none;
}
.field.city {
float: left;
width: 75%;
}
.field.state {
float: left;
width: 25%;
}
.f {
border: none;
background: transparent;
height: 32px;
width: 100%;
position: absolute;
top: 13px;
outline: none;
font-size: 1em;
}
.f + label {
position: absolute;
top: 20px;
left: 0px;
color: #727272;
transition: .3s;
-webkit-transition: .3s;
}
.f:focus + label, .f.set + label {
font-weight: 500;
font-size: 9px;
top: 5px;
color: #3b9dc4;
transition: .3s;
-webkit-transition: .3s;
}
.f.set + label {
color: #727272;
}
.submit {
background: none;
border: none;
color: #3b9dc4;
font-size: 1.2em;
cursor: pointer;
padding: 0;
}
JavaScript
$("input.f").on("focusout", function(){
var me = $(this);
if(me.val() !== ""){
me.addClass("set");
} else {
me.removeClass("set");
}
});