简体   繁体   English

如何根据孩子的绝对身高增加父母身高 div

[英]How to grow parent height div based on absolute child's height

I'm trying to achieve the following layout in a Gatbsy(React) application.我正在尝试在 Gatbsy(React) 应用程序中实现以下布局。 I have looking like that.我看起来像那样。 The problem is that the white square depending on the content the height will change.问题是白色方块根据内容的高度会发生变化。 If this height changes the height of the blue needs to change as well.如果这个高度改变,蓝色的高度也需要改变。 The page needs to to always look essentially like the picture attach, meaning no matter the height of the white square, it always need to be a little above the blue square.页面需要始终看起来像附加图片,这意味着无论白色方块的高度如何,它始终需要略高于蓝色方块。

I'm using Sass(SCSS).我正在使用 Sass(SCSS)。

CodePen: https://codepen.io/devpato/pen/ZEYwexw CodePen: https ://codepen.io/devpato/pen/ZEYwexw

在此处输入图片说明

CSS CSS

 .header, .footer {
      width: 100%;
      height: 200px;
      background-color: green;
    }

    .blue {
      position: relative;
      /* Height can't have a set value */
      height: 400px;
      background-color: blue;
      width: 100%;

      .white {
      height: 400px;
      position: absolute;
      background-color: white;
      left: 0;
      right: 0;
      margin: 0 auto;
      top: -24px;
      width:  80%;
     }
    }

HTML HTML

<div class="container">
  <div class="header"></div>
  <div class="blue">
       <div class="white"></div>
  </div>
  <div class="footer"></div>
</div>

Remove the height from parent and add position relative in child instead of absolute从父级中删除高度并在子级中添加相对位置而不是绝对位置

.blue {
  position: relative;
  background-color: blue;
  width: 100%;
  .white {
  height: 400px;
  position: relative;
  background-color: white;
  left: 0;
  right: 0;
  margin: 0 auto;
  top: -24px;
  width:  80%;
 }
}

Then just remove the parent height and keep the child height and set child as a relative like following:然后只需删除父高度并保留子高度并将子项设置为相对如下:

.blue {
  position: relative;
  /* Hight can't have a set value */
  /*height: 400px;*/
  background-color: blue;
  width: 100%;
  .white {
    height: 400px;
    position: relative;
    background-color: white;
    left: 0;
    right: 0;
    margin: 0 auto;
    top: -24px;
    width:  80%;
 }
}

Also see here: codepenexample另请参见此处: codepenexample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM