How to position the Placeholder and Search Icon at

2019-03-31 19:59发布

问题:

In my jQuery mobile app , I need to position the search Icon and Place holder of a search input field at the center , I have tried the folowing code but it didnt work , the placeholder and the icon are still appearing at the left on android devices . Please help me How can I Position the SearchIcon and Placeholder at the center of the Search input field ?

<input type="search" id="SearchFilter"  placeholder="Search.." />

 <ul data-role="listview"   data-filter="true"  data-input="#SearchFilter"
    data-split-icon="delete"> 
   // All Elements 
</ul>

CSS

::-webkit-input-placeholder { // its working on jsfiddle but it didnt work on mobile devices

    color: grey; 
    text-align:center;

}
#SearchFilter{

   text-shadow:none; 
   text-align:center;
}


.ui-icon-SearchIcon:after{

   background-image: url(icons/searchIcon.png);
   background-size: 100% 100%;
   background-position: 0 50%;
   width:32px;
   height:32px;
   text-align:center;
   float:center;    
 }

回答1:

Putting placeholder text in the middle:

Do you want tyled text in the middle too?

If Yes:

.ui-input-search input{
    text-align: center;
}

DEMO

If No:

::-webkit-input-placeholder {
   text-align:center;
}
:-moz-placeholder { /* Firefox 18- */
   text-align:center;  
}
::-moz-placeholder {  /* Firefox 19+ */
   text-align:center;  
}
:-ms-input-placeholder {  
   text-align:center; 
}

DEMO

You can place the icon in the center if you like, however it does not disappear when you type, so I think it does not make sense. But if you like:

.ui-input-search:after{
   width:18px;
   height:18px;
   left: 50%;
   margin-left: -50px;
}

DEMO



回答2:

To expand on ezanker's answer, you can also remove the search icon as there is a class applied once the input gains focus, therefore you could rewrite the css to look like (I also styled the icon position more to my liking):

.ui-input-search::after{
   width:18px;
   height:22px;
   left: 50%;
   top: 40%;
   margin-left: -52px;
}
.ui-focus::after{
  left: 10%;
}
.ui-focus ::-webkit-input-placeholder {
   color: white;
   text-shadow: 0px 0px #FFffff;
}
.ui-focus :-moz-placeholder { /* Firefox 18- */
   color: white; 
   text-shadow: 0px 0px #FFffff;
}
.ui-focus ::-moz-placeholder {  /* Firefox 19+ */
  color: white;
  text-shadow: 0px 0px #FFffff;
}
.ui-focus :-ms-input-placeholder {  
   color: white; 
   text-shadow: 0px 0px #FFffff;
}

Demo