Making a background-image trigger a form submit is not pretty.

A better approach is to place a regular submit button outside the input, then style things to make it look like the button is inside. That will also preserve accessibility (e.g. blind users will be able to use your website), and pressing Enter will submit your form automatically across all browsers.

See the below code, or check out this jsFiddle for a working proof-of-concept.

<style type="text/css">
.search_field {
    display: inline-block;
    border: 1px inset #ccc;
}

.search_field input {
    border: none;
    padding: 0;
}

.search_field button {
    border: none;
    background: none;
}
</style>

<div class="search_field">
    <input name="q" />
    <button type="submit"><img src="magnifying_glass.png" alt="Search" /></button>
</div>
Answer from Søren Løvborg on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 22954974 › how-can-i-make-search-icon-clickable
jquery - How can i make search icon clickable? - Stack Overflow
May 23, 2017 - As a workaround, you can insert an invisible DOM element (e.g. SPAN) on top of the icon and add a click event to that element: <form method="post"> <input name="search" id="search" value="" placeholder="Buscar" type="search" onfocus="this.p...
🌐
Stack Overflow
stackoverflow.com › questions › 73461980 › adding-a-search-icon-inside-search-bar
html - adding a search icon inside search bar - Stack Overflow
<img src="fileInsideYourPc/nam... your search bar. Again, you can add a class="searchIcon" and then call it inside your css and add to it "cursor:pointer" and some other sizing options....
Discussions

html - Clickable search icon in background - Stack Overflow
The form can only be submit by pressing enter button. What would be the easiest way to make the search icon clickable ? More on stackoverflow.com
🌐 stackoverflow.com
Android - Make whole search bar clickable - Stack Overflow
The problem is only the search icon is clickable other area in the search bar is not clickable, when i click the icon only i can able to search. Can you please help me to make the whole search area clickable. More on stackoverflow.com
🌐 stackoverflow.com
how to put a clickable icon in html input box
I would put the html elements right next to each other, then relative position it, right:15px or something... This should lay it overtop the input. More on reddit.com
🌐 r/web_design
16
9
October 3, 2015
Click on Search icon to make search bar appear - Customize with code - Squarespace Forum
Site URL: https://caper-badger-3s6d.squarespace.com/blog I currently have a cusstommade search icon (made in illustrator), I only want this search icon to be visible, and when clicked on, the search bar appears. I also want to make this icon a bit smaller. Can anyone help me with this? More on forum.squarespace.com
🌐 forum.squarespace.com
January 18, 2021
Top answer
1 of 6
54

Making a background-image trigger a form submit is not pretty.

A better approach is to place a regular submit button outside the input, then style things to make it look like the button is inside. That will also preserve accessibility (e.g. blind users will be able to use your website), and pressing Enter will submit your form automatically across all browsers.

See the below code, or check out this jsFiddle for a working proof-of-concept.

<style type="text/css">
.search_field {
    display: inline-block;
    border: 1px inset #ccc;
}

.search_field input {
    border: none;
    padding: 0;
}

.search_field button {
    border: none;
    background: none;
}
</style>

<div class="search_field">
    <input name="q" />
    <button type="submit"><img src="magnifying_glass.png" alt="Search" /></button>
</div>
2 of 6
9

2017 update CSS3 HTML5 (and optionally bootstrap)

jsfiddle

jsfiddle with tick box

I didn't like the aesthetics of the current answer, anyone arriving here looking for a solution using either bootstrap or font-awesome (or your own SVG graphic).

The example works without bootstrap, however the font-icon for the search symbol won't be there.

css

html {
  /* to make rem sizes behave */
  font-size: 10px;
}

.input-with-icon {
  /* causes absolute icon div to be positioned correctly */
  position: relative;

  width: 25rem;
  height: 3.2rem;
  box-sizing: border-box;
}

.input-with-icon .form-control {
    height: 100%;
    width: 100%;
    padding-right: 3.65rem;
    box-sizing: border-box;
}

.input-with-icon .icon {
  position: absolute;

  /* These are set relative to the height of the input box to bound the box neatly inside. This is aesthetic to me but you may change the dimensions of course. */
  right: 0.3rem;
  top: 0.3rem;
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 0.3rem;

  /* content in the icon div is centered, without bootstrap or font-awesome you may wish to add your own text in the span */
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
}

html

<div class="input-with-icon">
  <input type="text" class="form-control" value="thing">
  <div class="btn btn-default icon"><span class="glyphicon glyphicon-search"></span>
  </div>
</div>

Add a handler for the icon to cause a submit action to occur as desired, this is beyond the scope of this question though...

🌐
Stack Overflow
stackoverflow.com › questions › 34230862 › clickable-search-icon-in-background
html - Clickable search icon in background - Stack Overflow
I have a search field with a search icon in the background. Here's the CSS: input[type=search] { outline: none; -webkit-appearance: textfield; -webkit-box-sizing: content-box; ...
🌐
CodePen
codepen.io › vingroho › pen › KrBLQV
Clickable search box
<html> <head> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <!-- Define your search form --> <form class="navbar-form navbar-left" role="search"> <!-- Define a button to toggle the search area --> <button id='search-button' class='btn btn-default '><span class='glyphicon glyphicon-search'></span></button> <!-- Create your entire search form --> <div id='search-form' class="form-group"> <div class="input-group"> <span id='search-icon' class="input-group-addon"><span class='glyphicon glyphicon-search'></span></span> <input type="text" class="form-control" placeholder="Search"> </div> </div> </form> </div> </nav> </body> </html>
Find elsewhere
🌐
Nikita Hlopov
nikitahl.com › search-icon-inside-input
Create a search icon inside input box with HTML and CSS
December 3, 2021 - 💡 NOTE: To improve UX you can also specify an input placeholder e.g. "Search…". This will give a user an additional hint that this is indeed a search input. Finally, the button type attribute should have a submit value. The proper button type will ensure the form can be submitted via button click without additional events for this button. The button will also act as an icon.
🌐
Squarespace Forum
forum.squarespace.com › home › customize with code › click on search icon to make search bar appear
Click on Search icon to make search bar appear - Customize with code - Squarespace Forum
January 18, 2021 - Go to Safari → Preferences. Click the Websites tab. Select Notifications in the sidebar. Find this website and adjust your preference. Tap the lock icon next to the address bar.
🌐
W3Schools
w3schools.com › howto › howto_css_search_button.asp
How To Create a Search Button
Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete Turn off autocomplete Turn off spellcheck File Upload Button Empty Input Validation
Top answer
1 of 2
1

The issue is that the input element is over the icon, inspecting the element with the console you could see that the input element has some padding, so clicking there you're clicking on the input element.

No matter which component you use, a lightning-icon, a lightning-button-icon or a standard button, it would be the same.

You just have to move those elements:

<label class="slds-form-element__label" for="combobox-id-3">{lookupLabel}</label>
<div class="slds-is-relative">
    <div data-key="searchdiv" class="slds-combobox__form-element slds-input-has-icon slds-input-has-icon_right" role="none">  
        <input type="text" onfocusout={onLeave} value={searchKey} onkeyup={handleKeyChange} onchange={handleKeyChange} class="slds-input slds-combobox__input slds-has-focus" id="combobox-id-3" aria-autocomplete="list" aria-controls="listbox-id-2" role="textbox" placeholder="Search..." />   
    </div>
    <span style="right: 0.5rem; top: 0.5rem; cursor: pointer;" class="slds-is-absolute slds-icon_container slds-icon-utility-search slds-input__icon slds-input__icon_right">  
        <lightning-icon icon-name="utility:add" size="xx-small" class="slds-icon-text-default" onclick={handleAdd} aria-hidden="true"></lightning-icon>
    </span> 
</div>

Here I created a new container with a relative positioning (slds-is-relative) which includes the searchdiv and the span with the icon. Then I setted an absolute positioning (slds-is-absolute) to the span element and provided some css. Finally an handler for onclick event has been attached to the lightning-icon.

An element with position absolute is removed from the normal document flow, and no space is created for the element in the page layout. The element is positioned relative to its closest containing block, the first ancestor that has a position value other than static, and its final position is determined by the values of top, right, bottom, and left.
Keep in mind that if an element's position is not explicitly defined, then it's static by default. That's why I had to create a container with a relative positioning (using slds-is-relative) and I explicitly setted top and right values of the span element.
By the way, z-index, top, right, bottom, and left have no effect on a static element.

MDN - Positioning reference.

2 of 2
1

You could use a 'lightning button icon'

<lightning-button-icon icon-name="utility:settings" alternative-text="Settings" title="Settings" onclick={handleClick}></lightning-button-icon>

Here the onclick handler will invoke your js code.

Here is a detailed documentation on it : https://developer.salesforce.com/docs/component-library/bundle/lightning-button-icon/example

🌐
Stack Overflow
stackoverflow.com › questions › 54859211 › how-to-clickable-search-icon-in-form-input-field-using-css
php - how to clickable search icon in form input field using css - Stack Overflow
February 25, 2019 - I insert a search icon on the form input field. Like this #searchform { position: relative; } #searchform:before { content: "\f118"; font-family: "pharma"; right: 0px; positi...
Top answer
1 of 5
66

You need to use Input Adornments.

For example:

// imports
import IconButton from "@material-ui/core/IconButton";
import InputAdornment from "@material-ui/core/InputAdornment";
import SearchIcon from "@material-ui/icons/Search";

// render

<TextField
  label="With normal TextField"
  InputProps={{
    endAdornment: (
      <InputAdornment>
        <IconButton>
          <SearchIcon />
        </IconButton>
      </InputAdornment>
    )
  }}
/>

Here is a demo:

const {
  TextField,
  InputAdornment,
  IconButton,
  SearchIcon,
  Icon
} = window['material-ui'];

class App extends React.Component {
  render() {
    return (
      <TextField
        label="With normal TextField"
        InputProps={{
          endAdornment: (
            <InputAdornment>
              <IconButton>
                <Icon>search</Icon>
              </IconButton>
            </InputAdornment>
          )
        }}
      />
    );
  }
}

ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://unpkg.com/react@latest/umd/react.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/react-dom@latest/umd/react-dom.development.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/@material-ui/core/umd/material-ui.development.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

<div id="root"></div>

2 of 5
15

You simply need to import inputAdornment

import InputAdornment from '@material-ui/core/InputAdornment';

and add InputProps to textField like this

InputProps={{
  endAdornment: (
    <InputAdornment position="start">
      <SearchIcon />
    </InputAdornment>
   )
  }}

plz find the attached img for demo of your required solution.

🌐
Codeconvey
codeconvey.com › home › how to open search box onclick with css
How to Open Search Box OnClick with CSS | Codeconvey
August 28, 2023 - To handle the toggle function, we need to take a little help from Javascript toggle function. Let’s initialize the jQuery by using the document ready function and use the click function for search icons.
🌐
Reddit
reddit.com › r/css › how do i make a search bar with an icon inside it like this
how do I make a search bar with an icon inside it like this : r/css
March 30, 2023 - A community for discussing about CSS (Cascading Style Sheets), Web Design and surrounding relevant topics. Feel free to discuss, ask questions, share projects and do other things related to CSS here. ... Sorry, this post was deleted by the person who originally posted it. Share ... <input type="search" basically does this anyway.
Top answer
1 of 4
6

Overview

You created this UI element to allow users to easily clear their search input criteria. In most cases users who use their keyboard to interact with the form will be choosing the search button over the clear button as this is the primary action. By using keyboard navigation your first concern would be your indexing order, should clear come before search or not at all?

Tabable?

In my opinion good design is inclusive, some users may be using assistive technology. Removing clear from the index removes this feature from these users. That being said, yes you should make the clear button indexable.

Clear before search or search before clear?

It has become commonplace that to complete the primary action in a form you press Enter. In most cases users will expect that the tabbing order does not change from common designs (top to bottom, left to right). The user should tab into the input field, tab to access clear, and finally tab to access search. At any time you can press enter to submit the search as long as your focus is in the form (input field).

Things to consider

  • Information Architecture

Since "clear" is specific to this input field and not the form (even though the form only has one input field), it makes sense to put the clear button inside the input field (very common UI architecture Parent->child). However, the "Search" icon is your form's primary action. It makes more sense to put this outside of your input field. This will keep all forms following the same information architecture.

  • Textual Call to action

If there see search results, does "clear" also remove the results or just the input in the form? The "Close", "Delete", "Stop" icon is holistic and is not representative of exactly what the action is you're trying to inform the users of. Consider removing this icon and just use text to tell users what this does. Perhaps use a tool tip, but don't forget to use alt="" attribute for those on screen readers.

The same consideration could apply to the search call to action. Right now you're using a label to inform us of the input fields action not what the input field is. However, you can achieve this by using "Search" as the action and not the label. This again will keep all forms following the same patterns and removes any ambiguity.

Since search is at the top of your site, users read top to bottom, left to right. At the time of seeing this input field, they may have little context of what it does. Does this search products, contact info, blogs, search engines? Try to let your call to actions be clear and concise, for example "search products". If there is not a lot of room, you can use placeholder text to convey this to the users.

Never make the user think. "Don't make me think"

2 of 4
1

As a user I would expect, and I assume you're apply tab as a verb when you say "tabbable," the search to be performed when I tapped or clicked the search icon or the control lost focus.

I would expect the clear button to require and physical tap to clear the input.

If you included the clear button in the tab list, it strikes me that it would become quite irritating and people would inadvertently delete their search term.

🌐
YouTube
youtube.com › watch
How to Add Search Icon in Input Field in HTML - YouTube
In this video, you will learn how to add search icon in input field in html. We will show how you can add search icon inside the input box at beginning as we...
Published   August 7, 2024
🌐
SoloDev
solodev.com › blog › web-design › creating-a-toggled-search-bar.stml
Creating a Toggled Search Bar
December 27, 2016 - This article teaches you how to add a toggled search bar to your website navigation that populates a search form onClick.