在CSS中,scrollingheights(当元素嵌套深度超过1级时)似乎应该是一个初学者任务,我正在努力解决这个问题.

我想设计一些布局,其中默认元素不会扩展到父元素之后,如果扩展到父元素之后,就会有overflow: auto个元素进入并开始滚动.

不过,我不想在每个元素上都设置height; 100%,因为我需要元素只占用所需的空间,所以一直try 在每个元素上使用max-height: 100%max-height: inherit.

使用height: 100%时,即使元素嵌套在几层深的地方,也会正确拾取父元素的高度,如图所示:Code Pen 1

html {
  padding: 0;
  margin: 0;
  overflow: hidden;
}

body {
  height: 95vh;
  background-color: red;
  overflow: hidden;
}

.levelOne {
  background-color: blue;
  height: 100%;
  /* Correctly gets parent height */
}

.levelTwo {
  background-color: green;
  overflow: auto;
  height: 100%;
  /* Correctly gets grand-parent height */
}
<div class="levelOne">
  <div class=levelTwo>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
  </div>
</div>

但是,当try 不使嵌套的div溢出max-height: 100%时,级别1的子级似乎正确地停止在父级高度,但级别2的子级将溢出.Code Pen 2

*,
*:before,
*:after {
  box-sizing: inherit;
  max-height: inherit;
  overflow: hidden;
}

html {
  padding: 0;
  margin: 0;
  overflow: hidden;
}

body {
  height: 95vh;
  background-color: red;
  overflow: hidden;
}

.levelOne {
  background-color: blue;
  max-height: 100%;
  /* Correctly gets parent height*/
}

.levelTwo {
  background-color: green;
  overflow: auto;
  max-height: 100%;
  /* DOESN'T get grandparent hight, but spills out of levelOne so overflow: auto never starts to scroll; */
}
<div class="levelOne">
  <div class=levelTwo>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
    <p>Lorem</p>
  </div>
</div>
  1. 为什么max-height: 100%不能达到祖父母的身高,而"身高:max-height: 100%%"却达到了祖父母的身高?我本以为会有类似的行为.
  2. 有没有更好的方法不允许元素溢出其父级大小?

我也看到过这样的建议,如果max-hight不能计算出第一层的深度,它也会失败:

*, *:before, *:after {
    box-sizing: inherit;
    max-height: inherit;
    overflow: hidden;
}

似乎出于某种原因,如果div是1x1 css网格,那么在更深层次上的工作会更加一致,但有时网格的开销太大,我希望能更好地理解其基本原理.

非常感谢.

推荐答案

我需要的元素只占所需的空间

设置相对于父元素的宽度不会使元素只占用所需的空间.默认情况下,height设置为auto,这只会使图元达到所需的高度.

阻止Level2溢出Level1&;普遍告诉所有元素永远不要扩展到它们的父元素之外

决定当你的子元素太大,无法适应父母明确的身高时,他们会做什么:

  • 明显溢出(默认)
  • 隐藏(坏习惯)
  • 纸卷
  1. 为什么max-height: 100%人的身高不超过祖父母的身高,而height: 100%人的身高超过祖父母的身高?我本以为会有类似的行为.

为什么每次设置height: 100%有效,但设置max-height: 100%无效

MDN - The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the percentage value is treated as none.

因此,由于levelOne容器有一个最大高度,而不是一个高度,因此最大高度被视为100%自动,而100%的"您需要多少"是一个无最大高度.

  1. 有没有更好的方法不允许元素溢出其父级大小?

请让我知道这个例子是否符合你的要求.

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  height: 95vh;
  background-color: red;
}

.levelOne {
  background-color: blue;
  max-height: 100%;
  /* Correctly gets parent height*/
  overflow: auto;
}

.levelTwo {
  background-color: green;
}
<div class="levelOne">
  <div class=levelTwo>
    <p>Lorem</p>
    <p>Lorem</p>
  </div>
</div>

levelOne将尽可能大,以适应默认CSS高度值为auto的内容.它将以最大高度限制在其父级高度的100%处,并在内容因overflow: auto而超过该高度时滚动.

Html相关问答推荐

如何将grid—template—column应用于元素中的子元素

为什么这个高度为100%的页面会出现滚动条?

我无法从带Angular 的Bootstrap 5中的表单中获取数据

尽管div高度为100%,div中的内容仍会溢出

在Firefox中,使用写入模式:Vertical-LR时,不随内容扩展的css弹性项

如何生成随机字符串的字母数字字符集长度到html跨度?

如何使div填充父项的剩余高度

带有下拉菜单的完整css导航-链接不起作用?

带有排序元素的 CSS 网格

在移动屏幕上显示时分支树视图的响应能力问题

如何使用CSS Select 表亲元素

用于表行组标题的正确 HTML 标记是什么?

如何将自定义图像插入 Shiny plot header?

什么没有 margin-right 和 width:100% 溢出子元素到左边?

如何将图像移动到父容器的右侧?

如何使用 CSS 使粘性元素固定在视口顶部

无法从社交栏中 Select 选项

CSS 变量不适用于自定义属性回退值

svg 内容超过元素

如何阻止网格项目拉伸?