【问题标题】:Absolute positioning according to an indirect parent instead of direct parent根据间接父代而不是直接父代进行绝对定位
【发布时间】:2019-10-16 18:28:39
【问题描述】:

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

我有一个容器,里面有一张卡片。我想在这张卡片里面添加一个页脚,所以我在里面放了另一个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;
}

【问题讨论】:

    标签: html css css-position absolute


    【解决方案1】:

    在 .experience-card 类中取出position: "relative"; 中的引号,它应该可以工作。通过使其相对,它将成为页脚中绝对定位的上下文。

    【讨论】:

    • 从 jss 到 css.. 谢谢! 10 分钟限制结束后,我会尽快接受您的答复。谢谢。
    • 没问题。我添加了一些关于定位上下文的解释,但我想你已经知道了:)
    【解决方案2】:

    您可以使用此代码

    <!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>

    【讨论】:

      【解决方案3】:

      你可以试试这个:

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

      【讨论】:

      • 为答案提供一些上下文,解释每条规则的作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-21
      • 2018-05-18
      • 1970-01-01
      相关资源
      最近更新 更多