gpt4 book ai didi

HTML flexbox 布局 - 滚动元素不会拉伸(stretch)

转载 作者:太空狗 更新时间:2023-10-29 16:39:52 27 4
gpt4 key购买 nike

我正在尝试创建一个看起来像这样的布局:

enter image description here

页眉和页脚固定在窗口上,如果需要空间,蓝色区域会垂直滚动。

我以为 display:flex 可以做到,下面的代码很接近,但是有一个问题。如果您滚动该内容区域,它看起来像这样:

enter image description here

看起来“左侧导航”和“内容”的 div 被切到了它们父级的高度,即使该父级是可滚动的。

var linesToAdd = 100;
var html = "";
for (var i=0; i<linesToAdd; i++) {
html += i + "<br/>"
}
document.getElementById('content').innerHTML = html;
html {
height: 100%;
}
body {
margin: 0;
display: flex;
flex-direction: column;
height:100%
}
#header, #footer {
background: #ffa0a0;
flex-shrink:0;
}
#mid {
flex-grow: 1;
background: #a08080;
overflow-y: scroll;
display: flex;
}
#leftnav {
padding: 10px;
background: #8a8;
}
#content {
background: #88a;
padding: 10px;
}
<!doctype html>
<html>
<body>
<div id="header">Header</div>

<div id='mid'>
<div id='leftnav'>Left nav</div>
<div id='content'></div>
</div>
<div id="footer">Footer</div>
</body>
</html>

现在,我可以稍微修改一下,让它在内容溢出时起作用,但当内容变小时它就不起作用了。我正在寻找垂直延伸到页脚的东西,当内容很小时,但当内容很大时会溢出而不会切断背景。

// With 100 here it looks good, but with 5, you see it doesn't stretch vertically.
var linesToAdd = 5;
var html = "";
for (var i=0; i<linesToAdd; i++) {
html += i + "<br/>"
}
document.getElementById('content').innerHTML = html;
html {
height: 100%;
}
body {
margin: 0;
display: flex;
flex-direction: column;
height:100%
}
#header, #footer {
background: #ffa0a0;
flex-shrink:0;
}
#mid {
flex-grow:1;
background:#a88;
overflow-y:scroll;
}
#leftnav {
padding: 10px;
background: #8a8;
}
#content {
background: #88a;
padding: 10px;
}
<!doctype html>
<html>
<body>
<div id="header">Header</div>

<div id='mid' style="">
<div style="display:flex">
<div id='leftnav'>Left nav</div>
<div id='content'></div>
</div>
</div>
<div id="footer">Footer</div>
</body>
</html>

最佳答案

您也需要在 HTML 上设置高度

faux columns方法可以在这里帮助你:(使用渐变重新访问方法,但多个背景也可以工作)

<html>
<style>
html {
height: 100%;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.headorfoot {
background: #ffa0a0;
flex-shrink: 0;
}
</style>

<body style="display:flex;flex-direction:column;height:100%">

<div class="headorfoot">
Header
</div>

<div id='mid' style="flex-grow:1;background:linear-gradient(to right,#80A080 200px,gray 200px , gray 203px, #8080A0 203px);;overflow-y:scroll; display:flex">
<!-- <div style="display:flex"> -->
<div style="padding: 10px;width:200px;">Left nav</div>
<div style="width:3px;"></div>
<div style="padding: 10px;background:#8080a0;flex:1">
<script>
for (var i = 0; i < 100; i++) {
document.write(i + "<br/>");
}
</script>
</div>
<!-- </div> -->
</div>
<div class="headorfoot">
Footer
</div>
</body>

</html>


您可能还想为左侧导航使用:position:sticky,(如果您认为有用,则存在 polyfills)

html,
body {
margin: 0;
height: 100%
}
.headorfoot {
background: #ffa0a0;
}
body {
display: flex;
flex-direction: column;
}
#mid {
background: #8080a0;;
display: flex;
flex: 1;
overflow: auto;
}
.scroll {
flex:1;
border-left:solid gray;

}
.nav {
position:sticky;
top:0;
}

/* demo purpose */
.scroll:hover br {
float:left;
display:none;/* where float doesn't work ... demo purpose only */
}
<div class="headorfoot">
Header
</div>

<div id='mid'>

<div class="nav" style="padding: 10px;background:#80a080">Left nav</div>
<div class="scroll" style="padding: 10px;background:#8080a0;">
<script>
for (var i = 0; i < 100; i++) {
document.write(i + "<br/>");
}
</script>
</div>
</div>
<div class="headorfoot">
Footer
</div>

关于HTML flexbox 布局 - 滚动元素不会拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35133106/

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