Progressive fallback floating labels
A 'floating label' effect that requires no JS and is progressive enhancement
by Sora2455
HTML
<fieldset>
<legend>
Floating labels example (no-JS)
</legend>
<label class="floating-label">
<input type="text" placeholder=" ">
<span>Username</span>
</label>
<label class="floating-label">
<input type="Password" placeholder=" ">
<span>Password</span>
</label>
</fieldset>
<p>
Inspired by Bootstrap's <a href="https://getbootstrap.com/docs/4.0/examples/floating-labels/">floating labels</a>.
</p>
SCSS
*, ::after, ::before {
box-sizing: border-box;
}
label.floating-label {
display: block;
position: relative;
height: 2.2em;
margin-bottom: 1em;
}
label.floating-label input {
font-size: 1em;
height: 2.2em;
padding-top: 0.7em;
line-height: 1.5;
color: #495057;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out,box-shadow 0.15s ease-in-out;
}
label.floating-label input:focus {
color: #495057;
background-color: #fff;
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
label.floating-label input + span {
position: absolute;
top: 0em;
left: 0;
display: block;
width: 100%;
font-size: 0.66em;
line-height: 1.5;
color: #495057;
border: 1px solid transparent;
border-radius: 0.25rem;
transition: font-size 0.1s ease-in-out, top 0.1s ease-in-out;
}
label.floating-label input:-ms-input-placeholder {
padding-top: 0;
font-size: 1em;
}
label.floating-label input:-ms-input-placeholder + span {
top: 0.3em;
font-size: 1em;
}
label.floating-label input:placeholder-shown {
padding-top: 0;
font-size: 1em;
}
label.floating-label input:placeholder-shown + span {
top: 0.3em;
font-size: 1em;
}