将相对div拉伸到其静态父对象的高度

时间:2019-04-19 18:48:34

标签: html css css3 flexbox

我在水平flex框(黑色轮廓)中有一个div(红色框)。它的高度是根据flex框中其他物体的高度(在本例中为蓝色框)的高度计算得出的;一切都很好。

Green box not tall enough

红色div有一个孩子-图片中的绿色框。相对于红色框的位置。我希望绿色框通常能完全覆盖红色框。宽度没有问题,因为红色框的宽度是固定的。但是,我怎么说绿色框的高度应该等于其父框红色框的高度?

将绿色框置于红色框上方的原因是,我希望将绿色框悬停在上方时水平展开,但我不希望此扩展影响其他元素的布局。像这样:

green box expanded horizontally

有一个JSFiddle here

div.dropZone {
  position: relative;
  font-family: serif;
  margin: 0px;
  border-radius: 5px;
  left: 0px;
  top: 0px;
  width: 2em;
  height: inherit;
  min-height: 3ex;
  background-color: green;
}

div.dropZone:hover {
  width: 4em;
  left: -1em;
}

div.dzContainer {
  position: static;
  font-family: serif;
  margin: 0px 0px 0px 0px;
  -webkit-align-self: stretch;
  align-self: stretch;
  width: 2em;
  height: auto;
  min-height: 3ex;
  background-color: red;
}

div.tall {
  position: static;
  font-family: serif;
  margin: 0px 0px 0px 0px;
  -webkit-align-self: stretch;
  align-self: stretch;
  background-color: blue;
  width: 3em;
  height: 10ex;
}

.H {
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-flex-direction: row;
  flex-direction: row;
  -webkit-flex-wrap: nowrap;
  flex-wrap: nowrap;
  -webkit-justify-content: flex-start;
  justify-content: flex-start;
  -webkit-align-items: flex-start;
  align-items: flex-start;
  border: 1px solid black;
}
<div class="H">
  <!-- black flex-->
  <div class="tall"> </div>
  <!-- blue static-->
  <div class="dzContainer">
    <!-- red static-->
    <div class="dropZone"> </div>
    <!-- green relative-->
  </div>
  <div class="tall"> </div>
  <!-- blue static-->
</div>

2 个答案:

答案 0 :(得分:4)

如果您希望绿色框填充父级高度,请添加高度:100%;到.dropZone类。

div.dropZone {
  position: relative;
  font-family: serif;
  margin: 0px;
  border-radius: 5px;
  left: 0px;
  top: 0px;
  width: 2em;
  height: 100%;
  min-height: 3ex;
  background-color: green;
}

答案 1 :(得分:2)

您只需要在CSS中进行一项更改即可。在您的div.dropZone选择器中添加“ height:100%”。希望对您有所帮助。

您可以运行下面的代码片段进行试用。

div.dropZone {
    position: relative;
    font-family: serif;
    margin: 0px;
    border-radius: 5px;
    left: 0px;
    top: 0px;
    width: 2em;
    height: inherit;
    min-height: 3ex;
    background-color: green;
    height: 100%;
}

div.dropZone:hover {
  width: 4em;
  left: -1em;
}

div.dzContainer {
    position: static;
    font-family: serif;
    margin: 0px 0px 0px 0px;
    -webkit-align-self: stretch;
    align-self: stretch;
    width: 2em;
    height: auto;
    min-height: 3ex;
    background-color: red;
}

div.tall {
    position: static;
    font-family: serif;
    margin: 0px 0px 0px 0px;
    -webkit-align-self: stretch;
    align-self: stretch;
    background-color: blue;
    width: 3em;
    height: 10ex;
}

.H {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: row;
    flex-direction: row;
    -webkit-flex-wrap: nowrap;
    flex-wrap: nowrap;
    -webkit-justify-content: flex-start;
    justify-content: flex-start;
    -webkit-align-items: flex-start;
    align-items: flex-start;
    border: 1px solid black;
}
   <div class="H">                      <!-- black flex-->
    <div class="tall"> </div>          <!-- blue static-->
    <div class="dzContainer">           <!-- red static-->
        <div class="dropZone"> </div>   <!-- green relative-->
    </div>
    <div class="tall"> </div>          <!-- blue static-->
   </div>

相关问题