首页 > css布局问题:在IE7下显示正常,chrome,firefox,IE10下不正常?

css布局问题:在IE7下显示正常,chrome,firefox,IE10下不正常?

代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="GBK"/>
<title></title>
<style>
body, ul, li, div {
padding: 0;
margin: 0;
}

ul {
list-style: none;
}

#center {
margin: 20px auto;
width: 600px;
height: 600px;
}

#nav {
float: left;
width: 100px;
height: 600px;
}

#nav li {
width: 100px;
font-size: 24px;
line-height: 100px;
border:1px solid black;
}

#center div {
position: relative;
right:0;
width: 500px;
height: 500px;
text-align:center;
font-size: 30px;
line-height: 500px;
}

#d1 {
top: 0;
background: #ff0000;
}

#d2 {
top: 500px;
display:none;
background: #0000ff;
}

#d3 {
top: 500px;
display:none;
background: #00ffff;
}

#d4 {
top: 500px;
display:none;
background: #00ff00;
}

</style>
</head>
<body>


<div id="center">
<ul id="nav">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<div id="d1">div1</div>
<div id="d2">div2</div>
<div id="d3">div3</div>
<div id="d4">div4</div>
</div>


<script></script>
</body>
</html>

IE7下效果图(正常):

chrome,firefox下面效果图(不正常):

请问造成这种差异的原因是什么?是哪个css属性在作怪?谢谢!!!!!!


http://jsfiddle.net/gUZNT/1

把右侧几个DIV 包起来
添加:float:left


其实是IE 7不正常,不过我的理解是楼主想要达到上图IE 7的效果。
给#center div加上margin-left: 100px;就好了。

然后说一下为什么Chrome下“不正常”。因为按照规范,当元素浮动之后,元素是不应该占位置的,对于后面跟着的非浮动的块状元素来说就好像不存在一样。


center div {

position: relative;
right:0;
width: 500px;
height: 500px;
text-align:center;
font-size: 30px;
line-height: 500px;
left:100px;
}
最后 添加一个left:100px;就可以了


我来分析下:
首先,题主对ul施展了float:left魔法使之脱离了正常文档流漂浮在想要的位置,这使得div元素贴靠在父元素的左边界...不过,这些都不重要
重点是这个

  #center div {
    position: relative;

查阅资料得知,relative 生成相对定位的元素,相对于其正常位置进行定位。注意,是正常位置,这与position生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位是不同的。然后,题主华丽丽的给了一个right: 0;意思就是,你给我乖乖的呆在原地不要动,所以,DIV元素还是紧紧的贴着父元素的左边界,没有向右移动一步(想不通的话,可以给right赋值试试看)。
至于IE7的显示问题,我们知道,IE6/7中的表现,是不能称之为正常的,所以也不能拿来当做标准...只能说,IE7误打误撞的解析方式刚好温暖了题主受伤的心灵....

【热门文章】
【热门文章】