Center HTML Input Text Field Placeholder

前端 未结 10 1723
半阙折子戏
半阙折子戏 2020-11-28 03:53

How can I centre the input field\'s placeholder\'s alignment in a html form?

I am using the following code, but it doesn\'t work:

CSS ->

inpu         


        
相关标签:
10条回答
  • 2020-11-28 04:49

    It works fine for my needs, you should check compatibility for your browser, I didn't have issues because i didn't care for backward compatibility

    .inputclass::-webkit-input-placeholder {
    text-align: center;
    }
    .inputclass:-moz-placeholder { /* Firefox 18- */
    text-align: center;  
    }
    .inputclass::-moz-placeholder {  /* Firefox 19+ */
    text-align: center;  
    }
    .inputclass:-ms-input-placeholder {  
    text-align: center; 
    }
    
    0 讨论(0)
  • 2020-11-28 04:51

    By using the code snippet below, you are selecting the placeholder inside your input, and any code placed inside will affect only the placeholder.

    input::-webkit-input-placeholder {
          text-align: center
    }
    
    0 讨论(0)
  • 2020-11-28 04:52

    The HTML5 placeholder element can be styled for those browsers that accept the element, but in diferent ways, as you can see here: http://davidwalsh.name/html5-placeholder-css.

    But I don't believe that text-align will be interpreted by the browsers. At least on Chrome, this attribute is ignored. But you can always change other things, like color, font-size, font-family etc. I suggest you rethinking your design whether possible to remove this center behavior.

    EDIT

    If you really want this text centered, you can always use some jQuery code or plugin to simulate the placeholder behavior. Here is a sample of it: http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html.

    This way the style will work:

    input.placeholder {
        text-align: center;
    }
    
    0 讨论(0)
  • 2020-11-28 04:53

    You can use set in a class like below and set to input text class
    CSS:

     .place-holder-center::placeholder {
            text-align: center;
        }
    

    HTML:

    <input type="text" class="place-holder-center">
    
    0 讨论(0)
提交回复
热议问题