Vertically align text within input field of fixed-

2019-01-10 10:28发布

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?

标签: css xhtml
15条回答
一夜七次
2楼-- · 2019-01-10 10:57

I ran into this problem myself. I found that not specifying an input height, but using the font-height and padding combined, results in vertically aligned text.

For instance, lets say you want to have a 42px tall input box, with a font-size of 20px. You could simply find the difference between the input height and the font-size, divide it by two, and set your padding to that amount. In this case, you would have 22px total worth of padding, which is 11px on each side.

<input type="text" style="padding: 11px 0px 11px 0px; font-size: 20px;" />

That would give you a 42px tall input box with perfect vertical alignment.

Hope that helps.

查看更多
smile是对你的礼貌
3楼-- · 2019-01-10 10:57

I've not tried this myself, but try setting:

height : 36px; //for other browsers
line-height: 36px; // for IE

Where 36px is the height of your input.

查看更多
爷的心禁止访问
4楼-- · 2019-01-10 10:59

Go for line-height.

The vertical-align tag works fine for the submit button but not for the text in the input field. Setting line-height to the height of the input field works on all browsers. Incl IE7.

查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-10 11:01

Just don't set the height of the input box, only set the font-size, that will be ok

查看更多
我只想做你的唯一
6楼-- · 2019-01-10 11:04

this example can resolve your problems forever:

add this to the input css property and never add % instead of px:

height:25px;
查看更多
Bombasti
7楼-- · 2019-01-10 11:10

If your element is a block element contained/or with display like so:

display: table-cel

Or, with an fixed line-height, you can set the vertical align like so:

Vertical-Align: Middle;

It won't work for other cases, but it works fine on these conditions.

查看更多
登录 后发表回答