I have an input text field served to be a search bar, and I want to place a magnifier glass to the left, but when the input text field gets focus, the glass should disappear.
.search .form-item input {
background-image: url("https://i.stack.imgur.com/VtucQ.jpg");
background-size: 70% 70%;
background-position: -105% center;
background-repeat: no-repeat;
}
.search .form-item input:focus {
background-image: none; }
<form action="/search" method="get" id="views-exposed-form-search-page-page-search" accept-charset="UTF-8">
<div class="form--inline clearfix">
<div class="js-form-item form-item js-form-type-textfield form-type-textfield js-form-item-keys form-item-keys">
<label for="edit-keys">Fulltext search</label>
<input data-drupal-selector="edit-keys" type="text" id="edit-keys" name="keys" value="" size="30" maxlength="128" class="form-text">
</div>
</div>
</form>
<div class="search">
<input>
</div>
I wanted to achieve it using css:
.navigation .search input {
background-image: url("../images/search.svg");
background-size: 70% 70%;
background-position: -105% center;
background-repeat: no-repeat;
}
.navigation .search input:focus {
background-image: none; }
The code basically works. The thing is that I need to make the background-image
a bit smaller, what I achieved by background-size: 70% 70%
but it mixes up with the position of the image -it appears in the center. Then I had to play with background-position:-105% center
property, what shifted the icon to the left.
My question is if it is a good practice to use a negative percentage to specify the position of the background-image
or are there any better ways?