Floating Labels
by Kevin Pliester
HTML
<div class="container">
<div class="form-group">
<label for="date">Datum</label>
<input type="date" id="date">
</div>
<div class="form-group">
<label for="text">Text</label>
<input type="text" id="text">
</div>
</div>
SCSS
$primary: rgba(52, 152, 219, 1);
$text: rgba(236, 240, 241, 1);
$background: rgba(255, 255, 255, 1);
$dark: rgba(44, 62, 80, 1);
* {
outline: none !important;
font-family: arial;
line-height: 1.5;
font-size: 1em;
}
body {
color: $text;
background-color: $background;
}
.container {
top: 50%;
left: 50%;
width: 100%;
position: fixed;
max-width: 300px;
transform: translate(-50%, -50%);
}
.form-group {
position: relative;
margin-bottom: 5em;
}
label {
top: .5em;
left: 0;
z-index: 2;
color: $dark;
min-width: 5em;
padding: .2em 0;
cursor: text;
position: absolute;
background-color: $background;
transition: all .15s ease-in;
.focus & {
/* parent with .focus class */
top: -1.5em;
font-size: .9em;
color: $primary;
background: transparent;
}
}
input {
z-index: 3;
width: 100%;
border: 0px;
padding: .5em 0;
color: $dark;
cursor: text;
position: absolute;
background-color: transparent;
border-bottom: .1em solid $dark;
transition: all .15s ease-in;
.focus & {
/* parent with .focus class */
border-bottom: .1em solid $primary;
}
}
input[type=date] {
z-index: 1;
}
JavaScript
(function($) {
$('.form-group').each(function() {
var this_group = this
var this_input = $(this_group).find('input')
this_input.focus(function() {
$(this_group).addClass('focus')
})
this_input.blur(function() {
if (!this.value) {
$(this_group).removeClass('focus')
}
})
})
})(jQuery)