CSS网页布局DIV水平居中的各种方法 

一、单行垂直居中 

如果一个容器中只有一行文字,对它实现居中相对比较简单,我们只需要设置它的实际高度height和所在行的高度line-height相等即可。如: 

div {   
  height:25px;   
  line-height:25px;   
  overflow:hidden;   
}
这段代码很简单,后面使用overflow:hidden的设置是为了防止内容超出容器或者产生自动换行,这样就达不到垂直居中效果了。 


<!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>   
    <title> 单行文字实现垂直居中 </title>   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <style type="text/css">   
      body { font-size:12px;font-family:tahoma;}   
      div {   
        height:25px;   
        line-height:25px;   
        border:1px solid #FF0099;   
        background-color:#FFCCFF;   
      }   
    </style>   
  </head>   
  <body>   
    <div>现在我们要使这段文字垂直居中显示!</div>   
  </body>   
</html> 
不过在Internet Explorer 6及以下版本中,这和方法不支持对图片设置垂直居中。 

二、多行未知高度文字的垂直居中 

如果一段内容,它的高度是可变的那么我们就可以使用上一节讲到的实现水平居中时使用到的最后一种方法,就是设定Padding,使上下的padding值相同即可。同样的,这也是一种“看起来”的垂直居中方式,它只不过是使文字把<div>完全填充的一种方式而已。可以使用类似下面的代码: 

div {   
  padding:25px;   
} 
这种方法的优点就是它可以在任何浏览器上运行,并且代码很简单,只不过这种方法应用的前提就是容器的高度必须是可伸缩的。 

<!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>   
    <title> 多行文字实现垂直居中 </title>   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <style type="text/css">   
      body { font-size:12px;font-family:tahoma;}   
      div {   
        padding:25px;   
        border:1px solid #FF0099;   
        background-color:#FFCCFF;   
        width:760px;   
      }   
    </style>   
  </head>   
  <body>   
    <div><pre>现在我们要使这段文字垂直居中显示!   
      div {   
        padding:25px;   
        border:1px solid #FF0099;   
        background-color:#FFCCFF;   
      }   
    </pre></div>   
  </body>   
</html> 
三、多行文本固定高度的居中 

在本文的一开始,我们已经说过CSS中的vertical-align属性只会对拥有valign特性的(X)HTML标签起作用,但是在CSS中还有一个display属性能够模拟<table>,所以我们可以使用这个属性来让<div>模拟<table>就可以使用vertical-align了。注意,display:table和display:table-cell的使用方法,前者必须设置在父元素上,后者必须设置在子元素上,因此我们要为需要定位的文本再增加一个<div>元素: 

div#wrap {   
  height:400px;   
  display:table;   
}   
div#content {   
  vertical-align:middle;   
  display:table-cell;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
}
 

<!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>    
    <title> 多行文字实现垂直居中 </title>    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
    <style type="text/css">    
      body { font-size:12px;font-family:tahoma;}    
        div#wrap {    
        height:400px;    
        display:table;    
      }    
      div#content {    
        vertical-align:middle;    
        display:table-cell;    
        border:1px solid #FF0099;    
        background-color:#FFCCFF;    
        width:760px;    
      }    
    </style>    
  </head>    
  <body>    
    <div id="wrap">    
      <div id="content"><pre>现在我们要使这段文字垂直居中显示!    
        div#wrap {    
          height:400px;    
          display:table;    
        }    
        div#content {    
          vertical-align:middle;    
          display:table-cell;    
          border:1px solid #FF0099;    
          background-color:#FFCCFF;    
          width:760px;    
       }    
      </pre></div>    
    </div>    
  </body>    
</html> 
这个方法应该是很理想了,但是不幸的是Internet Explorer 6 并不能正确地理解display:table和display:table-cell,因此这种方法在Internet Explorer 6及以下的版本中是无效的。嗯,这让人很郁闷!不过我们还其它的办法。

四、在Internet Explorer中的解决方案 

在Internet Explorer 6及以下版本中,在高度的计算上存在着缺陷的。在Internet Explorer 6中对父元素进行定位后,如果再对子元素进行百分比计算时,计算的基础似乎是有继承性的(如果定位的数值是绝对数值没有这个问题,但是使用百分比计算的基础将不再是该元素的高度,而从父元素继承来的定位高度)。例如,我们有下面这样一个(X)HTML代码段: 


<div id="wrap">   
  <div id="subwrap">   
    <div id="content">   
    </div>   
  </div>   
</div>
如果我们对subwrap进行了绝对定位,那么content也会继承了这个属性,虽然它不会在页面中马上显示出来,但是如果再对content进行相对定位的时候,你使用的100%分比将不再是content原有的高度。例如,我们设定了subwrap的position为40%,我们如果想使content的上边缘和wrap重合的话就必须设置top:-80%;那么,如果我们设定subwrap的top:50%的话,我们必须使用100%才能使content回到原来的位置上去,但是如果我们把content也设置50%呢?那么它就正好垂直居中了。所以我们可以使用这中方法来实现Internet Explorer 6中的垂直居中: 

div#wrap {   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
  height:400px;   
  position:relative;   
}   
div#subwrap {   
  position:absolute;   
  border:1px solid #000;   
  top:50%;   
}   
div#content {   
  border:1px solid #000;   
  position:relative;   
  top:-50%;   
} 
当然,这段代码只能在Internet Exlporer 6等计算存在问题的浏览器中才会有作用。(不过我不解,我查阅了很多文章,不知道是因为出处相同还是什么原因,似乎很多人都不愿意去解释Internet Exlporer 6中这个Bug的原理,我也只是了解了一点皮毛,还要再研究) 

<!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>   
    <title> 多行文字实现垂直居中 </title>   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <style type="text/css">   
      body { font-size:12px;font-family:tahoma;}   
      div#wrap {   
        border:1px solid #FF0099;   
        background-color:#FFCCFF;   
        width:760px;   
        height:400px;   
        position:relative;   
      }   
      div#subwrap {   
        position:absolute;   
        top:50%;   
      }   
      div#content {   
          position:relative;   
          top:-50%;   
      }   
    </style>   
  </head>   
  <body>   
    <div id="wrap">   
      <div id="subwrap">   
        <div id="content"><pre>现在我们要使这段文字垂直居中显示!   
          div#wrap {   
            border:1px solid #FF0099;   
            background-color:#FFCCFF;   
            width:760px;   
            height:500px;   
            position:relative;   
          }   
          div#subwrap {   
            position:absolute;   
            border:1px solid #000;   
            top:50%;   
          }   
          div#content {   
            border:1px solid #000;   
            position:relative;   
            top:-50%;   
          }  
        </pre></div>   
      </div>   
    </div>   
  </body>   
</html>
五、完美的解决方案 

那么我们综合上面两种方法就可以得到一个完美的解决方案,不过这要用到CSS hack的知识。对于如果使用CSS Hack来区分浏览器,你可以参考这篇“简单CSS hack:区分IE6、IE7、IE8、Firefox、Opera”: 

div#wrap {   
  display:table;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
  height:400px;   
  _position:relative;   
  overflow:hidden;   
}   
div#subwrap {   
  vertical-align:middle;   
  display:table-cell;   
  _position:absolute;   
  _top:50%;   
}   
div#content {   
  _position:relative;   
  _top:-50%;   
}
 至此,一个完美的居中方案就产生了。 

<!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>   
    <title> 多行文字实现垂直居中 </title>   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
    <style type="text/css">   
      body { font-size:12px;font-family:tahoma;}   
      div#wrap {   
        display:table;   
        border:1px solid #FF0099;   
        background-color:#FFCCFF;   
        width:760px;   
        height:400px;   
        _position:relative;   
        overflow:hidden;   
      }   
      div#subwrap {   
        vertical-align:middle;   
        display:table-cell;   
        _position:absolute;   
        _top:50%;   
      }   
      div#content {   
        _position:relative;   
        _top:-50%;   
      }   
    </style>   
  </head>   
  <body>   
    <div id="wrap">   
      <div id="subwrap">   
        <div id="content"><pre>现在我们要使这段文字垂直居中显示!   
          div#wrap {   
            border:1px solid #FF0099;   
            background-color:#FFCCFF;   
            width:760px;   
            height:500px;   
            position:relative;   
          }   
          div#subwrap {   
            position:absolute;   
            border:1px solid #000;   
            top:50%;   
          }   
          div#content {   
            border:1px solid #000;   
            position:relative;   
            top:-50%;   
          }  
        </pre></div>   
      </div>   
    </div>   
  </body>   
</html> 
p.s. 垂直居中vertical-align的值是middle,而水平居中align的值是center,虽然同是居中但关键字不同。

position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
/* 1.内容左上角居中 */
top: 50%; left: 50%;
/* 2.负半宽高把内容挪回来 */
-webkit-transform: translate(-50%, -50%);

.outside {
width: 200px;
height: 200px;
background-color: red;
}

.inside {
width: 100px;
height: 100px;
background-color: rgb(231, 255, 15);
}

<div class="outside">
<div class="inside">
我是被居中的盒子
</div>
</div>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>上下左右居中对齐</title>
        <style type="text/css">
            #box {
                width: 100px;
                height: 100px;
                border: 2px solid #0000FF;
            }
            
            #box {
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translateX(-50%) translateY(-50%);
               
/* 或者这样写 */ right
: 50%; bottom: 50%; transform: translateX(50%) translateY(50%); } </style> </head> <body> <div id="box"></div> </body> </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>div里面图片垂直居中的几个例子</title> 

<style type="text/css"> 

<!-- 

body { 

 margin:0;padding:0 

} 

div { 

 width:500px; 

 height:500px; 

 line-height:500px; 

 border:1px solid #666; 

 overflow:hidden; 

 position:relative; 

 text-align:center; 

} 

div p { 

 position:static; 

 +position:absolute; 

 top:50% 

} 

img { 

 position:static; 

 +position:relative; 

 top:-50%;left:-50%; 

 vertical-align:middle 

} 

p:after { 

 content:".";font-size:1px; 

 visibility:hidden 

} 

--> 

</style> 

</head> 

<body> 

<div><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div> 

</body> 

</html> 



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<title>div里面图片垂直居中的几个例子</title> 

<style type="text/css"> 

<!-- 

* {margin:0;padding:0;} 

div { 

 width:500px;border:1px solid #666; 

 height:500px; 

 background:url("http://www.google.com/intl/en/images/logo.gif") center no-repeat 

} 

--> 

</style> 

</head> 

<body> 

<div></div> 

</body> 

</html> 



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> 


<!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 runat="server"> 

 <title>未命名頁面</title> 


 <script type="text/javascript"> 

 function setCenter(Xelement) 

 { 

 var parent=Xelement.parentNode; 

 parent.style.position="relative"; 

 Xelement.style.position="absolute"; 

 var left=(parent.clientWidth-Xelement.clientWidth)/2; 

 var top=(parent.clientHeight-Xelement.clientHeight)/2; 

 Xelement.style.left=left+"px"; 

 Xelement.style.top=top+"px"; 

 } 

 </script> 


</head> 

<body> 

 <form id="form1" runat="server"> 

 <div id="picbox" > 

 <img id="img1" src="images/c7.jpg" alt="" οnlοad="setCenter(this)" /> 

 </div> 

 </form> 

</body> 

</html>
.container {
position: relative;
}
.content {
position: absolute;
margin: auto;
top: 0; left: 0; bottom: 0; right: 0;
}
/*将最外层元素都撑开到100%屏幕*/
html,body{
width:100%;
height:100%;
}
body{
text-align:center;
}
body:after{
content:"";
display:inline-block;
height:100%;
vertical-align:middle;
}
/*选中的图片元素*/
img{
vertical-align:middle;
}
display: inline-block;
vertical-align: middle;
/*将最外层元素都撑开到100%屏幕*/
html,body{
	width:100%;
	height:100%;
}
body{
	text-align:center;
}
body:after{
	content:"";
	display:inline-block;
	height:100%;
	vertical-align:middle;
}
/*选中的图片元素*/
img{
	vertical-align:middle;
}
<div >
    <img alt="" src="https://www.baidu.com/img/baidu_jgylogo3.gif"  />
</div>
        .three {
        	width: 1000px;
        	height: 60px;
        	border-top: 1px solid #d6d6d6;
        	margin:0 auto; /*居中*/       	
        }
h1 {  
font-size: 3em;
height: 100px;
;
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cindy.picdrag.MainActivity">

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/iv"
        android:src="@drawable/yy"/>
</LinearLayout>
<ul class="imgWrap clearfix">
                <li><a href="#" class="imgBox"><span></span><img src="images/img1.jpg" alt="" /></a></li>
                <li><a href="#" class="imgBox"><span></span><img src="images/img2.jpg" alt="" /></a></li>
                <li><a href="#" class="imgBox"><span></span><img src="images/img3.jpg" alt="" /></a></li>
                <li><a href="#" class="imgBox"><span></span><img src="images/img4.jpg" alt="" /></a></li>
            </ul>
1 <div data-options="region:'north',border:false" >
2 <div id="navicatorbar" >
3 <i></i> <img src="../img/ep-logo.png"
4 />
5 </div>
6 </div>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:text="Hello, World!" />

</RelativeLayout>
/*css*/
div{
    border:1px solid red;
    text-align: center;
    width: 200px;
}
div span{
    width: 100px;
    background: #ddd;
}

<!--HTML-->
<div>
    <span>我是span元素</span>
</div>
img {
width: 100%;
object-fit: cover;
}
  • 1
  • 2
  • 3
  • 4
  • 5