简体   繁体   中英

Stop placeholder / input text from overlapping background image used as icon?

I'm creating a search input and am using a backimage css as the search icon.

However this causes the placeholder and content to overlap the backimage on the left.

Is there a way I can make the text inside the div start from the after the background image icon? If you put in a longish search, and then unfocus the input you will see it overlap.

jsfiddle:

http://jsfiddle.net/ET4e7/

Code:

<form class="searchform" action="search.php?" method="get">
  <input type="text" id="filtersearch" name="st" placeholder="search..." />
</form>

#filtersearch {
 width: 100px;
 padding-right: 5px;
 border-radius: 0px;
 border:none;
 background-color: #555;
 color:#fff;
 font-size: 15px;
 float:left;
 text-align: center;
 Display: table;
 margin-left: 0%;
 height: 100%;
 margin-top: 0px;
 -webkit-appearance: none;
 background-image: url('http://buildsanctuary.com/images/icons/searchicon.png');
 background-repeat: no-repeat;
}

#filtersearch:focus {
 text-align: center;
 width: 100%;
 outline: none;
 background-color: #555;
 -webkit-transition: width 200ms;
 -moz-transition: width 200ms;
 -o-transition: width 200ms;
 -ms-transition: width 200ms;
 transition: width 0.25;
 display: block;
}

.searchform {
 width: 200px; 
 float:left; 
 height: 35px;
}

*Note, haven't decided on a max char length yet.

You just need to add padding-left: 20px; to the input (or however large the icon actually is):

#filtersearch {
    width: 100px;
    padding-right: 5px;
    border-radius: 0px;
    border:none;
    background-color: #555;
    color:#fff;
    font-size: 15px;
    float:left;
    text-align: center;
    Display: table;
    margin-left: 0%;
    height: 100%;
    margin-top: 0px;
    -webkit-appearance: none;
    background-image: url('http://placehold.it/20x37');
    background-repeat: no-repeat;
    padding-left: 20px; /* <-------------------------------------------------------- */
}

Fiddle : Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM