首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将高度固定为相关元素的100%

如何将高度固定为相关元素的100%
EN

Stack Overflow用户
提问于 2022-11-17 17:00:20
回答 3查看 45关注 0票数 0

我正在寻找一个解决方案,以修复与CSS中的另一个div相关的div的最大高度,如果它超过它,应该让它使用滚动条。

我原计划使用柔性盒或网格,但我没有成功的结果想要的.

目标是不指定任何高度在px,但保持灵活的东西没有JS。

代码语言:javascript
复制
// Not looking for JS solution
代码语言:javascript
复制
.parent {
  display: flex;
}

.child1 {
  background: #eeeeee;
  height: 100%
}

.child2 {
  overflow: auto;
  max-height: 100%;
  background: #cccccc
}
代码语言:javascript
复制
<div class="parent">
  <div class="child1">
    <h2>Child1</h2>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
    <p>Some content</p>
  </div>
  <div class="child2">
    <h2>Child2</h2>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>
    <p>this content height must not exceed the height of "child1"</p>

  </div>
</div>

EN

回答 3

Stack Overflow用户

发布于 2022-11-17 17:08:21

如果您不想指定固定高度,只需添加max- not :calc(100 is 200 Is),这意味着如果屏幕高度=1000 is,您的div将不超过800 is,如果它大于div,则使用滚动条。

代码语言:javascript
复制
.parent {
 display: flex;
 max-height: calc(100vh - 100px)
}

.child1 {
  background: #eeeeee;
  overflow:auto;
  width:max-content;
}

.child2 {
 overflow: auto;
 max-height: 100%;
 background: #cccccc
}

这里查看代码示例

票数 -1
EN

Stack Overflow用户

发布于 2022-11-17 17:17:26

票数 -1
EN

Stack Overflow用户

发布于 2022-11-17 17:46:58

高度:自动;最大高度:100 max;

将这两行添加到child2类中

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

https://stackoverflow.com/questions/74479339

复制
相关文章

相似问题

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