How to build a floating label input field using HTML and CSS.

You have probably seen Floating Label Input Fields. It’s an input that appears as if it has placeholder text in it, but when you click/tap into that input, that text moves out of the way and allows you to type there.

Many of the demos I’ve seen involve JavaScript. But I did not a simple code for this. Here I am going to implemets this using HTML and CSS using few line of code.

Floating Label Input Fields:

Floating label inputbox
Structure Of File:
  • File -> index.html
  • File -> style.css


In this file I have used style.css for giving the look and feel with some effects to our html file.

<link rel="stylesheet" href="style.css">

<div class="container">
<h3>Floating Label using Html and CSS</h3>
<div class="form-group">
<input type="text" class="form-control" placeholder="Varun Singh" 
id="name" name="name" >
<label for="name" class="form-label">Your Name</label>

<div class="form-group">
<input type="email" class="form-control" placeholder="" 
id="email" name="email">
<label for="email" class="form-label">Your Email</label>


This file is used for giving the structure for html contents with some effects.

    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    font-family: "Raleway", sans-serif;
    background-color: #F9E5FE;   
	font-size: 20px;
	text-align: center;
    width: 40vw;
    padding: 2em;
    background-color: #fff;
    border-radius: 5px;  

    margin-bottom: 1em;
    transition: all .3s;  

    font-size: 14px;
    display: block;
    opacity: 1;
    transform: translateY(-1.6em);
    transform-origin: 0 2;
    transition: all .3s;
    padding-left: 5px;    

    box-shadow: none;
    background-color: aliceblue;
    border-color: #ccc;
    transition: all .5s;
    padding: 5px;
	border: 1px solid #E3E2E3;

    color: transparent;   

    box-shadow: none;
    outline: none;
    border-color: antiquewhite;  

.form-control:focus + .form-label,
.form-control:not(:placeholder-shown) + .form-label
    transform: translateY(-3.2em);
    padding-left: 0px;

    transform: scale(1.01,1.01);