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

Ways to extract the coordinates for slices positioned around the circumference of a pie chart

Currently, I am working on designing a pie chart with D3 using d3.layout.pie(). The image resembles the one displayed below, but without the black dots. These dots were added manually in Photoshop to highlight an issue that I am facing. I am curious about ...

Discover the secret to easily displaying or concealing the form of your choice with the perfect fields

Greetings! I require assistance in understanding how to hide or display a form. Specifically, I have two forms - studentForm and EmployeeForm. When selected from the dropdown list profileType, I want the corresponding form to be displayed. I am facing an ...

Is the form data in AngularJS not submitting correctly?

Why is my HTML form data not being submitted using POST method? HTML View Page:- <div class="col-sm-12"> <div class="card-box"> <form class="form-inline" name="addstock" ng-submit="saveStockPurchaseInvoice()"> <h ...

endless cycle of scrolling for div tags

My goal is to incorporate a tweet scroller on I believe it uses the tweet-scroller from Unfortunately, this link seems broken as the demo is not functioning. I searched for an alternative solution and came across http://jsfiddle.net/doktormolle/4c5tt/ ...

Customize the keyboard on your iPod by replacing the default one with a

Looking to customize the iPOD keyboard to only display numbers, similar to a telephone keypad: https://i.stack.imgur.com/X0L3y.png The current iPOD default keyboard looks like this: https://i.stack.imgur.com/VykjU.png ...

Invoking a static method within a cshtml document

I am currently working on implementing a clickable DIV within a vertical tab panel. My goal is to have a specific static method called when the DIV is clicked. Here is what I have done: <div class="tabbable tabs-left"> <ul class="nav nav-tabs"> ...

Apply the CSS style to make one element identical to another, while modifying a single property

I am currently working with the following CSS: input[type="text"] { border: 2px solid rgb(173, 204, 204); height: 31px; box-shadow: 0px 0px 27px rgb(204, 204, 204) inset; transition:500ms all ease; padding:3px 3px 3px 3px; } My goal i ...

Content and visual side by side

My logo and header image are giving me trouble. Whenever I attempt to center the header image, it keeps moving to the next row. How can I make sure it stays on the same row? Check out our website Thank you ...

Ways to Press the Enter Key on Different Browsers

Looking for a solution to get the keypress action working properly? I have a chat feature where I need to send messages. Here is what I have in the Form-tag (JSP based): function SendMessage() { if (event.keyCode===13) { ale ...

The CSS child selector seems to be malfunctioning as it is affecting all descendants of the specified type

Struggling with creating a menu containing nested lists. Attempted using a child selector (#menu-novo li:hover > ul) to display only immediate descendants, but all are still showing. Any suggestions or solutions for this issue? #menu-novo-container { ...

Avoiding HTML code within XML

I'm currently experiencing an issue with an invalid XML character appearing during interaction with a SOAP web service. When sending a formatted HTML message to a specific web service, I encountered a failed message that read as follows: Fault messa ...

Can I use Javascript to access a span element by its class name?

Similar Question: How to Retrieve Element By Class in JavaScript? I am looking to extract the text content from a span element that does not have an ID but only a specific class. There will be only one span with this particular class. Can anyone guide ...

Choose the first element of its type and disregard any elements nested within it

I need to change the color of only the first p element within a specific div. Using :first-child would work in a simple example, but in my project, there are multiple elements and more could be added in the future. I want to avoid using :first-child alto ...

What is the best way to format a condensed script into a single line?

There are times when the script in the web browser is packed into one line like function a(b){if(c==1){}else{}}. I have attempted to locate something that would display it in a more normal format. function a(b) { if(c==1) { } else { } } Howev ...

How can I set a background image to automatically adjust to the width of the window, be full height, allow for vertical scrolling, and

How can I set a full-screen background image that adjusts to the body width without horizontal scrolling, maintains height proportionate to width, and allows for vertical scrolling? Here is my current code: html, body { margin: 0px; padding: 0px; } bo ...

Transfer certain options to another selection except for the one currently chosen in the first one

My task is to use jQuery to generate multiple select elements. Each new select should copy the options from the previous one, excluding any that have already been selected by the user. This process should occur every time an option is changed. Select opti ...

Ways to increase the font size of input text using bootstrap's css framework

I currently have a bootstrap textbox set up like this: <input type="text" class="span9" required name="input_text"/> My goal is to increase the height of my input box to 300px. The width, on the other hand, is being handled by span9. I utilized In ...

The Ineffectiveness of Social Media Sharing in WordPress

I'm encountering an issue with adding social media sharing buttons to my website's product page. Although I've implemented the PHP code, clicking on the Facebook button only redirects to a page displaying a share button and URL, but it doesn ...

Issue with submitting input fields that were dynamically added using jquery

I created a table where clicking on a td converts it into an input field with both name and value. The input fields are successfully generated, but they do not get submitted along with the form. In my HTML/PHP : <tr> <f ...

What is the reason for using grid-lines instead of row and column numbers to position an item on the CSS grid?

During a recent tech presentation I delivered at my workplace on the new CSS grid spec, my manager posed an intriguing question that left me stumped. He wanted to know why elements positioned within a grid are identified based on the grid lines they fall b ...