Guide to adding an icon within an HTML `<input>` element

Is anyone able to help me successfully place my icon font inside the search input field? I've tried adjusting the margin and padding, but nothing seems to be working!

If you have any insights or suggestions, please let me know. Your help is greatly appreciated.

Please check out the issue in this fiddle:

http://jsfiddle.net/Tj7nJ/1/

This is a snippet of my HTML code:

<li id="sb-search" style="float:right; list-style:none; height:20px; bottom:3px; ">
        <form id="search">
          <input name="q" type="text" size="40" placeholder="Search..." />
          <button type="submit"><i class="fa fa-search"></i></button>
        </form>
    </li>
    

And here's a snippet of my CSS:

#search {

        outline:none ;
        border:none ;

    }

    #search button[type="submit"] {

        width: 20px;
        background:none;
        cursor:pointer;
        height:20px;
    }

    button[type="submit"]>i:hover
    {     
        color: #fff;

    }   

    button[type="submit"]>i
    {
        background:none;
        color:#ccc;
        font-size:1.1em; 
    }


    #search input[type="text"] 
    {
        border: 0 none;
        background: #141d22;
        text-indent: 0;
        width:110px;
        padding: 6px 15px 6px 35px;
        -webkit-border-radius: 20px;
        -moz-border-radius: 20px;
        border-radius: 20px;
    }
    

Answer №1

Placing an image inside an <input type='text'> element is not possible.

But you can eliminate the borders of the input using:

input[type='text'] {
    background-color:transparent;
    border: 0px solid;
}

and then enclose it with the icon in a div and apply new styles.

Sample code:

Check out this helpful jsFiddle link: http://jsfiddle.net/hxjY7/

Code snippet below:

<style type="text/css">
    #textBoxWrapper{
        border: 1px solid black;
        width: 172px;
        height: 20px;
    }

    #textBox {
        background-color:transparent;
        border: 0px solid;
        width: 150px;
        height: 20px;
        float:left;
    }
    #icon{
        width: 20px;
        height: 20px;
        background-color: grey;
        float:left;
    }
</style>

<div id="textBoxWrapper">
<div id="icon"></div>
<input type="text" id="textBox" />
</div>

Answer №2

If you want to adjust the positioning of the button within the input field, one way to do it is by setting the button to have a position of absolute (and making the search wrapper relative):

http://jsfiddle.net/K7hRt/4/

#search {
    outline: none;
    border: none;
}

#search {
    position: relative;
    z-index: 1;
}

#search button[type="submit"] {
    width: 25px;
    background: none;
    cursor: pointer;
    height: 25px;
    z-index: 2;
    position: absolute;
    top: 5px;
    right: 15px;
}

button[type="submit"]>i:hover {
    color: #eee;
}

button[type="submit"]>i {
    background: none;
    color: #999;
    font-size: 1em;
}

#search input[type="text"] {
    border: 0 none;
    background: #1c282d;
    text-indent: 0;
    width: 120px;
    padding: 8px 18px 8px 40px;
    -webkit-border-radius: 25px;
    -moz-border-radius: 25px;
    border-radius: 25px;
}

Answer №3

It seems like this could be the solution you are looking for. Example Link

HTML Code:

<form id="search-box" action="#" method="post">
    <fieldset>
        <input type="text" id="query" name="query" size="30" placeholder="Search here..." />
        <button type="submit"><i class="fa fa-search"></i>
        </button>
    </fieldset>
</form>

CSS Code:

form#search-box fieldset {
    position: relative;
    border: 1px solid #ccc;
}
form#search-box input[type='text'] {
    padding: 6px;
}
form#search-box button {
    position: absolute;
    top: 10px;
    left: 280px;
}

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

I'm looking for a way to retrieve an object from a select element using AngularJS. Can

I am working with a select HTML element that looks like this: <select required="" ng-model="studentGroup"> <option value="" selected="">--select group--</option> <option value="1">java 15-1</option> <option value="2">ja ...

The function element.checked = true/false in Vanilla Javascript seems to be malfunctioning

Here is the HTML code I am working with: <ul class="nav md-pills pills-default flex-column" role="tablist"> <li class="nav-item"> <input type="radio" name="q01" value="1" hidden checked> <a class="nav-link choice01 ...

Updating and managing the CSS style for button states

In my asp.net application, I have 5 list items functioning as tabs on a Masterpage. When a user clicks on a tab, I want to redirect them to the corresponding page and visually indicate which tab is active by changing its class. What is the most effective ...

What is causing the issue with using classes in Javascript to clear the form?

<!DOCTYPE html> <html> <head> <title>Onreset</title> </head> <body> <form> Username: <input type="text" class="abc"><br><br> Password: <input type ...

Exploring Webpack: Unveiling the Contrasts Between Production and Development CSS Bundles

Can you explain the differences in CSS between production and development when compiling with webpack? I've noticed that the production stylesheet seems to consider the entire website, whereas the development mode appears to only focus on the specifi ...

Updating the Navigation Bar and Theme in CRM Dynamics 2013 to Reflect Your Organization's Branding

In my CRM Dynamics 2013 setup, I am faced with a unique challenge. I need to customize the Organization navigation bar based on which organization is currently loaded. Specifically, I have two organizations - QA and PROD. When a user switches to the QA org ...

Using Paypal API in PHP to create a payment form

<?php $action=$_REQUEST['action']; if ($action=="") { ?> <center> <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type=" ...

Initial space in model variable not being displayed

Hey there, I am working with a model variable called "name" that is linked to a span element like this: <input type="text" ng-model="name"> <span ng-bind="name"></span> My goal is to display the text entered in the input field without r ...

What are the steps to stop the left alignment of header text?

As I'm working on designing a unique Markdown theme in CSS, I am currently facing some challenges with regards to the styling of headers. Specifically, the h1 header is styled as follows: h1 { border-top: 1px solid black; width: 10%; margin: 0 ...

Firebase hosting adjusts the transparency of an element to 1% whenever it detects the presence of CSS opacity

I'm currently working on a website using Vue.js and Firebase. Everything runs smoothly locally, including a button with a desired low opacity. However, when I deploy the site to Firebase Hosting, the button's opacity inexplicably changes to 1% an ...

Locate a Sub-Child href Element using Selenium

I am attempting to interact with a link using Selenium automation tool. <div id="RECORD_2" class="search-results-item"> <a hasautosubmit="true" oncontextmenu="javascript:return IsAllowedRightClick(this);" class="smallV110" href="#;cacheurl ...

Utilizing PNG images with color in CSS, HTML, or JavaScript

On my website, I have an image in PNG format. I am wondering if there is a way to change the color of the image using HTML5, JavaScript, or CSS. Ideally, I would like the image to be changed to white by inverting its colors (changing black to white, not al ...

Issues with zoom functionality not functioning properly within IE11

I am currently developing an application with Angular that is designed to be compatible with tablets and touch-enabled devices. One of the key features I want to implement is the ability for users to zoom/scale up the app, especially for those with visual ...

What is the process for transforming information from a database into a clickable hyperlink?

My course list includes the following: SE1111 SE2222 SE3333 I want to display this list on my webpage and have it redirect to a page where the course name is saved for future use. Here's what I've tried so far: <div id="join_courses" clas ...

Update the picture displayed in the parent div when it is in an

My code currently changes the image when a parent div is in an active state, but I'm struggling to figure out how to revert it back to the original image when clicked again. I attempted to use 'return false' at the end of the script, but it ...

The color scheme detection feature for matching media is malfunctioning on Safari

As I strive to incorporate a Dark Mode feature based on the user's system preferences, I utilize the @media query prefers-color-scheme: dark. While this approach is effective, I also find it necessary to conduct additional checks using JavaScript. de ...

Using jQuery to target form elements that have a specific class assigned to them

I am facing an issue with targeting input fields within a specific form on a page. The form has a certain class, let's say "has-error," and I want to remove this class from all the input fields in that form. I attempted the following code snippet: t ...

Unable to implement Bootstrap 5 Hamburger Animation - Looking for solutions

SOLVED I successfully resolved the issue :) The challenge was: $(function () { $('.navbar-toggler-button').on('click', function () { $('.animated-hamburger').toggleClass('open'); //Chrome expert mode ...

What is the method for adjusting the subheader color on a Material UI card component?

How can I change the subheader text color to white in a Material UI Card with a dark background? Here is my code: const useStyles = makeStyles(theme => ({ menuColor: { backgroundColor: theme.palette.primary.main, color: '#ffffff', ...

What is the best way to verify the font-family using JavaScript?

To verify if the user has installed the font family, we can run a JavaScript function with AngularJS. I am using a Typekit font and have only loaded this service for users who do not have this specific font. ...