html - 固定 View 如何让 child 100%适应 parent 高度?

标签 html css

我有一个固定高度为 20 厘米的 div。现在,内页需要填充,而绝对位置需要遵守该填充。

如何让红色框始终填充剩余的内容?无论如何,位置绝对元素都需要始终位于底部。

它需要有一个类,而不是具有 10 个不同高度(例如 58%、45% 等)的 10 个类...

如果你检查代码笔:https://codepen.io/Aurelian/pen/MqxvgW

这是 HTML:

<div class="page">

   <div class="image"></div>

   <div class="page-inner-default">
      <p>hello</p>
      <span class="pos-bot">Hi</span>
   </div>

</div>

<div class="page">

   <div class="image"></div>

   <h1>Heading</h1>

   <div class="page-inner-default">
      <p>hello</p>
     <span class="pos-bot">Hi</span>
   </div>

</div>

这是 CSS:

*, *:before, *:after {
  box-sizing: border-box;
}

body {
   background: grey;
   box-sizing: border-box;
}

.image {
   height: 250px;
   border: 1px solid green;
   background: green;;
}

.pos-bot {
   position: absolute;
   bottom: 0;
}

.page {
   height: 20cm;
   background-color: white;
   width: 16cm;
   margin: 50px auto;
   display: inline-block;
   vertical-align: top;
   position: relative;
}

.page-inner-default {
   position: relative;
   padding: 50px;
   height: 100%;
      border: 1px solid red;
}

.image {

}

最佳答案

给出 .page{overflow:hidden} 并从 .page-inner-default 中删除相对位置,这样 .pos-bot 将定位到最近的相对位置,在这种情况下它是 .page div

*, *:before, *:after {
  box-sizing: border-box;
}

body {
   background: grey;
   box-sizing: border-box;
}

.image {
   height: 250px;
   border: 1px solid green;
   background: green;;
}

.pos-bot {
   position: absolute;
   bottom: 0;
}

.page {
  overflow: hidden;
   height: 20cm;
   background-color: white;
   width: 16cm;
   margin: 50px auto;
   display: inline-block;
   vertical-align: top;
   position: relative;
}

.page-inner-default {
   padding: 50px;
   min-height: 100%;
      border: 1px solid red;
}
    <div class="page">

     <div class="image"></div>

     <div class="page-inner-default">
        <p>hello</p>
        <span class="pos-bot">Hi</span>
     </div>

  </div>

  <div class="page">

     <div class="image"></div>

     <h1>Heading</h1>

     <div class="page-inner-default">
        <p>hello</p>
       <span class="pos-bot">Hi</span>
     </div>

  </div>

关于html - 固定 View 如何让 child 100%适应 parent 高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52443528/

相关文章:

javascript - 如何编辑引导模式继承的 CSS

css - 如何给出固定位置的摆动动画效果?

javascript - 自动调整子 div 的大小,使它们平均共享父 div 的宽度

css - 定心浮子

php - 使用 jquery 传递的 GET 变量

python - 如何保护我的 Python 程序

javascript - 如何在fabricjs中使用嵌入式图像base64将 Canvas 转换为SVG

html - 响应式字体大小/粗细

html - 试图创建一个只有 css 和 html 的下拉菜单

html - 2 "style"内联 css img 标签?