<div class="input-container">
<input
type="text"
id="fname"
name="fname"
value=""
aria-labelledby="label-fname"
/>
<label class="label" for="fname" id="label-fname">
<div class="text">First Name</div>
</label>
</div>
.input-container {
position: relative;
}
input {
height: 48px;
width: 280px;
border: 1px solid #c0c0c0;
border-radius: 4px;
box-sizing: border-box;
padding: 16px;
}
.label {
position: absolute;
top: 0;
bottom: 0;
left: 16px;
display: flex;
align-items: center;
pointer-events: none;
}
input, .label .text {
font-family: 'Segoe UI';
font-size: 16px;
}
.label .text {
transition: all 0.15s ease-out;
color: grey;
}
input:focus {
outline: none;
border: 2px solid blue;
}
input:focus + .label .text, :not(input[value=""]) + .label .text {
font-size: 12px;
transform: translate(0, -150%);
background-color: white;
padding-left: 4px;
padding-right: 4px;
}
input:focus + .label .text {
color: blue;
}
const input = document.getElementById('fname');
input.addEventListener('input', () => {
input.setAttribute('value', input.value);
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.