手机
当前位置:查字典教程网 >网页设计 > Div+Css教程 >css 实现文字垂直居中
css 实现文字垂直居中
摘要:分两种情况:fixheight:即垂直居中的元素高度已知,这个比较简单,也不需要额外的辅助元素。i'mMr.Middle.提示:您可以先修改...

分两种情况:

fix height:

即垂直居中的元素高度已知,这个比较简单,也不需要额外的辅助元素。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="cache-control" content="no-cache" /> <style> <!-- html, body { height: 100%; margin: 0; padding: 0; } body { position: relative; } #div { background: blue; color: #fff; position: absolute; top: 50%; left: 50%; height: 240px; width: 240px; margin: -120px 0 0 -120px; } --></style> </head> <body> <div id="div"> i'm Mr. Middle. </div> </body> </html>

提示:您可以先修改部分代码再运行

variable height:

居中元素高度可变,这个需要额外的一个wrapper元素,用table-cell的方式来模拟表格的居中实现,当然,早期的ie又是另一招,具体可见代码。

<!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta http-equiv="cache-control" content="no-cache" /> <style> <!-- html, body { height: 100%; margin: 0 auto; padding: 0; } body { position: relative; display: table; } #wrapper { display: table-cell; vertical-align: middle; text-align: middle; } #div { background: blue; color: #fff; } * html #wrapper, *+html #wrapper { position: absolute; top: 50%; } * html #div, *+html #div { position: relative; top: -50%; } --></style> </head> <body> <div id="wrapper"> <div id="div"> i'm Mr. Middle. i'm Mr. Middle. i'm Mr. Middle. </div> </div> </body> </html>

提示:您可以先修改部分代码再运行

【css 实现文字垂直居中】相关文章:

用css实现十字的布局示例代码

css布局中的居中问题

DIV垂直居中的办法

css实现连续的英文或数字自动换行的方法

css+div 图片排列布局

DIV+CSS 全屏垂直居中的一个办法

CSS实现同一行的图片和文字垂直居中对齐的方法

用CSS实现文字的阴影效果

css实现鼠标滑过改变文字(中文变英文)

用css实现隐藏文本框

精品推荐
分类导航