Insert an icon inside a text field, HTML Input

You cannot safely place an image in an input text field. The same works in some browsers and it works for you, but I don’t think it works always. What I would do is mount it in a current element, placing the input inside the element and putting the background in that element. All with CSS.

The HTML you would use would be like this:

The CSS then uses an image icon as the background. You have to use the icon that best suits you with the color you need in your interface. You place it and position it inside the element, on the left side, by padding the

, so that the input field doesn’t sit on top of the background.

Another detail is that the input removes the border. You put the border on the

element, as if to mimic the border the input would have, and the borderless input will make it look like the

is the input field.

The CSS could be more or less the following:

.input-icon { background-image: url(‘success.png’); background-repeat: no-repeat; background-position: 4px center; background-size: 20px; display: flex; align-items: center; width: 300px; padding-left: 28px; height: 30px; border: 1px solid rgba(9, 113, 163, 1); border-radius: 3px; } .input-icon input { width: 100%; font-size: 0.9em; border: none; } .input-icon input:focus { outline: none; }

You don’t need Javascript for this to work. But I think that one thing that could be useful for you is that, when you click on the

, you bring the focus of the application into the input field.

document.querySelector(‘.input-icon’).addEventListener(‘click’, function() { this.querySelector(‘input’).focus(); });

See also  Cursors in PL/SQL part one

The most interesting thing would be for you to make a component with this user interface, since that way you could reuse it as many times as you need. You can make a custom element of web components, a jquery plugin or whatever suits you.

Loading Facebook Comments ...
Loading Disqus Comments ...