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

标签 html css css-position absolute

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

我有一个容器,里面有一张卡片。我想在这张卡片中添加一个页脚,所以我在里面放了另一个 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 定位,因为我得到结果如下:

enter image description here

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

这是完整的 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 类中 position: "relative"; 中的引号,它应该可以工作。通过使其相对,它将成为页脚中绝对定位的上下文。

关于html - 根据间接父级而不是直接父级的绝对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58402669/

相关文章:

css - div类中的背景图像不会拉伸(stretch)到浏览器的全宽

css - 修复了响应式 Bootstrap 侧边栏导航中的垂直 Logo

html - 在一个元素上使用超过 1 个 CSS 类的正确方法是什么?

c# - 选择没有特定类的 jQuery First Child

html - GMail 强制包含 spacer.gif 的 TD 为 16px 高

html - CSS 使整个相对主体水平居中,无需包装器或滚动条?

javascript - 当用户通过jquery在文本框中输入时,如何自动添加字符串 "http://'

javascript - CSS\JS - Long li 文本默认展开(应该 overflow hidden )

html - 水平调整大小时样式困惑

带有边框半径和粘性标题的 HTML 表格