CSS未知div高度垂直居中代码_层和布局特效
2014-03-20 15:12
609 查看
<!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;
}
body
{
min-height: 200px;
text-align: center;
min-width: 402px
}
.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事件会在窗口或框架被调整大小时发生。
在事件里面计算出要居中的内容的top,left值,设置css.
<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;
}
body
{
min-height: 200px;
text-align: center;
min-width: 402px
}
.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事件会在窗口或框架被调整大小时发生。
在事件里面计算出要居中的内容的top,left值,设置css.
相关文章推荐
- CSS解决未知高度的DIV垂直居中
- CSS解决未知高度的DIV垂直居中
- CSS实现已知宽高的div垂直居中;自适应高度两列布局
- CSS 解决未知高度垂直居中实现代码
- 如何实现未知高居垂直居中?_div+css布局教程
- DIV未知高度的垂直居中代码
- 【转】CSS解决未知高度的DIV垂直居中
- CSS让未知高度div的内容垂直居中(table-cell 和 负margin)
- CSS解决未知高度的DIV垂直居中
- DIV未知宽度高度垂直水平居中
- div中文字水平和垂直居中的css代码
- div图片水平垂直居中不随滚动条滚动的CSS代码(兼容IE8+)
- css实现固定高度及未知高度文字垂直居中的完美解决方案
- 用CSS让未知高度内容垂直方向居中
- 未知高度div垂直居中
- CSS实现未知高度图文混合垂直居中
- CSS解决未知高度垂直居中的问题
- CSS在页面布局中实现div垂直居中的方法总结
- 实现div垂直居中的CSS代码