通過css控制html文本框中的文字垂直居中

字號:


    當定義了 text 的 height 屬性時,在 text 中輸入的文字都不是垂直居中的,不過你可以添加css來控制它,讓輸入的文字垂直居中,使網(wǎng)頁更完美。
    代碼如下:
    html>
    <head>
    <style type=text/css>
    #text {
    height:20px;
    vertical-align:middle;
    line-height:20px; /*line-height must be equal to height*/
    }
    </style>
    </head>
    <body>
    <table>
    <input type=text id=text>
    </table>
    </body>
    </html>
    加入 verticla-align、line-height 兩個屬性后,文本框中的文字就在文本框中垂直居中了,要注意的是 line-height 必須要等于 height 。