css div水平垂直置中幾個例子

來源:互聯網
上載者:User

一、未知寬度水平置中

 代碼如下 複製代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>未知寬高div在頁面內水平垂直置中</title>
<style>
*{margin:0; padding:0;}
body,html{overflow:hidden; height:100%;}
.box {height: 100%; overflow: hidden; position: relative; width: 100%; display: table;}
.middle {top:50%;text-align:center;display: table-cell; vertical-align: middle; *position:absolute; *left:50%;}
.content{border:#09F solid 1px; margin:0 auto; position:relative; top:-50%;font:12px/2 "Microsoft YaHei"; padding:0 10px; display:table; *left:-50%;}
</style>
</head>

<body>
<div class="box">
        <div class="middle">
        <div class="content">未知寬高div在頁面內水平垂直置中</div>
    </div>
</div>
</body>
</html>

 
二、未知寬高div 水平垂直置中

 代碼如下 複製代碼

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>未知寬高div在頁面內水平垂直置中</title>
<style>
*{margin:0; padding:0;}
body,html{overflow:hidden; height:100%;}
.box {height: 100%; overflow: hidden; position: relative; width: 100%; display: table;}
.middle {top:50%;text-align:center;display: table-cell; vertical-align: middle; *position:absolute; *left:50%;}
.content{border:#09F solid 1px; margin:0 auto; position:relative; top:-50%;font:12px/2 "Microsoft YaHei"; padding:0 10px; display:table; *left:-50%;}
</style>
</head>

<body>
<div class="box">
        <div class="middle">
        <div class="content">未知寬高div在頁面內水平垂直置中</div>
    </div>
</div>
</body>
</html>

三、已知寬高div 水平垂直置中

 代碼如下 複製代碼

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>寬高固定的容器如何在瀏覽器中水平垂直置中</title>
</head>
<style>

html,body { margin:0; padding:0; height:100%; font:12px/180% "宋體"; text-align:center;}
#main {
 width:200px;
 height:20px;
 background-color:#ddd;
 position:relative;
 top:50%;
 margin:-10px auto 0 auto;   
 /*   margin-top=-(height/2);   */
}
</style>
<body>
<div id="main">
  傳說中的水平及垂直置中
</div>
</body>
</html>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.