gpt4 book ai didi

css - 需要在父级中设置高度以使拉伸(stretch)内容内部可滚动

转载 作者:行者123 更新时间:2023-11-27 22:54:50 25 4
gpt4 key购买 nike

我是一名日本工程师,我的英语不好,抱歉。

我想让“.main-content”可滚动。

我做到了。

它工作正常。

但是我有一个问题。

为什么我必须将 height: 0; 设置为类“.body”。

这是我的代码。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>

<style type="text/css">
* {
margin: 0;
box-sizing: border-box;
}
body {
width: 100vw;
height: 100vh;
padding: 32px;
overflow: hidden;
}
.container {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
}
.heading {
background-color: cornflowerblue;
}
.body {
display: flex;
flex-grow: 1;
height: 0;
}
.left-nav {
flex-basis: 200px;
max-height: 300px;
overflow-y: auto;
background-color: lightblue;
}
.left-nav-item, .main-content-item {
height: 80px;
width: 80px;
margin: 8px;
background-color: lightgray;
}
.main-content {
flex-grow: 1;
overflow-y: auto;
background-color: lightsteelblue;
}
.footing {
background-color: lightseagreen;
}
</style>

</head>
<body>
<div class="container">
<div class="heading">
heading
</div>
<div class="body">
<div class="left-nav">
<div class="left-nav-item">
left-nav-item1
</div>
<div class="left-nav-item">
left-nav-item2
</div>
<div class="left-nav-item">
left-nav-item3
</div>
<div class="left-nav-item">
left-nav-item4
</div>
<div class="left-nav-item">
left-nav-item5
</div>
<div class="left-nav-item">
left-nav-item6
</div>
<div class="left-nav-item">
left-nav-item7
</div>
<div class="left-nav-item">
left-nav-item8
</div>
</div>
<div class="main-content">
<div class="main-content-item">
main-content-item1
</div>
<div class="main-content-item">
main-content-item2
</div>
<div class="main-content-item">
main-content-item3
</div>
<div class="main-content-item">
main-content-item4
</div>
<div class="main-content-item">
main-content-item5
</div>
<div class="main-content-item">
main-content-item6
</div>
<div class="main-content-item">
main-content-item7
</div>
<div class="main-content-item">
main-content-item8
</div>
</div>
</div>
<div class="footing">
footing
</div>
</div>
</body>
</html>

“.main-content”按“.body”的默认值垂直拉伸(stretch),align-content: stretch

但是如果“.main-content”的内部溢出,“.main-content”的高度会变大。

而且,overflow-y: auto; 不起作用。

但是如果我给 height: 0;(0 并不重要。除了 auto 之外的一些值是需要的。)给类“.body”,“.main-content”得到适当的高度和变得可滚动。

它现在可以正常工作了,但我想知道为什么我需要给类“.body”height: 0;

最佳答案

欢迎来到 Stack overflow!

The overflow property specifies what should happen if content overflows an element's box

.

此属性指定当元素的内容太大无法容纳在指定区域时是裁剪内容还是添加滚动条。

Note: The overflow property only works for block elements with a specified height.

这是 overflow 的默认行为,如果你没有给出任何特定的高度,元素应该溢出一些高度,内容不会溢出。

In order for overflow to work on an element’s content, the element needs to have a definite measurement, otherwise, the element container will keep on expanding to accommodate as many content elements it can have or no matter the content’s size, but if the container element has a set height or a cap height ( max-height), once its content reaches those limits, it will follow the overflow rule, which in case of scroll, it will hide any content that should be surpassing the height of the container, and it will allow us to see it by dragging a scroll bar.

详细说明的引用链接

www.w3schools.com

关于css - 需要在父级中设置高度以使拉伸(stretch)内容内部可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59297896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com