Bootstrap, a popular open-source CSS framework, offers a wide range of pre-built components to simplify the development of responsive and mobile-first websites. One of its most fundamental elements is the text input with a button, a staple in form design. In this guide, we'll delve into creating a Bootstrap 5 text input with a button, exploring its various aspects and best practices.

Before we dive into the code, let's briefly discuss why using Bootstrap for form elements is beneficial. Bootstrap's components are not only responsive but also highly customizable, ensuring your forms look great and function well across different devices and screen sizes.

Setting Up the Bootstrap Environment
To start using Bootstrap 5, you'll first need to include it in your project. You can either download the source files or use a CDN (Content Delivery Network). For this guide, we'll use the CDN.

Add the following lines in the
section of your HTML file:<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
Creating a Basic Text Input with Button

Now that we have Bootstrap set up, let's create a simple text input with a button. Wrap the input and button inside a Bootstrap form:
<form>
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Recipient's username" aria-label="Recipient's username">
<button class="btn btn-outline-secondary" type="button">Button</button>
</div>
</form>
The input-group class applies Bootstrap's input group styles, ensuring the input and button are properly aligned. The btn and btn-outline-secondary classes style the button.
Customizing the Input and Button

Bootstrap provides several customization options for inputs and buttons. Let's explore a few:
Input Sizes
You can change the input size using the size class. Add .form-control-lg or .form-control-sm to the input:

<input type="text" class="form-control form-control-lg" ...>
Button Variants
Bootstrap offers various button variants. Replace btn-outline-secondary with another variant, such as btn-primary or btn-danger:




















<button class="btn btn-primary" type="button">Button</button>
Adding Icons to the Button
Adding icons to buttons can enhance their functionality and aesthetics. Bootstrap uses Font Awesome for icons, so you'll need to include Font Awesome in your project:
Add this line in the
section of your HTML file:<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css">
Using Font Awesome Icons
Now, let's add an icon to our button. Replace the button code with the following:
<button class="btn btn-primary" type="button"> <i class="fas fa-search"></i> </button>
The fa-search icon is used here, but you can replace it with any Font Awesome icon.
Button with Icon and Text
You can also add text alongside the icon. Update the button code as follows:
<button class="btn btn-primary" type="button"> <i class="fas fa-search"></i> Search </button>
With these customization options, you can create text inputs with buttons that perfectly fit your project's design and functionality needs.
In conclusion, Bootstrap 5 simplifies the creation of text inputs with buttons, offering a wide range of customization options. By leveraging Bootstrap's pre-built components and styles, you can enhance your forms' appearance and responsiveness. So, start exploring and building today!