magin-top和top的区别
来源:2-11 编程练习
onedream4
2020-08-21 17:40:04
子级div使用绝对定位后,magin-top和top的去区别是什么
1回答
同学你好,如果是子级设置绝对定位,父级设置相对定位的话,margin-top跟top的功能都是差不多的,都是距离父元素顶部的距离。例如:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } .test { position: relative; width: 200px; height: 200px; background-color: red; } .one { position: absolute; width: 100px; height: 100px; background-color: blue; /* 设置margin-top和top的效果是一样的 */ /* margin-top: 50px; */ top: 50px; } </style> </head> <body> <div class="test"> <div class="one"></div> </div> </body> </html>
如果我的回答帮助了你,欢迎采纳。祝学习愉快~
相似问题