3

I would like to know how to align a search icon, within the text box, same as Google's search bar

Like this:

enter image description here

This is what i got, but idk How to do it:

enter image description here

I am trying to avoid using bootstrap or any library. I want to keep it vanilla as much as I can.

#search-bar {
  width: 32%;
  color: white;
  border-radius: 24px;
  border-style: solid;
  border-width: 1px;
  border-color: white;
  background-color: #2d2d2d;
  height: 44px;
}
<form class="search-bar-form">
  <input id="search-bar" type="text" href="#" />
  <img src="https://via.placeholder.com/24" />
</form>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Nibras Shami
  • 144
  • 2
  • 14
  • mabye switching your styled textbox for a div, then styling it like you did with textbox, adding the image inside and next to it the textbox would be an option? – Dev Sep 17 '21 at 13:44

4 Answers4

0

You can use position: absolute. Like this:

form {
  position: relative;
}

img {
  position: abosolute;
  top: 10px;
  right: 20px;
}

You should update top and right values. This is only example.

Harry Potter
  • 137
  • 7
0

Add some padding-left to the search bar, to move the text more to the right side.

Then add a class to the image and define it like that:

.mySearchIconImage {
    position: absolute;
    top: 10px;
    left: 10px;

Also add position: relative to the form itself, to make sure the img isn't positioned outside of your form.

0

You can place the image as the background of the textbox:

#search-bar {
  width: 32%;
  color: white;
  border-radius: 24px;
  border-style: solid;
  border-width: 1px;
  border-color: white;
  background-color: #2d2d2d;
  height: 44px;
  
  background-image: url(https://via.placeholder.com/24);
  background-position: 10px center;
  background-repeat: no-repeat;
  padding-left: 40px;
}
<form class="search-bar-form">
  <input id="search-bar" type="text" value="text goes after the padding" />
</form>
Kobi
  • 135,331
  • 41
  • 252
  • 292
-1

There are several ways. One way is with absolute position inside a relative position parent...

body {
  margin: 0;
}

#search-bar {
  width: 32%;
  color: white;
  border-radius: 24px;
  border-style: solid;
  border-width: 1px;
  border-color: white;
  background-color: #2d2d2d;
  height: 44px;
  position: relative;
  padding-left: 44px;
}

.search-icon {
    position: absolute;
    left: 0;
}
<form class="search-bar-form">
    <input id="search-bar" type="text" href="#" />
    <img src="https://img.icons8.com/material-outlined/50/000000/search.png" class="search-icon" />
</form>

Demo


Another way is using relative position and a negative margin. For this, put the img before the input in your markup.

#search-bar {
  width: 32%;
  color: white;
  border-radius: 24px;
  border-style: solid;
  border-width: 1px;
  border-color: white;
  background-color: #2d2d2d;
  height: 44px;
  padding-left: 44px;
}

.search-icon {
  position: relative;
  margin-right: -60px;
  z-index: 1;
  vertical-align: middle;
}
<form class="search-bar-form">
    <img src="https://img.icons8.com/material-outlined/50/000000/search.png" class="search-icon" />
    <input id="search-bar" type="text" href="#" />
</form>
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624