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. 我的android学习经历30

    数据存储的持久化技术 数据存储主要有五种方式: (1)文件存储或者SD卡 (2)SharedPreferences (3)SQLite (4)ContentProvider (5)网络存储 下面简单介 ...

  2. Cheatsheet: 2014 10.01 ~ 10.30

    .NET ASP.NET Web Api: Unwrapping HTTP Error Results and Model State Dictionaries Client-Side HTTP 20 ...

  3. PHP中的替代语法(冒号、endif、endwhile、endfor)

    我们经常在wordpress一类博客程序的模板里面看到很多奇怪的PHP语法,比如: <?php if(empty($GET_['a'])): ?> <font color=" ...

  4. BP神经网络求解异或问题(Python实现)

    反向传播算法(Back Propagation)分二步进行,即正向传播和反向传播.这两个过程简述如下: 1.正向传播 输入的样本从输入层经过隐单元一层一层进行处理,传向输出层:在逐层处理的过程中.在输 ...

  5. 微博传播数量和传播深度的预测--基于pyspark和某个回归算法

    8-28决定参加一下这个千万条的数据处理任务,因为场景和自己做过的一个回归分析预测差不多,第一天开始在小规模的数据上做准备工作. 第二次大修改版本 date 20160829 星期一¶ 原始数据处理, ...

  6. TCP三次握手及四次挥手详细 转

    一.TCP报文格式        TCP/IP协议的详细信息参看<TCP/IP协议详解>三卷本.下面是TCP报文格式图: 图1 TCP报文格式 上图中有几个字段需要重点介绍下:      ...

  7. 64位Eclipse运行时提示“Failed to load the JNI shared library \Java\jre6\bin\client\jvm.dll”的一个解决方案

    系统安装的32位JREE,64位eclipse无法识别,解决方案:下载安装64位jdk即可.

  8. iOS - Threads 多线程

    1.Threads 1.1 进程 进程是指在系统中正在运行的一个应用程序.每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内. 比如同时打开 QQ.Xcode,系统就会分别启动两个进程. ...

  9. iOS - UIViewController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIViewController : UIResponder <NSCoding, UIAppearanceC ...

  10. SAP接口编程 之 JCo3.0系列(05) : Exception Handling

    JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...