Vertically align text within input field of fixed-height without display: table or padding?

后端 未结 16 2355
Happy的楠姐
Happy的楠姐 2020-12-01 01:27

The line-height property usually takes care of vertical alignment, but not with inputs. Is there a way to automatically center text without playing around with padding?

16条回答
  •  遥遥无期
    2020-12-01 01:32

    In my opinion, the answer on this page with the most votes is the best answer, but his math was wrong and I couldn't comment on it.

    I needed a text input box to be exactly 40 pixels high including a 1 pixel border all the way around. Of course I wanted the text vertically aligned in the center in all browsers.

    1 pixel border top
    1 pixel border bottom
    8 pixel top padding
    8 pixel bottom padding
    22 pixel font size

    1 + 1 + 8 + 8 + 22 = 40 pixels exactly.

    One thing to remember is that you must remove your css height property or those pixels will get added to your total above.

    
    

    This is working in Firefox, Chrome, IE 8, and Safari. I can only assume that if something simple like this is working in IE8, it should work similarly in 6, 7, and 9 but I have not tested it. Please let me know and I'll edit this post accordingly.

提交回复
热议问题