div中的文本水平居中,一般都是用text-align:center;就可以解决,那么垂直居中呢,知道vertiacl-align:middle;但有时候却不起作用;整理下div中文本垂直居中对齐的问题(只是自己总结)
1.单行文本垂直居中对齐
① height=line-height即可;

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style type="text/css">
*{margin: 0;padding: 0;}
.text{
width: 200px;
height: 100px;
line-height:100px;
border:2px solid #eee;
margin: 20px auto;
text-align: center;
}
</style>
<body>
<div class="text">无意苦争春</div>
</body>
</html>

②通过padding值来调节,此时padding-top=padding-bottom,且padding-top+padding-bottom+div的height=真正想要的高度;

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style type="text/css">
*{margin: 0;padding: 0;}
.text{
width: 200px;
height: 100px;
padding: 40px 0;
border:2px solid #eee;
margin: 20px auto;
text-align: center;
}
</style>
<body>
<div class="text">无意苦争春</div>
</body>
</html>

2.多行文字
第一种和单行方法②一样;
第二种是将外部该div放到一空div里,该div display:table-cell;vertical:middle;

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style type="text/css">
*{margin: 0;padding: 0;}
.texts{width: 602px;height: 100px;margin: 20px auto;border:1px solid #eee;}
.text{
width: 200px;
height: 100px;
display: table-cell;
vertical-align: middle;
text-align: center;
border-left: 1px solid #eee;
}
.text:first-child{border-left: none;} </style>
<body>
<div class="texts">
<div class="text">无意苦争春</div>
<div class="text">一任群芳妒</div>
<div class="text">无意苦争春<br />一任群芳妒</div>
</div>
</body>
</html>

对于多行文本可以垂直居中的方法,
单行文本也可以垂直居中。
不过最后一种方法不兼容IE6/7。。。
目前只知道这些,先记录下来。

div中字垂直居中对齐的更多相关文章

  1. div+css:div中图片垂直居中

    div中图片垂直居中 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> &l ...

  2. 文本在div中始终垂直居中

    如果是文本字数固定,在div中垂直居中,相信大家都会 这边分享个不固定文本在div中垂直居中的方法 html代码 <div class="box"> <div c ...

  3. p标签在div中水平垂直居中且文本左对齐

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. div图片垂直居中 如何使div中图片垂直居中

    (从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期 2014-04-03) 『此方法在ie7下,如果.box的高度为800等比较大的数值时,并不能起到垂直居中的作用.』 点评:关于图片垂 ...

  5. DIV中的垂直居中

    <div style="border:0px #ff0000 solid; width:100px;height:380px; line-height:380px; float:lef ...

  6. img标签在div中水平垂直居中--两种实现方式

    第一种方式: text-align:center; vertical-align:middle; div{ text-align: center; vertical-align:middle;widt ...

  7. 如何让一个div水平和垂直居中对齐

    以下方法来自百度知道:https://zhidao.baidu.com/question/558984366971173044.html 方法1: .parent { width: 800px; he ...

  8. 【div+css】两个div,如何让内层的div在外层div中水平垂直居中

    好久没有写样式,很是很生疏 ==================================================================== 方法1: .parent { wi ...

  9. div中的img垂直居中的方法,最简单! 偷学来的,,,不要说我抄袭啊(*^__^*)

    让div中的img垂直居中,水平居中很简单,用text-align:center; 让div中img垂直居中的方法其实也很简单 重点是: display:table-cell;   让标签具有表格的属 ...

随机推荐

  1. Zabbix监控交换机设置

    说明: Zabbix监控服务端已经配置完成,现在要使用Zabbix对交换机进行监控. 具体操作: 以下操作在被监控的交换机上进行,这里以Cisco交换机为例. 一.登录到Cisco交换机,开启snmp ...

  2. Outlook 无法更新全球通讯簿,错误 0×80190194

    当 Outlook 客户端尝试更新全球通讯簿,实际上是下载脱机通讯簿(Officeline Address Book,简称 OAB)时,可能会收到 0×80190194 的错误.错误代码 0×8019 ...

  3. 我的android学习经历15

    利用Intent实现有返回结果的页面跳转 主要用的方法: (1)Intent的构造方法:intent(当前界面对象,要跳转的界面.class); (2)接受结果的方法onActivityResult( ...

  4. sql server 查询分析器消息栏里去掉“(5 行受影响)”

    sql server 查询分析器消息栏里去掉"(5 行受影响)"     在你代码的开始部分加上这个命令: set nocount on   记住在代码结尾的地方再加上: set ...

  5. TCP服务器不回复SYN的问题

    个人问题发生环境: 1.TCP服务器是虚拟机,IP地址是192.168.8.12. 2.TCP客户端是宿主机,IP地址是192.168.8.11. 3.从宿主机(192.168.8.11)上启动Soc ...

  6. GMM及EM算法

    GMM及EM算法 标签(空格分隔): 机器学习 前言: EM(Exception Maximizition) -- 期望最大化算法,用于含有隐变量的概率模型参数的极大似然估计: GMM(Gaussia ...

  7. Reading Csv Files with Text_io in Oracle D2k Forms

    Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...

  8. mysqldump使用方法(MySQL数据库的备份与恢复)

    #mysqldump --help 1.mysqldump的几种常用方法: (1)导出整个数据库(包括数据库中的数据) mysqldump -u username -p dbname > dbn ...

  9. django-crontab定时任务

    django-crontab实现定时任务 1 django-crontab安装 django-crontab安装: django-crontab加入:只需要将INSTALLED_APPS即可.如下代码 ...

  10. zoj 1010 (线段相交判断+多边形求面积)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Mem ...