relative和absolute依据什么定位

一、relative依据自身定位

 

二、absolute依据最近一层的定位元素定位

定位元素有:

  • relative、absolute、fixed
  • body (向上查找没有找到定位元素,就会依据body定位)

三、案例

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>absote relative 定位问题</title>
    <style type="text/css">
        body {
            margin: 20px;
        }
        .relative {
            position: relative;
            width: 400px;
            height: 200px;
            border: 1px solid #ccc;

            top: 20px;
            left: 50px;
        }
        .absolute {
            position: absolute;
            width: 200px;
            height: 100px;
            border: 1px solid blue;

            top: 20px;
            left: 50px;
        }
    </style>
</head>
<body>

    <p>absolute 和 relative 定位问题</p>
    <div class="relative">
        <div class="absolute">
            this is absolute
        </div>
    </div>

</body>
</html>

 

posted @ 2023-02-27 13:13  安静的女汉纸  阅读(79)  评论(0编辑  收藏  举报