Search code examples
cssxhtml

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


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?


Solution

  • In Opera 9.62, Mozilla 3.0.4, Safari 3.2 (for Windows) it helps, if you put some text or at least a whitespace within the same line as the input field.

    <div style="line-height: 60px; height: 60px; border: 1px solid black;">
        <input type="text" value="foo" />&nbsp;
    </div>
    

    (imagine an &nbsp after the input-statement)

    IE 7 ignores every CSS hack I tried. I would recommend using padding for IE only. Should make it easier for you to position it correctly if it only has to work within one specific browser.