首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将一个元素的高度设置为等于另一个元素?(包含Codepen)

如何将一个元素的高度设置为等于另一个元素?(包含Codepen)
EN

Stack Overflow用户
提问于 2020-06-02 19:41:02
回答 2查看 48关注 0票数 0

我有两个Bootstrap列,其中有4个div(每个div 2个):

代码语言:javascript
复制
<div class="row">
  <div class="col-8">
    <div class="cell-1">
      Cell 1
    </div>
    <div class="cell-2">
      Cell 2
      <ul>
        <li>Some repeatable text</li>
        <li>Some repeatable text</li>
      </ul>
    </div>
  </div>
  <div class="col-4">
    <div class="cell-3">
      Cell 3
    </div>
    <div class="cell-4" style="overflow-y:auto; max-height:[height of cell-2]">
      Cell 4
      <ul>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
      </ul>
    </div>
  </div>
</div>

.cell-4overflow-y: auto。如何设置.cell-4max-height等于.cell-2的动态高度?

理想情况下没有Javacript。

Codepen在这里:link

EN

回答 2

Stack Overflow用户

发布于 2020-06-02 19:46:43

使用display:flexalign-items: stretch

代码语言:javascript
复制
.col-4{
  display: flex;
  align-items: center;
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-02 22:31:14

我已经修改了CSS flexbox的配置以使其正常工作。col-4和col-8被配置为挠性柱。然后,cell-4添加flex-basis和flex-grow配置。

代码语言:javascript
复制
.cell-1{
  background: red;
}
.cell-2{
  background: blue;
}
.cell-3{
  background: green;
}
.cell-4{
  background: pink;
  overflow-x: hidden;
  /* overflow-y: scroll; */
  /* max-height: 50px; // Set this to equal height of Cell 2 */
  /* Addition flex configuration to make it work */
  flex-basis: 0px;
  flex-grow: 1;
  overflow-y: auto;
}

/* Addition flex configuration to make it work */
.col-4, .col-8 {
  display: flex;
  flex-direction: column;
}
代码语言:javascript
复制
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<div class="row">
  <div class="col-8">
    <div class="cell-1">
      Cell 1
    </div>
    <div class="cell-2">
      Cell 2
      <ul>
        <li>Some repeatable text</li>
        <li>Some repeatable text</li>
      </ul>
    </div>
  </div>
  <div class="col-4">
    <div class="cell-3">
      Cell 3
    </div>
    <div class="cell-4" style="overflow-y:auto; max-height:[height of cell-2]">
      Cell 4
      <ul>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
        <li>Lots of repeatable text</li>
      </ul>
    </div>
  </div>
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62151177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档