Highlighting an input field on focus is a great way to make your web forms more user-friendly and visually appealing. It can also help to draw attention to the input field and make it easier for users to see where they should enter their information.
In this blog post you'll learn how to create animated input field with floating placeholder step by step. You may have seen this type of input field, that when you focus on input field its get highlighted. We going to highlight the input field with animation using HTML, CSS & JavaScript.
By the end of this tutorial you'll be able to make your own and customize animated input field using html css and javascript. You'll find full source code at the end of this blog post.
So, its easy to highlight input field when focused. Here actually input field is not highlighted. its a span tag that is shown. Which is initially set to bottom centre of input field with width:0% and when input field is focused then width change to 100%.
So here I made a simple input field and button, you can say a search bar. the main focus of this blog post is to highlight input field with animation when focused.
Video Tutorial on Animated Input Field:
Highlight Input Field on Focus.
File structure:
- Create one folder.
- Open it in your code editor.
- Create two files in that folder index.html and index.css.
I’ve integrated javascript within the html file inside the script tag. You can create a separate file and link it to an html file.
Step 1: Create a Simple Input Field With Button.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="index.css">
</head>
<body>
<div class="main_box">
<div class="container">
<form action="#">
<div class="box">
<div class="input_box">
<input type="text" class="input" required>
<!-- placeholder -->
<label class="label">Search</label>
<!-- For highlighting input field bottom border -->
<span class="span"></span>
</div>
<button class="button" type="submit">Search</button>
</div>
</form>
</div>
</div>
</body>
</html>
CSS
*{
padding: 0;
margin: 0;
}
.main_box{
width: 100%;
height: 100vh;
background: linear-gradient(90deg , #d4d4d4 , #d0d3ff);
display: flex;
align-items: center;
justify-content: center;
}
.container{
width: 500px;
padding: 20px;
background-color: #fff;
}
.box{
width: 100%;
display: flex;
align-items: center;
}
.box .input_box{
width: 100%;
border-bottom: 1px solid #000;
margin-right: 20px;
position: relative;
}
.box .input{
width: 100%;
font-size: 25px;
padding: 10px 6px;
outline: none;
border: none;
}
.box label{
position: absolute;
left: 0;
top: 14px;
color: #0000009e;
pointer-events: none;
font-size: 25px;
transition: 0.2s;
}
.box span{
position: absolute;
width: 20%;
height: 2px;
top: 100%;
left: 50%;
transform: translate(-50% , -50%);
background-color: blue;
transition: 0.2s;
}
.box .button{
font-size: 25px;
padding: 10px 16px;
border: none;
background-color: #000;
color: #fff;
font-weight: bold;
cursor: pointer;
}
.box .button:active{
background-color: #0000009e;
transform: scale3d(0.9, 0.9, 0.9);
}
CSS
.box span{
position: absolute;
width: 0%;
height: 2px;
top: 100%;
left: 50%;
transform: translate(-50% , -50%);
background-color: blue;
transition: 0.2s;
}
Step 3: Highlight Input Field and Float Up Placeholder (animation).
So first, define variables.
JavaScript
let input = document.querySelector('.input');
let placeholder = document.querySelector('.label');
let span = document.querySelector('.span');
Then after defining variable, We'll run a function on focus event and on blur event on input field.
When input field focus we'll slide placeholder up and change font-size and color of placeholder (label tag). And make span tag (line that highlight the input field) width to 100%.
When input field blur (unfocus) we'll slide placeholder down and change font-size and color of placeholder (label tag). And make span tag (line that highlight the input field) width to 0%.
JavaScript
input.onfocus = function(){
placeholder.style.top = '-10px';
placeholder.style.fontSize = '16px';
placeholder.style.color = 'blue';
span.style.width = '100%';
}
input.onblur = function(){
if (input.value == '') {
placeholder.style.top = '14px';
placeholder.style.fontSize = '25px';
placeholder.style.color = '#0000009e';
span.style.width = '0%';
}
}
Output:
Before focus
After focus
Final code :
That's how you can highlight input field with animation on focus. If you have any question or suggestion feel free to write them in the comment section.
You may like:
- Add image/icon inside input field.
- Search bar design using HTML, CSS & JavaScript.
- Show button only when input field has value.
- How to create an animated input field with linear gradient border.
- How to get input field value using JavaScript.
- How to disable input field using Both HTML & JavaScript.
- Make input field to not take more than one space between words using JavaScript.
Share your thoughts.