1

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?

scooterlord
  • 15,124
  • 11
  • 49
  • 68
badm
  • 219
  • 2
  • 13

2 Answers2

1

You can do something like this in CSS:

.search  input {
   background-size: 30px 17px;
   background-repeat: no-repeat;
   background-position: left;
   transition: .3s;
   background-image: url("https://i.stack.imgur.com/VtucQ.jpg");
 }

.search input:focus {
     background-position: left 50px; 
  }

and html

<div class="search">
    <input>
</div>

Background size is 30x17 beacuse image will not lose ratio. And on focus just animate position from image to hide it.

mocni_T
  • 179
  • 4
0

you can use focus in .when someone enter to the textbox hide the image that simple. set the display none to the image

Rahees Ahmed
  • 23
  • 10