div中内容垂直居中的方法小结

一、行高(line-height)法

如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如:

p { height:30px; line-height:30px; 100px; overflow:hidden; }

这段代码可以达到让文字在段落中垂直居中的效果。

二、内边距(padding)法

另一种方法和行高法很相似,它同样适合一行或几行文字垂直居中,原理就是利用padding将内容垂直居中,比如: 

p { padding:20px 0; }

这段代码的效果和line-height法差不多。

三、模拟表格法

将容器设置为display:table,然后将子元素也就是要垂直居中显示的元素设置为display:table-cell,然后加上vertical-align:middle来实现。

html结构如下: 

<div id="wrapper">
    <div id="cell">
        <p>测试垂直居中效果测试垂直居中效果</p>
        <p>测试垂直居中效果测试垂直居中效果</p>
    </div>
</div>

css代码: 

#wrapper {display:table;300px;height:300px;background:#000;margin:0 auto;color:red;}
#cell{display:table-cell; vertical-align:middle;}

实现如图所示:

遗憾的是IE7及以下不支持。

四、CSS3的transform来实现

css代码如下:

 
.center-vertical{
  position: relative;
  top:50%;
  transform:translateY(-50%);
}.center-horizontal{
  position: relative;
  left:50%;
  transform:translateX(-50%); 
}

五:css3的box方法实现水平垂直居中

html代码: 

<div class="center">
  <div class="text">
    <p>我是多行文字</p>
    <p>我是多行文字</p>
    <p>我是多行文字</p>
  </div>
</div>

css代码: 

.center {
   300px;
  height: 200px;
  padding: 10px;
  border: 1px solid #ccc;
  background:#000;
  color:#fff;
  margin: 20px auto;


  display: -webkit-box;
  -webkit-box-orient: horizontal;
  -webkit-box-pack: center;
  -webkit-box-align: center;
  
  display: -moz-box;
  -moz-box-orient: horizontal;
  -moz-box-pack: center;
  -moz-box-align: center;
  
  display: -o-box;
  -o-box-orient: horizontal;
  -o-box-pack: center;
  -o-box-align: center;
  
  display: -ms-box;
  -ms-box-orient: horizontal;
  -ms-box-pack: center;
  -ms-box-align: center;
  
  display: box;
  box-orient: horizontal;
  box-pack: center;
  box-align: center;
}

结果如图:

图片垂直居中的方法:

1、方法一:将外部容器的显示模式设置成display:table,img标签外部再嵌套一个span标签,并设置span的显示模式为display:table-cell,这样span内部的内容就相当于表格,可以很方便的使用vertical-align属性来对齐其中的内容了。

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5     <title>未知高度的图片垂直居中</title>
 6     <style type="text/css">
 7         body {
 8             height: 100%;
 9         }
10 
11         #box {
12             width: 500px;
13             height: 400px;
14             display: table;
15             text-align: center;
16             border: 1px solid #d3d3d3;
17             background: #fff;
18         }
19 
20             #box span {
21                 display: table-cell;
22                 vertical-align: middle;
23             }
24 
25             #box img {
26                 border: 1px solid #ccc;
27             }
28     </style>
29 </head>
30 <body>
31     <div id="box">
32         <span><img src="../Content/Images/home-one.png" alt="家居" /></span>utf-8
33     </div>
34 </body>
35 </html>

2、方法二:标准浏览器的情况还是和上面一样,不同的是针对IE6/IE7利用在img标签的前面插入一对空标签的办法。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>方法2 - 未知高度的图片垂直居中</title>

    <style type="text/css">
        body {
            height: 100%;
        }

        #box {
             500px;
            height: 400px;
            display: table-cell;
            text-align: center;
            vertical-align: middle;
            border: 1px solid #d3d3d3;
            background: #fff;
        }

            #box img {
                border: 1px solid #ccc;
            }
    </style>
</head>
<body>
    <div id="box">
        <i></i><img src="../Content/Images/home-one.png" alt="家居" />
    </div>
</body>
</html>

3、方法三:在img标签外包裹一个p标签,标准浏览器利用p标签的伪类属性:before来实现居中,另外,对于IE6/IE7使用了CSS表达式来实现兼容。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>方法3 - 未知高度的图片垂直居中 - www.cleanthem.com</title>

    <style type="text/css">
        body {
            height: 100%;
        }

        #box {
             500px;
            height: 400px;
            text-align: center;
            border: 1px solid #d3d3d3;
            background: #fff;
        }

            #box p {
                 500px;
                height: 400px;
                line-height: 400px; /* 行高等于高度 */
            }

                /* 兼容标准浏览器 */
                #box p:before {
                    content: "."; /* 具体的值与垂直居中无关,尽可能的节省字符 */
                    margin-left: -5px;
                    font-size: 10px; /* 修复居中的小BUG */
                    visibility: hidden; /*设置成隐藏元素*/
                }

                #box p img {
                    *margin-top: expression((400 - this.height )/2); /* CSS表达式用来兼容IE6/IE7 */
                    vertical-align: middle;
                    border: 1px solid #ccc;
                }
    </style>
</head>
<body>
    <div id="box">
        <p><img src="../Content/Images/home-one.png" alt="家居" /></p>
    </div>
</body>
</html>

注:以上div内img标签的居中参考自:http://www.cnblogs.com/cnliu/archive/2012/06/20/image-center.html

love life,love work!---身为屌丝,向往的仍然是逆袭的故事。
原文地址:https://www.cnblogs.com/liushenghuan/p/5736751.html