相对定位和浮动一起使用,那元素还会占据原来的位置码?难道不是使用了float会脱离标准流吗?
来源:2-7 编程练习
伪装的很酷
2019-03-23 18:20:06
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{
padding: 0;
margin: 0;
}
.blue{
width: 100px;
height: 100px;
background: blue;
position: relative;
left: 50px;
z-index:2;
}
.red{
width: 100px;
height: 100px;
background: red;
position: relative;
top: -50px;
}
</style>
</head>
<body>
<div class="blue"></div>
<div class="red"></div>
</body>
</html>
1回答
同学,这个题中不需要设置浮动,考察的是相对定位,只需要设置这些就可以了,red不需要设置:

另外,关于你问到的是否会脱离文档流,是否会占据原来的位置,这个需要在实践中总结,有时候情况复杂要具体问题具体分析,但是position:relative是会占据原来位置的,还是要看实际的效果,你写的时候就能看出来,这些东西不用死记硬背的哦。
如果帮助到了你,欢迎采纳!
祝学习愉快!
相似问题