这样做是不是显得多此一举,直接给子div设置相对定位是不是好一点?

来源:2-4 编程练习

慕圣2189747

2019-10-16 05:32:53

<html>

<head>

  <meta charset="UTF-8">

  <title>Document</title>

  <style>

    /*完善下列代码*/

    .per{

     width: 300px;

     height: 300px;

     background: red;

     margin-left:200px;

     margin-top: 200px;

     position:relative;

   }


   .son{

     width: 100px;

     height: 100px;

     background: blue;

     position:absolute;

     left:100px;

     top:100px;

   }

  </style>

</head>

<body>

  <div class="per">

    <div class="son"></div>

  </div>

</body>

</html>


写回答

1回答

好帮手慕查理

2019-10-16

您好,因为absolute:生成绝对定位的元素,相对于最近定位的第一个父元素进行定位,如果没有会相对浏览器定位。

而relative:生成相对定位的元素,相对于其正常位置进行定位。

所以在设置子元素absolute时,会设置父元素的relative。

祝学习愉快!

0

0 学习 · 4928 问题

查看课程