What is the best way to insert an image icon within an input field using bootstrap?

I am currently working on enhancing the design of my input text field by incorporating an image icon using Bootstrap. I have successfully used a font-awesome icon within the input tag as an example.

Here is the example of what I have implemented:

https://i.sstatic.net/Dnrs6.png

Below is the code for the above example:

     <form  id="main-form" method="post" action="index.php">
        <div class="main-form">
                <div class="row" id="single">
                    <div class="col-md-9 shortfieldz">
                           <i class="zmdi zmdi-link"></i>
                             <input type="text" class="form-control main-input"  id='myInput' name="url" value="" placeholder="Paste a long url" /> 

                    </div>
                    <div class="col-md-3 shortbtnz">
                        <input class="btn btn-default btn-block main-button"  type="submit"   name="Shorten" value="Shorten">
                        <!--<button class="btn btn-primary btn-block main-button" id="copyurl" type="button">Copy</button>-->
                    </div>


                </div>
        </div>
      </form>

However, I now intend to replace the font-awesome icon with an img tag to allow for further customization of the image:

Here is my approach:

   <form  id="main-form" method="post" action="index.php">
        <div class="main-form">
                <div class="row" id="single">
                    <div class="col-md-9 shortfieldz">
                            <img src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/GG-2.png" class="img-responsive" alt="Responsive image" width="30" height="24" /> 
                             <input type="text" class="form-control main-input"  id='myInput' name="url" value="" placeholder="Paste a long url" /> 

                    </div>
                    <div class="col-md-3 shortbtnz">
                        <input class="btn btn-default btn-block main-button"  type="submit"   name="Shorten" value="Shorten">
                        <!--<button class="btn btn-primary btn-block main-button" id="copyurl" type="button">Copy</button>-->
                    </div>


                </div>
        </div>
      </form>

Output: https://i.sstatic.net/r0jaU.png

For further details, visit:

I am specifically seeking a solution within Bootstrap. Any guidance or suggestions would be greatly appreciated.

Answer №1

Utilize Bootstrap's input group feature and insert the image within the span element. Visit Bootstrap's official documentation for more details.

<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon1">
      <img src="https://cdncontribute.geeksforgeeks.org/wp-content/uploads/GG-2.png" class="img-responsive" alt="Responsive image" width="30" height="24" />
    </span>
  </div>
  <input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
</div>

Feel free to check out the working code pen here

Answer №2

If you observe closely in the inspector, the link provided gives detailed instructions on how to achieve the desired outcome.

The class shortfieldz is defined with a relative position, indicating that its children should be positioned relative to its boundaries.

#main-form .main-form .shortfieldz {
    padding-right: 0px;
    position: relative; <--
}

Subsequently, the image is absolutely positioned, with top and left coordinates set in relation to its parent element, shortfieldz.

#main-form .main-form .shortfieldz .zmdi-link {
    position: absolute; <---
    font-size: 28px;
    top: 15px; <---
    left: 25px; <---
    color: #8c8c8c;
    pointer-events: none;
}

Finally, a padding is added to the input element on the left to accommodate the overlapping image.

#main-form .main-form .main-input {
    padding: 0px 55px; <----
    border-width: 0;
    font-size: 19px;
    font-family: roboto;
    background: #f2f2f2;
    height: 55px;
    color: #151720;
    border-radius: 29px 0px 0px 29px;
    transition: all 0.2s linear;
    -webkit-transition: all 0.2s linear;
    -o-transition: all 0.2s linear;
    -ms-transition: all 0.2s linear;
    -moz-transition: all 0.2s linear;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mastering the CSS Art of Working with Sprite Images and Their Positioning

I am currently working on an educational project as a front-end developer, where I aim to clone the Google homepage. To achieve this, I am utilizing Google's own sprite and here is the image of the sprite that I am using. https://i.stack.imgur.com/YT ...

Inspecting JavaScript for any attachments

Before hitting the submit button, I need to verify if a file has been uploaded and display a warning message prompting the user to attach a file if it hasn't been done yet. I am looking for guidance on how to achieve this using JavaScript, Prototype, ...

What is the best method for users to add a div element and its contents?

As someone new to the world of web development, I am currently learning and working on a project. I have encountered an issue with creating a custom div that includes a link and user-defined text. I want the div to be generated automatically when the use ...

Unusual occurrence involving label span and the use of italic font styling

Currently following a Xamarin tutorial on creating a label view. You can check out the tutorial here. Encountered an issue where applying an italic font attribute to text within a span tag does not retain the text size set in the label tag. <StackLayo ...

Unlocking the potential of Object[value] in JavaScript/jQuery: A guide to extracting its value

I have a table named 'mytable' with the following structure: <tr> <td><input type="checkbox" name="check[]" value="11"></td> <td>11</td> <td>2014-11-06 18:49:26</td> < ...

calculation of progress bar advancement

I want to make the progress bar in my game responsive to changes in the winning score. Currently, it moves from 0% to 100%, which is equivalent to 100 points - the default winning score. But I need the progress bar to adjust accordingly based on the user-i ...

Is it possible to set an onmousedown event to represent the value stored at a specific index in an array, rather than the entire array call?

Apologies if the question title is a bit unclear, but I'm struggling to articulate my issue. The challenge lies in this synchronous ajax call I have (designed to retrieve json file contents). $.ajax({ url: "/JsonControl/Events.json", dataTyp ...

Facing a blank page with no errors displayed during the HTML rendering process in Angular version 6

One of the most frustrating aspects of working with Angular is the lack of information provided when there is a render error in an HTML page. Instead of specifying which page the error is in, Angular defaults to the route page and provides no further detai ...

Tips for capturing the interaction between a jQuery Dialog and its Parent

Within the parent HTML, there is a call that triggers a JavaScript function. <div class="data"> <form:input title="Building" styleClass="content contractorDisable" maxlength="5" size="6" path="fireImpairForm.bldCode" />&nbsp; <a hre ...

Tips for adjusting the horizontal position of a grid item within a map() loop

I am trying to align the text in my Timeline component from Material Ui always towards the center of the timeline. The TimelineContent contains Paper, Typography (for title and description), and an image. Currently, I have multiple TimelineContent element ...

Creating Images that appear optimized on smaller screens - the art of responsive design!

Currently, I am in the process of working on this test page located at the following link: While the page appears fine on my laptop screen, it is displaying poorly on my phone's browser. The images, in particular, appear disorganized and messy. Coul ...

Conceal or reveal radio buttons according to information in a table

How can I dynamically add and remove values from a table based on radio button selections? My goal is to add the selected value to the table and hide it from the radio button list, and if the value is deleted from the table, I want to show it back in the r ...

Is it possible to continuously generate webpages using AJAX?

Is there a way to achieve an infinite scrolling effect in all directions using ajax requests without the need for flash or silverlight? If anyone has an example of this, I would love to see it! Thank you for your time. ...

What is the best way to use shortcodes to display custom fields for post objects, specifically products, within WordPress posts

I'm in the process of displaying 'featured products' within my blog posts. These specific products need to be chosen using custom post objects on the backend for each individual post. I've outlined what I believe the PHP code should lo ...

The Next.js Image component does not implement the maxWidth attribute

<Image src="/assets/blog/image.webp" style={{ maxWidth: "700px" }} width={1400} height={1400} /> Issue Detected The image with src '/assets/blog/image.webp' has the following styles applied, but they are being overwrit ...

Enhancing User Experience with Animated jQuery Progress Bar

I came across a cool tutorial on animating progress bars: The only issue was that the progress bar didn't utilize jquery, and there wasn't information on linking multiple buttons to it. After some searching, I found another tutorial that address ...

Align the content evenly on several cards using Material-UI

Trying to work on some frontend coding, but struggling with getting the content justified properly in fixed-height MUI cards. Each card consists of a title, description, divider, and author, but I can't seem to get everything aligned correctly within ...

How can I apply multiple backgrounds to a SVG object?

I am in the process of converting an HTML element into an SVG object. I have made progress, but there is one thing that I am stuck on: I'm having trouble figuring out how to define multiple backgrounds in SVG, specifically when it comes to converting ...

Building a custom server rack table using PHP and MySQL is a great way to optimize your

I am in the process of developing a rack server using PHP and MySQL. My first task is to set up a dynamic table with rowspan functionality, followed by implementing drag-and-drop features using Ajax. Although I have researched various resources and attemp ...

What is the process for utilizing the Bootstrap required field for a type of "button" rather than "submit"?

Check out my code below: <form> <div class="input-group"> <span class="input-group-addon"><i class="fa fa-envelope fa-10x"></i></span> <input type="email" class="for ...