根据间接父级而不是直接父级的绝对定位

Absolute positioning according to an indirect parent instead of direct parent

我只是在研究一个简单的例子,试图理解绝对定位元素的行为。有一个案例我不明白。

我有一个容器,里面有一张卡片。我想在这张卡片中添加一个页脚,所以我在里面放了另一个 div ,如下所示:

<body>
   <section id="experiences">
      <div class="experiences-cards-container">
         <div class="experience-card">
            <div class="card-footer"></div>
         </div>
      </div>
   </section>
</body>

因为它是页脚,所以我希望它显示在我名片的底部。这就是我使用绝对定位的原因:

.experience-card .card-footer {
  position: absolute;
  left: 0px;
  bottom: 0px;
  height: 70px;
  width: 100%;
  background-color: blue;
}

但似乎页脚元素没有根据其直接父元素 experience-card 定位,而是根据间接父元素 experiences-card-container 定位,因为我得到以下结果:

我的问题是:为什么页脚元素根据间接父元素而不是卡片定位,因为它是直接父元素?

这里是完整的CSS:

html, body 
{
    height: 100%;
}

#experiences {
    height: 100%;
    background-color: #ECECEC;
}

.experiences-cards-container {
    position: relative;
    width: 100%;
    max-width: 1200px;
    height: calc(100% - 100px);
    text-align: center;
    margin: auto;
}

.experience-card {
    position: "relative";
    display: inline-block;
    width: 280px;
    height: 350px;
    background-color: white;
    margin-right: 20px;
    box-shadow: 0px 0px 6px 0px #949494;
    margin-bottom: 20px;
    text-align: left;
}

.experience-card .card-body {
    padding-left: 10px;
    padding-right: 10px;
}

.experience-card .card-footer {
    position: absolute;
    left: 0px;
    bottom: 0px;
    height: 70px;
    width: 100%;
    background-color: blue;
}

去掉.experience-card class中position: "relative";中的引号,应该可以了。通过使其相对,它将成为页脚中绝对定位的上下文。

你可以试试这个:

.experiences-cards-container {
   position: relative;
   width: 100%;
   max-width: 1200px;
   /* height: calc(100% - 100px); */
   text-align: center;
   margin: auto;
}

您可以使用此代码

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <title>Hello, world!</title>
  <style type="text/css">
    html,
    body {
      height: 100%;
    }
    
    #experiences {
      height: 100%;
      background-color: #ECECEC;
    }
    
    .experiences-cards-container {
      position: relative;
      width: 100%;
      max-width: 1200px;
      height: calc(100% - 100px);
      text-align: center;
      margin: auto;
    }
    
    .experience-card {
      display: inline-block;
      width: 280px;
      height: 350px;
      background-color: white;
      margin-right: 20px;
      box-shadow: 0px 0px 6px 0px #949494;
      margin-bottom: 20px;
      text-align: left;
      position: relative;
    }
    
    .experience-card .card-body {
      padding-left: 10px;
      padding-right: 10px;
    }
    
    .experience-card .card-footer {
      position: absolute;
      left: 0px;
      bottom: 0px;
      height: 70px;
      width: 100%;
      background-color: blue;
      right: 0;
    }
  </style>
</head>

<body>
  <section id="experiences">
    <div class="experiences-cards-container">
      <div class="experience-card">
        <div class="card-footer"></div>
      </div>
    </div>
  </section>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>