站长技术网

首页 > 网站基础 > DIV&CSS >

通过css控制HTML文本框中的文字垂直居中

点评:当定义了 Text 的 height 属性时,在 Text 中输入的文字都不是垂直居中的,不过可以通过css来控制它,下面有个不错的示例,大家可以参考下当定义了 Text 的 height 属性时,在 Text 中输入的文字都不是垂直居中的,不过你可以添加CSS来控制它,让输入的文字垂直居中,使网页更完美。

复制代码代码如下:

<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 。