这个top left 和 margin-left margin-top 用起来感觉有点混淆
来源:2-7 编程练习
悉尼audrey
2019-11-05 21:00: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;
top:50px;
left:50px;
z-index:5;
}
.red{
width: 100px;
height: 100px;
background: red;
position:relative;
z-index:1;
}
</style>
</head>
<body>
<div class="blue"></div>
<div class="red"></div>
</body>
</html>
1回答
同学你好,1、top与left要结合定位使用。而外边距margin-top这些不需要哦。
2、margin-top是外边距,调整当前元素的外边距,以这个代码例子来说,就是调整蓝色块的上外边距,整体都会下移。
而top是偏移值,就顶部多少距离。只会调整本身的位置,不会影响挨着的元素哦。可以看下效果,对比下区别。
3、代码实现没有问题。另,red这个元素,不需要移动,可以不设置定位哦。
希望能帮助到你,祝学习愉快!
相似问题