CSS未知div高度垂直居中代碼_層和佈局特效

<!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" mrc="text/html; charset=utf-8" />
<title>CSS未知高度垂直居中</title>
<style type="text/css">
html,
body
{
height: 100%;
padding:0;
margin:0;
}css

body
{
min-height: 200px;
text-align: center;
min-width: 402px
}html

.FirstDIV
{
margin-top: -100px; /* SecondDIV高度的一半 */
float: left;
width: 100%;
height: 50%;
}編程

.SecondDIV
{
clear: both;
border: silver 1px solid;
background: #CCCCCC;
margin-left: auto;
overflow: auto;
width: 400px;
margin-right: auto;
height: 200px;
text-align: left
}
</style>
</head>
<body>
<div class="FirstDIV"></div>
<div class="SecondDIV">
<p>暈噢,難道沒有居中嗎?</p>
<p>我是模板無憂(<a href="/jscss/">mb5u.com</a>)提供各種編程源碼、視頻教程、JavaScript/CSS特效代碼以及經常使用軟件下載等。<br>看我是否是垂直居中了?</p>
</div>
</body>
</html>框架

 

以上方式測試了下 ,發覺兼容性還不錯測試

還有一種實現方式,是經過onresize 。onresize事件會在窗口或框架被調整大小時發生。ui

在事件裏面計算出要居中的內容的top,left值,設置css.spa

相關文章
相關標籤/搜索