How to Add Button Inside an Input Field in HTML ?
Last Updated :
14 Feb, 2024
Integrating a button inside an input box is a common design pattern in web development, often seen in search bars, where a “search” button sits within the text input field, or in password fields that include a “show/hide” toggle. This UI element enhances user experience by offering a clear, interactive way to perform actions directly from the input field. Achieving this layout involves a combination of HTML, CSS, and sometimes JavaScript, depending on the functionality required.
Method 1: Using HTML and CSS
The simplest way to place a button inside an input box is by wrapping both elements within a parent div and using CSS to position the button over the input field.
Example: Basic Implementation to add button in input field.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Input with Button</title>
<style>
.input-wrapper {
position: relative;
display: inline-block;
}
.input-wrapper input {
padding-right: 40px;
}
.input-wrapper button {
position: absolute;
top: 0;
right: 0;
border: none;
background-color: transparent;
cursor: pointer;
}
</style>
</head>
<body>
<div class="input-wrapper">
<input type="text" placeholder="Search...">
<button onclick="alert('Button clicked!');">
🔍
</button>
</div>
</body>
</html>
Output

In this example, the button is absolutely positioned within the input-wrapper container, aligning it to the right of the input field. The padding on the right side of the input ensures that text entered by the user does not overlap with the button.
Method 2: Advanced Customization with Icons and JavaScript
For a more advanced implementation, such as adding an icon inside the input field that toggles the input type (e.g., for a password field), you might use Font Awesome for the icon and JavaScript for the toggle functionality.
Example: Show/Hide Password Toggle
First, include Font Awesome for the icons:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
Then, add the HTML structure with embedded JavaScript:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Show/Hide Password</title>
<link rel="stylesheet" href=
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
<style>
.input-wrapper {
position: relative;
display: inline-block;
}
.input-wrapper input {
padding-right: 30px;
}
.toggle-password {
position: absolute;
top: 50%;
right: 10px;
transform: translateY(-50%);
border: none;
background: none;
cursor: pointer;
}
</style>
</head>
<body>
<div class="input-wrapper">
<input id="password" type="password"
placeholder="Password">
<button class="toggle-password"
onclick="togglePasswordVisibility()">
<i id="toggleIcon" class="fas fa-eye"></i>
</button>
</div>
<script>
function togglePasswordVisibility() {
var passwordInput = document.getElementById('password');
var toggleIcon = document.getElementById('toggleIcon');
if (passwordInput.type === "password") {
passwordInput.type = "text";
toggleIcon.classList.remove('fa-eye');
toggleIcon.classList.add('fa-eye-slash');
} else {
passwordInput.type = "password";
toggleIcon.classList.remove('fa-eye-slash');
toggleIcon.classList.add('fa-eye');
}
}
</script>
</body>
</html>
Output

This setup offers an interactive user experience by allowing the user to toggle the visibility of their password input.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read