3 div在父容器中,它们应该动态地改变高度,但是它们的高度加起来应该总是相同的总高度

8dtrkrch  于 2021-09-29  发布在  Java
关注(0)|答案(3)|浏览(397)

此问题已在此处找到答案

将剩余高度或宽度填入柔性容器(2个答案)
css-使所有元素与最高元素的高度相同(3个答案)
五小时前关门了。
我试着在一个父div中放置3个div,父div有一个固定的高度(比如说300px)。中间的div有一个可变的高度,我希望其他两个div动态调整它们的高度,以便总高度始终为300px。有解决办法吗?

yr9zkbsy

yr9zkbsy1#

实现这一点的一个简单方法是使用 flex . 你有一个单一的维度布局,所以它是完美的适合。

  • 编辑-看看其他答案,我可能误解了你,写的是垂直轴,而不是水平轴。没有特别提到。
.parent {
  /* We use vertical flex direction so we can utilize the flex property */
  display: flex;
  flex-direction: column;

  height: 300px;
}

.children {
  /* All should grow or shrink and take as much as possible.*/
  flex: 1 1 auto; 

  background: yellow;
}

.auto {
  /* No need to grow or shink specifically.*/
  flex-grow: 0;

  background: red;
  color: white;
}
<h2>One line text:</h2>
<div class="parent">
  <div class="children"></div>
  <div class="children auto"><p>I am a paragraph.</p></div>
  <div class="children"></div>
</div>

<h2>Long text:</h2>
<div class="parent">
  <div class="children"></div>
  <div class="children auto"><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p></div>
  <div class="children"></div>
</div>
2eafrhcq

2eafrhcq2#

.parentDiv{
      background-color: #999;
      display:flex;
      height:300px;
      justify-content: space-between;
      align-items: stretch;
    }
    .childDiv{
      background-color: #fff;
      border: 1px solid red;
      flex: 1 1 0;
    }
<div class="parentDiv">
    <div class="childDiv"></div>
    <div class="childDiv">
      Hi, this is default text. It can be increase accordingly
    </div>
    <div class="childDiv"></div>
  </div>
93ze6v8z

93ze6v8z3#

是的,您可以获得父元素的高度&javascript中中间元素的高度,然后选择其他两个div,并给它们剩余部分的百分比,以获得元素的高度:document.getelementbyid('myid')。越界
设置文档的高度。getelementbyid(“otherid”)。style.height=所需的高度

相关问题