未知高度的居中

来源:互联网 发布:office软件如何自学 编辑:程序博客网 时间:2024/06/10 02:08
<script type="text/javascript"><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<html>
<head>
<style type="text/css">
.big
{
position:relative;width:300px;height:300px;background:#ffff00;
}
.small
{
position:absolute;top:50%;left:50%;background:#ff6600;
}
</style>
</head>

<body>
<div class="big"><div class="small">测试<br>测试<br>测试<br>测试<br>测试<br>测试<br>
</div></div>
<p>其他内容</p>
</body>

</html>

此处关键是绝对定位的问题,要注意的是定位居中是以内容块的左上角确定居中的,absolute很容易被误解--以前一直以为是以document原点为坐标原点,其实这在大部分时候也是正确的;但是如果当前设置了absolute的元素的parentelement也设置了absolute或者relative属性时,该元素的坐标就会参照parentElement的坐标拉
原创粉丝点击